From 6a08e1416573fbc252a70f38a96d1fcca004b458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Thu, 28 Jun 2018 12:10:31 +0000 Subject: [PATCH] Otimize DB request for the right page Reimplement end_adhesion and is_adherent methods in the query itself so it can be done in a single query and not done for each user individually. --- users/templates/users/aff_listright.html | 4 ++-- users/views.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/users/templates/users/aff_listright.html b/users/templates/users/aff_listright.html index 3c7137bf..e7d3e64a 100644 --- a/users/templates/users/aff_listright.html +++ b/users/templates/users/aff_listright.html @@ -70,7 +70,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% for user in superusers %} {{ user.pseudo }} - {% if user.is_adherent %} + {% if user.end_adhesion is not None and user.end_adhesion >= now %} {% trans "Member" %} {% elif not user.end_adhesion %} {% trans "No membership records" %} @@ -175,7 +175,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% for user in users %} {{ user.pseudo }} - {% if user.is_adherent %} + {% if user.end_adhesion is not None and user.end_adhesion >= now %} {% trans "Member" %} {% elif not user.end_adhesion %} {% trans "No membership records" %} diff --git a/users/views.py b/users/views.py index b427f710..4072e6a7 100644 --- a/users/views.py +++ b/users/views.py @@ -811,15 +811,18 @@ def index_listright(request): .order_by('name') .prefetch_related('permissions') .prefetch_related('user_set') + .prefetch_related('user_set__facture_set__vente_set__cotisation') ): rights[right] = (right.user_set .annotate(action_number=Count('revision'), - last_seen=Max('revision__date_created')) + last_seen=Max('revision__date_created'), + end_adhesion=Max('facture__vente__cotisation__date_end')) ) superusers = (User.objects .filter(is_superuser=True) .annotate(action_number=Count('revision'), - last_seen=Max('revision__date_created')) + last_seen=Max('revision__date_created'), + end_adhesion=Max('facture__vente__cotisation__date_end')) ) return render( request,