diff --git a/openlobby/core/models.py b/openlobby/core/models.py index 6dabe4201f11fecc1fdc9cd1d6e09e5f2fe55896..74d21119c62a8c66b1dac89f4707c6bd9b01d9c8 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 7477d8409a190d2b2b82772b1425ef6693cfe4e0..3048752736590939a8b95c4210d424544768e18b 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