From 6608dac4d30d4436aaad90a6e1394c098b0b60c0 Mon Sep 17 00:00:00 2001 From: Kenneth SOARES Date: Sat, 14 Jun 2025 16:58:43 +0200 Subject: [PATCH] improved Club related queries formatted migration file --- counter/migrations/0032_invoicecall.py | 4 +-- counter/views/invoice.py | 49 ++++++++------------------ 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/counter/migrations/0032_invoicecall.py b/counter/migrations/0032_invoicecall.py index 47cf079d..5c4e0f81 100644 --- a/counter/migrations/0032_invoicecall.py +++ b/counter/migrations/0032_invoicecall.py @@ -1,4 +1,4 @@ -# Generated by Django 5.2 on 2025-06-14 13:36 +# Generated by Django 5.2 on 2025-06-14 14:35 import django.db.models.deletion from django.db import migrations, models @@ -25,7 +25,7 @@ class Migration(migrations.Migration): verbose_name="ID", ), ), - ("validated", models.BooleanField(verbose_name="is validated")), + ("is_validated", models.BooleanField(verbose_name="is validated")), ( "month", counter.models.MonthField( diff --git a/counter/views/invoice.py b/counter/views/invoice.py index e6570506..55c96b38 100644 --- a/counter/views/invoice.py +++ b/counter/views/invoice.py @@ -15,7 +15,8 @@ from datetime import date, datetime, timedelta from datetime import timezone as tz -from django.db.models import F +from django.db.models import Exists, F, OuterRef +from django.shortcuts import redirect from django.utils import timezone from django.views.generic import TemplateView @@ -82,14 +83,12 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): .order_by("-selling_sum") ) - # une query pour tous les clubs qu'on met dans un dico dont la clé est le nom du club club_names = [i["club__name"] for i in kwargs["sums"]] clubs = Club.objects.filter(name__in=club_names) - # et une query pour les factures invoice_calls = InvoiceCall.objects.filter(month=month_str, club__in=clubs) - invoice_statuses = {ic.club.name: ic.validated for ic in invoice_calls} + invoice_statuses = {ic.club.name: ic.is_validated for ic in invoice_calls} kwargs["validated"] = invoice_statuses return kwargs @@ -98,44 +97,26 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): month_str = request.POST.get("month") if not month_str: return self.get(request, *args, **kwargs) - try: start_date = datetime.strptime(month_str, "%Y-%m") start_date = date(start_date.year, start_date.month, 1) except ValueError: - from django.shortcuts import redirect - return redirect(request.path) - club_names = list( - Selling.objects.filter( - date__year=start_date.year, date__month=start_date.month - ) - .values_list("club__name", flat=True) - .distinct() + selling_subquery = Selling.objects.filter( + club=OuterRef("pk"), + date__year=start_date.year, + date__month=start_date.month, ) - clubs = Club.objects.filter(name__in=club_names) - club_map = {club.name: club for club in clubs} + clubs = Club.objects.annotate(has_selling=Exists(selling_subquery)).filter( + has_selling=True + ) - invoice_calls = InvoiceCall.objects.filter(month=month_str, club__in=clubs) - invoice_statuses = {ic.club.name: ic for ic in invoice_calls} - - for club_name in club_names: - is_checked = f"validate_{club_name}" in request.POST - invoice_call = invoice_statuses.get(club_name) - - if invoice_call: - if invoice_call.validated != is_checked: - invoice_call.validated = is_checked - invoice_call.save() - else: - InvoiceCall.objects.create( - month=month_str, - club=club_map[club_name], - validated=is_checked, - ) - - from django.shortcuts import redirect + for club in clubs: + is_checked = f"validate_{club.name}" in request.POST + InvoiceCall.objects.update_or_create( + month=month_str, club=club, defaults={"is_validated": is_checked} + ) return redirect(f"{request.path}?month={request.POST.get('month', '')}")