Skip to content

Commit

Permalink
admin user: display info about relais
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Feb 14, 2025
1 parent 15b3844 commit dd0ab27
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions mesads/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from reversion.models import Revision

from mesads.app.models import Notification
from mesads.vehicules_relais.models import Proprietaire

from .models import User

Expand Down Expand Up @@ -129,6 +130,7 @@ class UserAdmin(BaseUserAdmin):
"admin_roles_link",
"ads_manager_request_link",
"notifications_link",
"relais_links",
)

readonly_fields = (
Expand All @@ -142,6 +144,7 @@ class UserAdmin(BaseUserAdmin):
# change them.
"is_staff",
"is_superuser",
"relais_links",
)

search_fields = (
Expand Down Expand Up @@ -229,3 +232,48 @@ def has_delete_permission(self, request, obj=None):
if count:
return False
return True

@admin.display(description="Registre des véhicules relais")
def relais_links(self, obj):
proprietaires = Proprietaire.objects.filter(users__in=[obj]).annotate(
vehicules_count=Count("vehicule")
)

ret = """
<table>
<thead>
<tr>
<th>Propriétaire</th>
<th>Nombre de véhicules</th>
</tr>
</thead>
<tbody>
"""
if len(proprietaires) == 0:
ret += """
<tr>
<td colspan="2">Aucun compte propriétaire associé</td>
</tr>
"""
else:
for proprietaire in proprietaires:
proprietaire_url = reverse(
"admin:vehicules_relais_proprietaire_change",
kwargs={"object_id": proprietaire.id},
)
vehicules_url = (
reverse("admin:vehicules_relais_vehicule_changelist")
+ f"?proprietaire={proprietaire.id}"
)
ret += f"""
<tr>
<td><a href="{proprietaire_url}">{proprietaire.nom}</a></td>
<td><a href="{vehicules_url}">Voir le(s) {proprietaire.vehicules_count} véhicule(s)</a></td>
</tr>
"""

ret += """
</tbody>
</table>
"""
return mark_safe(ret)

0 comments on commit dd0ab27

Please sign in to comment.