Skip to content

Commit 6cacb97

Browse files
committed
♻️(backend) fallback to email identifier when no name
In the UserlightSerializer, if the user has no short_name or full_name, we have no info about the user. We decided to use the email identifier and slugify it to have a little bit information. Fix #1210
1 parent 586825a commit 6cacb97

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to
2424
- #1244
2525
- #1270
2626
- #1282
27+
- ♻️(backend) fallback to email identifier when no name #1298
2728

2829
### Fixed
2930

src/backend/core/api/serializers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.conf import settings
88
from django.db.models import Q
99
from django.utils.functional import lazy
10+
from django.utils.text import slugify
1011
from django.utils.translation import gettext_lazy as _
1112

1213
import magic
@@ -32,11 +33,30 @@ class Meta:
3233
class UserLightSerializer(UserSerializer):
3334
"""Serialize users with limited fields."""
3435

36+
full_name = serializers.SerializerMethodField(read_only=True)
37+
short_name = serializers.SerializerMethodField(read_only=True)
38+
3539
class Meta:
3640
model = models.User
3741
fields = ["full_name", "short_name"]
3842
read_only_fields = ["full_name", "short_name"]
3943

44+
def get_full_name(self, instance):
45+
"""Return the full name of the user."""
46+
if not instance.full_name:
47+
email = instance.email.split("@")[0]
48+
return slugify(email)
49+
50+
return instance.full_name
51+
52+
def get_short_name(self, instance):
53+
"""Return the short name of the user."""
54+
if not instance.short_name:
55+
email = instance.email.split("@")[0]
56+
return slugify(email)
57+
58+
return instance.short_name
59+
4060

4161
class TemplateAccessSerializer(serializers.ModelSerializer):
4262
"""Serialize template accesses."""
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""Test user light serializer."""
2+
3+
import pytest
4+
5+
from core import factories
6+
from core.api.serializers import UserLightSerializer
7+
8+
pytestmark = pytest.mark.django_db
9+
10+
11+
def test_user_light_serializer():
12+
"""Test user light serializer."""
13+
user = factories.UserFactory(
14+
email="test@test.com",
15+
full_name="John Doe",
16+
short_name="John",
17+
)
18+
serializer = UserLightSerializer(user)
19+
assert serializer.data["full_name"] == "John Doe"
20+
assert serializer.data["short_name"] == "John"
21+
22+
23+
def test_user_light_serializer_no_full_name():
24+
"""Test user light serializer without full name."""
25+
user = factories.UserFactory(
26+
email="test_foo@test.com",
27+
full_name=None,
28+
short_name="John",
29+
)
30+
serializer = UserLightSerializer(user)
31+
assert serializer.data["full_name"] == "test_foo"
32+
assert serializer.data["short_name"] == "John"
33+
34+
35+
def test_user_light_serializer_no_short_name():
36+
"""Test user light serializer without short name."""
37+
user = factories.UserFactory(
38+
email="test_foo@test.com",
39+
full_name=None,
40+
short_name=None,
41+
)
42+
serializer = UserLightSerializer(user)
43+
assert serializer.data["full_name"] == "test_foo"
44+
assert serializer.data["short_name"] == "test_foo"

0 commit comments

Comments
 (0)