mirror of
https://github.com/ae-utbm/sith.git
synced 2025-06-27 05:35:16 +00:00
improved Club related queries
formatted migration file
This commit is contained in:
parent
9fe4cc5102
commit
6608dac4d3
@ -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
|
import django.db.models.deletion
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
@ -25,7 +25,7 @@ class Migration(migrations.Migration):
|
|||||||
verbose_name="ID",
|
verbose_name="ID",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
("validated", models.BooleanField(verbose_name="is validated")),
|
("is_validated", models.BooleanField(verbose_name="is validated")),
|
||||||
(
|
(
|
||||||
"month",
|
"month",
|
||||||
counter.models.MonthField(
|
counter.models.MonthField(
|
||||||
|
@ -15,7 +15,8 @@
|
|||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
from datetime import timezone as tz
|
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.utils import timezone
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
@ -82,14 +83,12 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
|||||||
.order_by("-selling_sum")
|
.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"]]
|
club_names = [i["club__name"] for i in kwargs["sums"]]
|
||||||
clubs = Club.objects.filter(name__in=club_names)
|
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_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
|
kwargs["validated"] = invoice_statuses
|
||||||
return kwargs
|
return kwargs
|
||||||
@ -98,44 +97,26 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView):
|
|||||||
month_str = request.POST.get("month")
|
month_str = request.POST.get("month")
|
||||||
if not month_str:
|
if not month_str:
|
||||||
return self.get(request, *args, **kwargs)
|
return self.get(request, *args, **kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
start_date = datetime.strptime(month_str, "%Y-%m")
|
start_date = datetime.strptime(month_str, "%Y-%m")
|
||||||
start_date = date(start_date.year, start_date.month, 1)
|
start_date = date(start_date.year, start_date.month, 1)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
from django.shortcuts import redirect
|
|
||||||
|
|
||||||
return redirect(request.path)
|
return redirect(request.path)
|
||||||
|
|
||||||
club_names = list(
|
selling_subquery = Selling.objects.filter(
|
||||||
Selling.objects.filter(
|
club=OuterRef("pk"),
|
||||||
date__year=start_date.year, date__month=start_date.month
|
date__year=start_date.year,
|
||||||
)
|
date__month=start_date.month,
|
||||||
.values_list("club__name", flat=True)
|
|
||||||
.distinct()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
clubs = Club.objects.filter(name__in=club_names)
|
clubs = Club.objects.annotate(has_selling=Exists(selling_subquery)).filter(
|
||||||
club_map = {club.name: club for club in clubs}
|
has_selling=True
|
||||||
|
)
|
||||||
|
|
||||||
invoice_calls = InvoiceCall.objects.filter(month=month_str, club__in=clubs)
|
for club in clubs:
|
||||||
invoice_statuses = {ic.club.name: ic for ic in invoice_calls}
|
is_checked = f"validate_{club.name}" in request.POST
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
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', '')}")
|
return redirect(f"{request.path}?month={request.POST.get('month', '')}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user