diff --git a/myhpi/core/models.py b/myhpi/core/models.py index 1d592743..52a31cfb 100644 --- a/myhpi/core/models.py +++ b/myhpi/core/models.py @@ -33,6 +33,10 @@ def __str__(self): def get_full_name(self): return self.display_name + @staticmethod + def for_user(u): + return UserProfile.objects.get(user=u) + from django.db.models.signals import post_save from django.dispatch import receiver @@ -41,10 +45,10 @@ def get_full_name(self): @receiver(post_save, sender=User) def post_user_save(sender, instance, created, **kwargs): display_name = instance.first_name + " " + instance.last_name - user_profile = UserProfile.objects.get(user=instance.pk) + user_profile = UserProfile.objects.filter(user=instance.pk) if user_profile: - user_profile.display_name = display_name - user_profile.save() + user_profile[0].display_name = display_name + user_profile[0].save() else: UserProfile.objects.create(user=instance, display_name=display_name) diff --git a/myhpi/tests/core/setup.py b/myhpi/tests/core/setup.py index f13e71f0..8631699f 100644 --- a/myhpi/tests/core/setup.py +++ b/myhpi/tests/core/setup.py @@ -10,6 +10,7 @@ MinutesList, RootPage, SecondLevelMenuItem, + UserProfile, ) @@ -171,9 +172,9 @@ def setup_minutes(group, students_group, parent, user): date="2022-01-01", is_public=False, visible_for=[students_group, group], - moderator=user, - author=user, - participants=[user], + moderator=UserProfile.for_user(user), + author=UserProfile.for_user(user), + participants=[UserProfile.for_user(user)], body="These are the first minutes.", slug="first-minutes", ), @@ -193,9 +194,9 @@ def setup_minutes(group, students_group, parent, user): date="2022-03-03", is_public=False, visible_for=[group], - moderator=user, - author=user, - participants=[user], + moderator=UserProfile.for_user(user), + author=UserProfile.for_user(user), + participants=[UserProfile.for_user(user)], body="These minutes are private.", slug="private-minutes", ), @@ -205,9 +206,9 @@ def setup_minutes(group, students_group, parent, user): is_public=False, live=False, visible_for=[group], - moderator=user, - author=user, - participants=[user], + moderator=UserProfile.for_user(user), + author=UserProfile.for_user(user), + participants=[UserProfile.for_user(user)], body="These minutes are unpublished.", slug="unpublished-minutes", ), @@ -216,9 +217,9 @@ def setup_minutes(group, students_group, parent, user): date="2022-05-05", is_public=False, visible_for=[students_group, group], - moderator=user, - author=user, - participants=[user], + moderator=UserProfile.for_user(user), + author=UserProfile.for_user(user), + participants=[UserProfile.for_user(user)], body="These minutes are the most recent.", slug="recent-minutes", ),