From 7216172fd09fa7ce136fb64cf64cd98e7369d38b Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Tue, 13 Oct 2020 14:54:26 +0200 Subject: [PATCH] models: order AttributeValue according to corresponding attribute (#47627) --- src/authentic2/models.py | 1 + tests/test_user_model.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/authentic2/models.py b/src/authentic2/models.py index 14f40e27a..d914424f0 100644 --- a/src/authentic2/models.py +++ b/src/authentic2/models.py @@ -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'), diff --git a/tests/test_user_model.py b/tests/test_user_model.py index afea5ab84..58a6f566d 100644 --- a/tests/test_user_model.py +++ b/tests/test_user_model.py @@ -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'