models: order AttributeValue according to corresponding attribute (#47627)

This commit is contained in:
Valentin Deniaud 2020-10-13 14:54:26 +02:00
parent c0590210ab
commit 7216172fd0
2 changed files with 22 additions and 0 deletions

View File

@ -336,6 +336,7 @@ class AttributeValue(models.Model):
class Meta:
verbose_name = _('attribute value')
ordering = ('attribute__order', 'id')
verbose_name_plural = _('attribute values')
unique_together = (
('content_type', 'object_id', 'attribute', 'multiple', 'content'),

View File

@ -16,6 +16,7 @@
# authentic2
import pytest
import datetime
from django.core.exceptions import ValidationError
from django.core import management
@ -215,3 +216,23 @@ def test_attributes_hasattr(db):
assert not hasattr(user.verified_attributes, 'email')
assert hasattr(user.verified_attributes, 'first_name')
assert user.verified_attributes.last_name is None
def test_attribute_values_order(db):
phone = Attribute.objects.create(name='phone', label='phone', kind='string', order=9)
birthdate = Attribute.objects.create(name='birthdate', label='birthdate', kind='birthdate', order=10)
user = User.objects.create(first_name='john', last_name='Doe')
user.attributes.phone = '0123456789'
user.attributes.birthdate = datetime.date(year=1980, month=1, day=2)
attribute_values = user.attribute_values.all().reverse()
val1, val2 = attribute_values[:2]
assert val1.attribute.label == 'birthdate'
assert val2.attribute.label == 'phone'
phone.order, birthdate.order = birthdate.order, phone.order
phone.save()
birthdate.save()
val1, val2 = attribute_values[:2]
assert val1.attribute.label == 'phone'
assert val2.attribute.label == 'birthdate'