refactor: replace full name logic with short_name template filter and utility across inspection views and templates

This commit is contained in:
kdeterme 2026-08-12 04:44:18 +02:00
parent e6166b6adb
commit 6cfd80d61e
5 changed files with 13 additions and 10 deletions

View file

@ -1,4 +1,4 @@
{% load i18n static inspections_tags %}
{% load i18n static inspections_tags short_name %}
{# Inspections et contrôles liés à l'asset #}
<div class="card mb-3" id="asset-inspections-card">
@ -53,7 +53,7 @@
<span class="badge bg-light text-dark border">{{ insp.get_nature_display }}</span>
</td>
<td>
<i class="bi bi-person me-1 text-muted"></i>{{ insp.inspector.get_full_name|default:insp.inspector.username|default:"-" }}
<i class="bi bi-person me-1 text-muted"></i>{{ insp.inspector|short_name|default:insp.inspector.username|default:"-" }}
</td>
<td>
<span class="small text-muted text-truncate d-inline-block" style="max-width: 220px;" title="{{ insp.details_summary }}">
@ -110,7 +110,7 @@
</li>
<li class="list-group-item d-flex justify-content-between align-items-center px-0">
<span class="text-muted"><i class="bi bi-person me-1"></i>{% translate "Inspecteur" %}</span>
<span class="fw-semibold">{{ insp.inspector.get_full_name|default:insp.inspector.username|default:"-" }}</span>
<span class="fw-semibold">{{ insp.inspector|short_name|default:insp.inspector.username|default:"-" }}</span>
</li>
{% get_inspection_detail_template insp as detail_template %}

View file

@ -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

View file

@ -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 _

View file

@ -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 @@
</span>
{% endif %}
{% if insp.inspector %}
<span><i class="bi bi-person me-1"></i>{{ insp.inspector.get_full_name|default:insp.inspector.username }}</span>
<span><i class="bi bi-person me-1"></i>{{ insp.inspector|short_name|default:insp.inspector.username }}</span>
{% endif %}
</div>
</div>
@ -201,7 +201,7 @@
</li>
<li class="list-group-item d-flex justify-content-between align-items-center px-0">
<span class="text-muted"><i class="bi bi-person me-1"></i>{% translate "Inspecteur" %}</span>
<span class="fw-semibold">{{ insp.inspector.get_full_name|default:insp.inspector.username|default:"-" }}</span>
<span class="fw-semibold">{{ insp.inspector|short_name|default:insp.inspector.username|default:"-" }}</span>
</li>
{% 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 %}

View file

@ -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 '',