Skip to content

Commit

Permalink
♻️(backend) extract user name to model property
Browse files Browse the repository at this point in the history
As we have the same logic to get the best available user name, it has
been moved to the user model.
  • Loading branch information
kernicPanel committed Dec 19, 2024
1 parent 33d75d6 commit 9e5a405
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/backend/joanie/core/models/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ def __init__(self, *args, **kwargs):
def __str__(self):
return self.username

@property
def name(self):
"""
Return the full name of the user if available, otherwise the username.
"""
return self.get_full_name() or self.username

def clean(self):
"""
Normalize the `phone_number` value for consistency in database.
Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/core/models/certifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def get_document_context(self, language_code=None):
"delivery_stamp": timezone.now(),
"verification_link": self.verification_uri,
"student": {
"name": self.owner.get_full_name() or self.owner.username,
"name": self.owner.name,
},
"site": {
"name": settings.JOANIE_CATALOG_NAME,
Expand Down
6 changes: 3 additions & 3 deletions src/backend/joanie/core/serializers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ def get_owner_name(self, instance) -> str:
Return the full name of the order's owner if available,
otherwise fallback to the username
"""
return instance.owner.get_full_name() or instance.owner.username
return instance.owner.name


class AdminOrderExportSerializer(serializers.ModelSerializer): # pylint: disable=too-many-public-methods
Expand Down Expand Up @@ -1396,7 +1396,7 @@ def get_owner_name(self, instance) -> str:
Return the full name of the order's owner if available,
otherwise fallback to the username
"""
return instance.owner.get_full_name() or instance.owner.username
return instance.owner.name

def get_enrollment_created_on(self, instance) -> str:
"""
Expand Down Expand Up @@ -1609,7 +1609,7 @@ def get_user_name(self, instance) -> str:
Return the full name of the enrollment's user if available,
otherwise fallback to the username
"""
return instance.user.get_full_name() or instance.user.username
return instance.user.name


class AdminEnrollmentSerializer(serializers.ModelSerializer):
Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/core/serializers/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def get_owner_name(self, instance) -> str:
"""
Return the name full name of the order's owner or fallback to username
"""
return instance.owner.get_full_name() or instance.owner.username
return instance.owner.name


class CertificationDefinitionSerializer(serializers.ModelSerializer):
Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/core/utils/contract_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def generate_document_context(contract_definition=None, user=None, order=None):
contract_description = contract_definition.description

if user:
user_name = user.get_full_name() or user.username
user_name = user.name
user_email = user.email
user_phone_number = user.phone_number

Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/core/utils/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def prepare_context_data(
or refused.
"""
context_data = {
"fullname": order.owner.get_full_name() or order.owner.username,
"fullname": order.owner.name,
"email": order.owner.email,
"product_title": product_title,
"installment_amount": Money(installment_amount),
Expand Down
4 changes: 2 additions & 2 deletions src/backend/joanie/debug/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = "👨‍💻Development email preview"
context["email"] = order.owner.email
context["fullname"] = order.owner.get_full_name() or order.owner.username
context["fullname"] = order.owner.name
context["product"] = order.product
context["site"] = {
"name": settings.JOANIE_CATALOG_NAME,
Expand Down Expand Up @@ -122,7 +122,7 @@ def get_context_data(self, **kwargs):
targeted_installment_index=order.get_installment_index(
state=PAYMENT_STATE_PAID
),
fullname=order.owner.get_full_name() or order.owner.username,
fullname=order.owner.name,
email=order.owner.email,
dashboard_order_link=settings.JOANIE_DASHBOARD_ORDER_LINK,
site={
Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/payment/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _send_mail_subscription_success(cls, order):
template_vars={
"title": _("Subscription confirmed!"),
"email": order.owner.email,
"fullname": order.owner.get_full_name() or order.owner.username,
"fullname": order.owner.name,
"product": order.product,
"site": {
"name": settings.JOANIE_CATALOG_NAME,
Expand Down

0 comments on commit 9e5a405

Please sign in to comment.