From 6cfd80d61e6a649863a4d9d53aa9c7057049aaa0 Mon Sep 17 00:00:00 2001 From: kdeterme Date: Wed, 12 Aug 2026 04:44:18 +0200 Subject: [PATCH] refactor: replace full name logic with short_name template filter and utility across inspection views and templates --- .../templates/assets/components/asset_inspections.html | 6 +++--- streetup/common/templatetags/short_name.py | 3 ++- streetup/common/utils.py | 3 ++- .../templates/inspections/inspections_list.html | 8 ++++---- streetup/inspections/views.py | 3 ++- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/streetup/assets/templates/assets/components/asset_inspections.html b/streetup/assets/templates/assets/components/asset_inspections.html index 91c4d8d..20fd05c 100644 --- a/streetup/assets/templates/assets/components/asset_inspections.html +++ b/streetup/assets/templates/assets/components/asset_inspections.html @@ -1,4 +1,4 @@ -{% load i18n static inspections_tags %} +{% load i18n static inspections_tags short_name %} {# Inspections et contrôles liés à l'asset #}
@@ -53,7 +53,7 @@ {{ insp.get_nature_display }} - {{ insp.inspector.get_full_name|default:insp.inspector.username|default:"-" }} + {{ insp.inspector|short_name|default:insp.inspector.username|default:"-" }} @@ -110,7 +110,7 @@
  • {% translate "Inspecteur" %} - {{ insp.inspector.get_full_name|default:insp.inspector.username|default:"-" }} + {{ insp.inspector|short_name|default:insp.inspector.username|default:"-" }}
  • {% get_inspection_detail_template insp as detail_template %} diff --git a/streetup/common/templatetags/short_name.py b/streetup/common/templatetags/short_name.py index 2ab58ef..28215f0 100644 --- a/streetup/common/templatetags/short_name.py +++ b/streetup/common/templatetags/short_name.py @@ -13,7 +13,8 @@ def short_name(user): first = getattr(user, "first_name", "") or "" last = getattr(user, "last_name", "") or "" initial = (last[0].upper() + ".") if last else "" - return f"{first} {initial}".strip() + res = f"{first} {initial}".strip() + return res if res else getattr(user, "username", "") @register.filter diff --git a/streetup/common/utils.py b/streetup/common/utils.py index d904ef7..344f6f8 100644 --- a/streetup/common/utils.py +++ b/streetup/common/utils.py @@ -122,7 +122,8 @@ def get_short_name(user): first = getattr(user, "first_name", "") or "" last = getattr(user, "last_name", "") or "" initial = (last[0].upper() + ".") if last else "" - return f"{first} {initial}".strip() + res = f"{first} {initial}".strip() + return res if res else getattr(user, "username", "") from django.utils.translation import gettext_lazy as _ diff --git a/streetup/inspections/templates/inspections/inspections_list.html b/streetup/inspections/templates/inspections/inspections_list.html index b5c32a2..caf8c65 100644 --- a/streetup/inspections/templates/inspections/inspections_list.html +++ b/streetup/inspections/templates/inspections/inspections_list.html @@ -1,5 +1,5 @@ {% extends base_template|default:"base.html" %} -{% load i18n static l10n %} +{% load i18n static l10n short_name %} {% block title %}{% translate "Inspections d'assets" %}{% endblock %} @@ -150,7 +150,7 @@
    {% endif %} {% if insp.inspector %} - {{ insp.inspector.get_full_name|default:insp.inspector.username }} + {{ insp.inspector|short_name|default:insp.inspector.username }} {% endif %}
    @@ -201,7 +201,7 @@
  • {% translate "Inspecteur" %} - {{ insp.inspector.get_full_name|default:insp.inspector.username|default:"-" }} + {{ insp.inspector|short_name|default:insp.inspector.username|default:"-" }}
  • {% with leaf=insp.detailed_inspection %} @@ -454,7 +454,7 @@ document.addEventListener('DOMContentLoaded', function() { result: "{{ insp.get_result_status_display|escapejs }}", result_code: "{{ insp.result_status|escapejs }}", badge_class: "{{ insp.status_badge_class|escapejs }}", - inspector: "{{ insp.inspector.get_full_name|default:insp.inspector.username|escapejs }}", + inspector: "{{ insp.inspector|short_name|default:insp.inspector.username|escapejs }}", nature: "{{ insp.get_nature_display|escapejs }}", summary: "{{ insp.details_summary|escapejs }}" }{% if not forloop.last %},{% endif %} diff --git a/streetup/inspections/views.py b/streetup/inspections/views.py index 23a868c..77a2196 100644 --- a/streetup/inspections/views.py +++ b/streetup/inspections/views.py @@ -8,6 +8,7 @@ from django.utils import timezone from django.utils.translation import gettext as _ from common.models import Thematic +from common.utils import get_short_name from assets.models import AssetCategory from .models import ( Inspection, ParkingSpotInspection, GiepInspection, InspectionDocument, @@ -547,7 +548,7 @@ def inspections_geojson(request): 'result_status_display': insp.get_result_status_display(), 'status_badge_class': insp.status_badge_class, 'nature_display': insp.get_nature_display(), - 'inspector_name': insp.inspector.get_full_name() if insp.inspector else '-', + 'inspector_name': get_short_name(insp.inspector) if insp.inspector else '-', 'inspector_role_display': insp.get_inspector_role_display() if insp.inspector_role else '', 'details_summary': insp.details_summary, 'notes': insp.notes or '',