From 0fef2e007161d3e93046dee0960d81a7527cea5d Mon Sep 17 00:00:00 2001 From: Kenneth SOARES Date: Tue, 9 Sep 2025 12:35:10 +0200 Subject: [PATCH] change MonthField from CharField to DateField --- counter/forms.py | 10 +++-- counter/migrations/0032_invoicecall.py | 10 ++--- counter/models.py | 53 ++++++++++---------------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/counter/forms.py b/counter/forms.py index be955610..0207d92f 100644 --- a/counter/forms.py +++ b/counter/forms.py @@ -378,19 +378,21 @@ BasketForm = forms.formset_factory( class InvoiceCallForm(forms.Form): - def __init__(self, *args, month=None, clubs: list[Club] | None = None, **kwargs): + def __init__(self, *args, month, clubs: list[Club] | None = None, **kwargs): super().__init__(*args, **kwargs) self.month = month self.clubs = clubs if self.clubs is None: self.clubs = [] + invoices = { i["club_id"]: i["is_validated"] - for i in InvoiceCall.objects.filter(club__in=self.clubs).values( - "club_id", "is_validated" - ) + for i in InvoiceCall.objects.filter( + club__in=self.clubs, month=self.month + ).values("club_id", "is_validated") } + for club in self.clubs: is_validated = invoices.get(club.id, False) diff --git a/counter/migrations/0032_invoicecall.py b/counter/migrations/0032_invoicecall.py index 5c4e0f81..b02edf20 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 14:35 +# Generated by Django 5.2.3 on 2025-09-09 10:24 import django.db.models.deletion from django.db import migrations, models @@ -25,13 +25,11 @@ class Migration(migrations.Migration): verbose_name="ID", ), ), - ("is_validated", models.BooleanField(verbose_name="is validated")), ( - "month", - counter.models.MonthField( - max_length=7, verbose_name="invoice date" - ), + "is_validated", + models.BooleanField(default=False, verbose_name="is validated"), ), + ("month", counter.models.MonthField(verbose_name="invoice date")), ( "club", models.ForeignKey( diff --git a/counter/models.py b/counter/models.py index 88fe9639..72058ef6 100644 --- a/counter/models.py +++ b/counter/models.py @@ -1364,46 +1364,33 @@ class ReturnableProductBalance(models.Model): ) -class MonthField(models.CharField): - description = _("Year + month field") - - def __init__(self, *args, **kwargs): - kwargs["max_length"] = 7 - super().__init__(*args, **kwargs) - - def db_type(self, connection): - return "char(7)" - - def from_db_value(self, value, expression, connection): - if value is None: - return value - try: - year, month = value.split("-") - return date(year, month, 1) - except (ValueError, TypeError): - return value +class MonthField(models.DateField): + description = _("Year + month field (day forced to 1)") + default_error_messages = { + "invalid": _( + "ā€œ%(value)sā€ value has an invalid date format. It must be " + "in YYYY-MM format." + ), + "invalid_date": _( + "ā€œ%(value)sā€ value has the correct format (YYYY-MM) " + "but it is an invalid date." + ), + } def to_python(self, value): if isinstance(value, date): - return value + return value.replace(day=1) + if isinstance(value, str): try: - year, month = value.split("-") + year, month = map(int, value.split("-")) return date(year, month, 1) - except ValueError: - pass - return value + except (ValueError, TypeError) as err: + raise ValueError( + self.error_messages["invalid"] % {"value": value} + ) from err - def get_prep_value(self, value): - if isinstance(value, date): - return value.strftime("%Y-%m") - if isinstance(value, str) and len(value) == 7 and value[4] == "-": - return value - return value - - def value_to_string(self, obj): - value = self.value_from_object(obj) - return self.get_prep_value(value) + return super().to_python(value) class InvoiceCall(models.Model):