From 1f77f154ba9385629cdb4775ff72d18fa48a8f44 Mon Sep 17 00:00:00 2001 From: Kenneth SOARES Date: Sat, 14 Jun 2025 15:52:39 +0200 Subject: [PATCH] MonthField for InvoiceCall --- counter/migrations/0032_invoicecall.py | 2 +- counter/models.py | 36 +++++++++++++++++--------- counter/views/invoice.py | 32 +++++++++++++---------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/counter/migrations/0032_invoicecall.py b/counter/migrations/0032_invoicecall.py index a51e8b27..47cf079d 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-11 12:53 +# Generated by Django 5.2 on 2025-06-14 13:36 import django.db.models.deletion from django.db import migrations, models diff --git a/counter/models.py b/counter/models.py index 89681e62..d8d29a12 100644 --- a/counter/models.py +++ b/counter/models.py @@ -1364,7 +1364,7 @@ class ReturnableProductBalance(models.Model): ) -class MonthField(models.DateField): +class MonthField(models.CharField): description = _("Year + month field") def __init__(self, *args, **kwargs): @@ -1377,21 +1377,33 @@ class MonthField(models.DateField): def from_db_value(self, value, expression, connection): if value is None: return value - year, month = map(value.split("-")) - return date(year, month, 1) + try: + year, month = map(int, value.split("-")) + return date(year, month, 1) + except (ValueError, TypeError): + return value + + def to_python(self, value): + if isinstance(value, date): + return value + if isinstance(value, str): + try: + year, month = map(int, value.split("-")) + return date(year, month, 1) + except ValueError: + pass + return value def get_prep_value(self, value): if isinstance(value, date): return value.strftime("%Y-%m") - elif isinstance(value, str): - try: - datetime.strptime(value, "%Y-%m") - except ValueError: - raise ValueError("invalid format for date (use YYYY-mm)") from None - elif value is None: + if isinstance(value, str) and len(value) == 7 and value[4] == "-": return value - else: - raise TypeError("Invalid type for MonthField") + return value + + def value_to_string(self, obj): + value = self.value_from_object(obj) + return self.get_prep_value(value) class InvoiceCall(models.Model): @@ -1404,4 +1416,4 @@ class InvoiceCall(models.Model): verbose_name_plural = _("Invoice calls") def __str__(self): - return f"invoice call of {self.month}/{self.year} made by {self.club}" + return f"invoice call of {self.month} made by {self.club}" diff --git a/counter/views/invoice.py b/counter/views/invoice.py index 6adce714..e6570506 100644 --- a/counter/views/invoice.py +++ b/counter/views/invoice.py @@ -32,7 +32,9 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): """Add sums to the context.""" kwargs = super().get_context_data(**kwargs) kwargs["months"] = Selling.objects.datetimes("date", "month", order="DESC") - if "month" in self.request.GET: + month_str = self.request.GET.get("month") + + if month_str: start_date = datetime.strptime(self.request.GET["month"], "%Y-%m") else: start_date = datetime( @@ -85,9 +87,7 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): clubs = Club.objects.filter(name__in=club_names) # et une query pour les factures - invoice_calls = InvoiceCall.objects.filter( - month=date(start_date.year, start_date.month, 1), 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} @@ -95,14 +95,22 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): return kwargs def post(self, request, *args, **kwargs): - if request.POST["month"]: - start_date = datetime.strptime(request.POST["month"], "%Y-%m") + month_str = request.POST.get("month") + if not month_str: + return self.get(request, *args, **kwargs) - year = start_date.year - month = start_date.month + 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=year, date__month=month) + Selling.objects.filter( + date__year=start_date.year, date__month=start_date.month + ) .values_list("club__name", flat=True) .distinct() ) @@ -110,9 +118,7 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): clubs = Club.objects.filter(name__in=club_names) club_map = {club.name: club for club in clubs} - invoice_calls = InvoiceCall.objects.filter( - month=date(year, month, 1), club__in=clubs - ) + 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: @@ -125,7 +131,7 @@ class InvoiceCallView(CounterAdminTabsMixin, CounterAdminMixin, TemplateView): invoice_call.save() else: InvoiceCall.objects.create( - month=date(year, month, 1), + month=month_str, club=club_map[club_name], validated=is_checked, )