From 742ac504dc42da54753569d8717df81d92cd515b Mon Sep 17 00:00:00 2001 From: imperosol Date: Fri, 7 Nov 2025 13:07:46 +0100 Subject: [PATCH] optimize db requests on club sales view --- club/templates/club/club_sellings.jinja | 15 ++++++++------ club/views.py | 26 ++++++++++++------------- core/templates/core/macros.jinja | 4 ++-- locale/fr/LC_MESSAGES/django.po | 26 +++++++------------------ 4 files changed, 31 insertions(+), 40 deletions(-) diff --git a/club/templates/club/club_sellings.jinja b/club/templates/club/club_sellings.jinja index 3733d0c8..59edd18e 100644 --- a/club/templates/club/club_sellings.jinja +++ b/club/templates/club/club_sellings.jinja @@ -6,11 +6,11 @@ because it works with a somewhat dynamic form, but was written before Alpine was introduced in the project. TODO : rewrite the pagination used in this template an Alpine one #} -{% macro paginate(page_obj, paginator, js_action) %} - {% set js = js_action|default('') %} +{% macro paginate(page_obj, paginator) %} + {% set js = "formPagination(this)" %} {% if page_obj.has_previous() or page_obj.has_next() %} {% if page_obj.has_previous() %} - {% trans %}Previous{% endtrans %} + {% trans %}Previous{% endtrans %} {% else %} {% trans %}Previous{% endtrans %} {% endif %} @@ -18,11 +18,11 @@ TODO : rewrite the pagination used in this template an Alpine one {% if page_obj.number == i %} {{ i }} ({% trans %}current{% endtrans %}) {% else %} - {{ i }} + {{ i }} {% endif %} {% endfor %} {% if page_obj.has_next() %} - {% trans %}Next{% endtrans %} + {% trans %}Next{% endtrans %} {% else %} {% trans %}Next{% endtrans %} {% endif %} @@ -81,6 +81,10 @@ TODO : rewrite the pagination used in this template an Alpine one {% endfor %} + {{ paginate(paginated_result, paginator) }} +{% endblock %} + +{% block script %} - {{ paginate(paginated_result, paginator, "formPagination(this)") }} {% endblock %} diff --git a/club/views.py b/club/views.py index 02e87ba7..103eecb4 100644 --- a/club/views.py +++ b/club/views.py @@ -30,7 +30,7 @@ from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.messages.views import SuccessMessageMixin from django.core.exceptions import NON_FIELD_ERRORS, PermissionDenied, ValidationError from django.core.paginator import InvalidPage, Paginator -from django.db.models import Q, Sum +from django.db.models import F, Q, Sum from django.http import Http404, HttpResponseRedirect, StreamingHttpResponse from django.shortcuts import get_object_or_404, redirect from django.urls import reverse, reverse_lazy @@ -370,7 +370,7 @@ class ClubOldMembersView(ClubTabsMixin, PermissionRequiredMixin, DetailView): class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView): - """Sellings of a club.""" + """Sales of a club.""" model = Club pk_url_kwarg = "club_id" @@ -396,9 +396,8 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView): def get_context_data(self, **kwargs): kwargs = super().get_context_data(**kwargs) - qs = Selling.objects.filter(club=self.object) - kwargs["result"] = qs[:0] + kwargs["result"] = Selling.objects.none() kwargs["paginated_result"] = kwargs["result"] kwargs["total"] = 0 kwargs["total_quantity"] = 0 @@ -406,6 +405,7 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView): form = self.get_form() if form.is_valid(): + qs = Selling.objects.filter(club=self.object) if not len([v for v in form.cleaned_data.values() if v is not None]): qs = Selling.objects.none() if form.cleaned_data["begin_date"]: @@ -425,18 +425,18 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView): if len(selected_products) > 0: qs = qs.filter(product__in=selected_products) + kwargs["total"] = qs.annotate( + price=F("quantity") * F("unit_price") + ).aggregate(total=Sum("price", default=0))["total"] kwargs["result"] = qs.select_related( "counter", "counter__club", "customer", "customer__user", "seller" ).order_by("-id") - kwargs["total"] = sum([s.quantity * s.unit_price for s in kwargs["result"]]) - total_quantity = qs.all().aggregate(Sum("quantity")) - if total_quantity["quantity__sum"]: - kwargs["total_quantity"] = total_quantity["quantity__sum"] - benefit = ( - qs.exclude(product=None).all().aggregate(Sum("product__purchase_price")) - ) - if benefit["product__purchase_price__sum"]: - kwargs["benefit"] = benefit["product__purchase_price__sum"] + kwargs["total_quantity"] = qs.aggregate(total=Sum("quantity", default=0))[ + "total" + ] + kwargs["benefit"] = qs.exclude(product=None).aggregate( + res=Sum("product__purchase_price", default=0) + )["res"] kwargs["paginator"] = Paginator(kwargs["result"], self.paginate_by) try: diff --git a/core/templates/core/macros.jinja b/core/templates/core/macros.jinja index 26f2ca17..990dd184 100644 --- a/core/templates/core/macros.jinja +++ b/core/templates/core/macros.jinja @@ -153,7 +153,7 @@ current_page (django.core.paginator.Page): the current page object paginator (django.core.paginator.Paginator): the paginator object #} - {{ paginate_server_side(current_page, paginator, False) }} + {{ paginate_server_side(current_page, paginator, False) }} {% endmacro %} {% macro paginate_htmx(current_page, paginator) %} @@ -168,7 +168,7 @@ current_page (django.core.paginator.Page): the current page object paginator (django.core.paginator.Paginator): the paginator object #} - {{ paginate_server_side(current_page, paginator, True) }} + {{ paginate_server_side(current_page, paginator, True) }} {% endmacro %} {% macro paginate_server_side(current_page, paginator, use_htmx) %} diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index 3c790c88..cbf564b3 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2025-10-17 13:41+0200\n" +"POT-Creation-Date: 2025-11-04 12:43+0100\n" "PO-Revision-Date: 2016-07-18\n" "Last-Translator: Maréchal \n" @@ -893,7 +893,8 @@ msgstr "Administration des mailing listes" msgid "Actions" msgstr "Actions" -#: com/templates/com/mailing_admin.jinja core/templates/core/file_detail.jinja +#: com/templates/com/mailing_admin.jinja com/templates/com/poster_list.jinja +#: core/templates/core/file_detail.jinja #: core/templates/core/file_moderation.jinja sas/templates/sas/moderation.jinja #: sas/templates/sas/picture.jinja msgid "Moderate" @@ -1109,8 +1110,7 @@ msgstr "Vous n'avez pas accès à ce contenu" msgid "Poster" msgstr "Affiche" -#: com/templates/com/poster_edit.jinja com/templates/com/poster_moderate.jinja -#: com/templates/com/screen_edit.jinja +#: com/templates/com/poster_edit.jinja com/templates/com/screen_edit.jinja msgid "List" msgstr "Liste" @@ -1123,25 +1123,13 @@ msgstr "Affiche - modifier" msgid "Create" msgstr "Créer" -#: com/templates/com/poster_list.jinja -msgid "Moderation" -msgstr "Modération" - -#: com/templates/com/poster_list.jinja -msgid "No posters" -msgstr "Aucune affiche" - #: com/templates/com/poster_list.jinja com/templates/com/screen_slideshow.jinja msgid "Click to expand" msgstr "Cliquez pour agrandir" -#: com/templates/com/poster_moderate.jinja -msgid "Posters - moderation" -msgstr "Affiches - modération" - -#: com/templates/com/poster_moderate.jinja -msgid "No objects" -msgstr "Aucun éléments" +#: com/templates/com/poster_list.jinja +msgid "No posters" +msgstr "Aucune affiche" #: com/templates/com/screen_edit.jinja msgid "Screen"