change MonthField from CharField to DateField

This commit is contained in:
Kenneth SOARES
2025-09-09 12:35:10 +02:00
parent c7231608a9
commit 0fef2e0071
3 changed files with 30 additions and 43 deletions

View File

@@ -378,19 +378,21 @@ BasketForm = forms.formset_factory(
class InvoiceCallForm(forms.Form): 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) super().__init__(*args, **kwargs)
self.month = month self.month = month
self.clubs = clubs self.clubs = clubs
if self.clubs is None: if self.clubs is None:
self.clubs = [] self.clubs = []
invoices = { invoices = {
i["club_id"]: i["is_validated"] i["club_id"]: i["is_validated"]
for i in InvoiceCall.objects.filter(club__in=self.clubs).values( for i in InvoiceCall.objects.filter(
"club_id", "is_validated" club__in=self.clubs, month=self.month
) ).values("club_id", "is_validated")
} }
for club in self.clubs: for club in self.clubs:
is_validated = invoices.get(club.id, False) is_validated = invoices.get(club.id, False)

View File

@@ -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 import django.db.models.deletion
from django.db import migrations, models from django.db import migrations, models
@@ -25,13 +25,11 @@ class Migration(migrations.Migration):
verbose_name="ID", verbose_name="ID",
), ),
), ),
("is_validated", models.BooleanField(verbose_name="is validated")),
( (
"month", "is_validated",
counter.models.MonthField( models.BooleanField(default=False, verbose_name="is validated"),
max_length=7, verbose_name="invoice date"
),
), ),
("month", counter.models.MonthField(verbose_name="invoice date")),
( (
"club", "club",
models.ForeignKey( models.ForeignKey(

View File

@@ -1364,46 +1364,33 @@ class ReturnableProductBalance(models.Model):
) )
class MonthField(models.CharField): class MonthField(models.DateField):
description = _("Year + month field") description = _("Year + month field (day forced to 1)")
default_error_messages = {
def __init__(self, *args, **kwargs): "invalid": _(
kwargs["max_length"] = 7 "%(value)s” value has an invalid date format. It must be "
super().__init__(*args, **kwargs) "in YYYY-MM format."
),
def db_type(self, connection): "invalid_date": _(
return "char(7)" "%(value)s” value has the correct format (YYYY-MM) "
"but it is an invalid date."
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
def to_python(self, value): def to_python(self, value):
if isinstance(value, date): if isinstance(value, date):
return value return value.replace(day=1)
if isinstance(value, str): if isinstance(value, str):
try: try:
year, month = value.split("-") year, month = map(int, value.split("-"))
return date(year, month, 1) return date(year, month, 1)
except ValueError: except (ValueError, TypeError) as err:
pass raise ValueError(
return value self.error_messages["invalid"] % {"value": value}
) from err
def get_prep_value(self, value): return super().to_python(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)
class InvoiceCall(models.Model): class InvoiceCall(models.Model):