From 2c10c1c55272da1eb81fca3112243c2381bfe780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Wed, 21 Feb 2018 11:53:54 +0100 Subject: [PATCH] Fix updating of name collision id on every save of existing user. --- openlobby/core/models.py | 5 +++-- tests/test_models.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/openlobby/core/models.py b/openlobby/core/models.py index 6dabe42..74d2111 100644 --- a/openlobby/core/models.py +++ b/openlobby/core/models.py @@ -18,9 +18,10 @@ class User(AbstractUser): def save(self, *args, **kwargs): # deal with first name and last name collisions collisions = User.objects.filter(first_name=self.first_name, last_name=self.last_name)\ - .order_by('-name_collision_id')[:1] - if len(collisions) > 0: + .order_by('-name_collision_id') + if len(collisions) > 0 and self not in collisions: self.name_collision_id = collisions[0].name_collision_id + 1 + # TODO when we allow name change, it should also reset name_collision_id super().save(*args, **kwargs) diff --git a/tests/test_models.py b/tests/test_models.py index 7477d84..3048752 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -90,3 +90,14 @@ def test_user__name_collision(): assert u1.name_collision_id == 0 assert u2.name_collision_id == 1 assert u3.name_collision_id == 2 + + +def test_user__name_collision_is_not_updated_for_existing_user(): + u1 = User.objects.create(username='a', first_name='Ryan', last_name='Reynolds') + u2 = User.objects.create(username='b', first_name='Ryan', last_name='Reynolds') + assert u1.name_collision_id == 0 + assert u2.name_collision_id == 1 + u1.save() + u2.save() + assert u1.name_collision_id == 0 + assert u2.name_collision_id == 1 -- GitLab