Skip to content

Commit

Permalink
πŸ§‘β€πŸ’»(models) improve user str representation
Browse files Browse the repository at this point in the history
Improve user model str representation to display name or email
if provided. Otherwise, returns sub as last resort.
  • Loading branch information
mjeammet committed Jul 3, 2024
1 parent 2598f36 commit 66300ac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
6 changes: 1 addition & 5 deletions src/backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,7 @@ class Meta:
verbose_name_plural = _("users")

def __str__(self):
return (
str(self.profile_contact)
if self.profile_contact
else self.email or str(self.sub)
)
return self.name if self.name else self.email or f"User {self.sub}"

def save(self, *args, **kwargs):
"""
Expand Down
7 changes: 2 additions & 5 deletions src/backend/core/tests/test_models_team_accesses.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ def test_models_team_accesses_str():
"""
The str representation should include user name, team full name and role.
"""
contact = factories.ContactFactory(full_name="David Bowman")
user = contact.owner
user.profile_contact = contact
user.save()
user = factories.UserFactory()
access = factories.TeamAccessFactory(
role="member",
user=user,
team__name="admins",
)
assert str(access) == "David Bowman is member in team admins"
assert str(access) == f"{user} is {access.role} in team {access.team}"


def test_models_team_accesses_unique():
Expand Down
15 changes: 10 additions & 5 deletions src/backend/core/tests/test_models_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@


def test_models_users_str():
"""The str representation should be the full name."""
"""
user str representation should return name or email when avalaible.
Otherwise, it should return the sub.
"""
user = factories.UserFactory()
contact = factories.ContactFactory(full_name="david bowman", owner=user)
user.profile_contact = contact
user.save()
assert str(user) == user.name

no_name_user = factories.UserFactory(name=None)
assert str(no_name_user) == no_name_user.email

assert str(user) == "david bowman"
no_name_no_email_user = factories.UserFactory(name=None, email=None)
assert str(no_name_no_email_user) == f"User {no_name_no_email_user.sub}"


def test_models_users_id_unique():
Expand Down

0 comments on commit 66300ac

Please sign in to comment.