mirror of
https://github.com/ae-utbm/sith.git
synced 2025-09-13 19:45:50 +00:00
change MonthField from CharField to DateField
This commit is contained in:
@@ -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)
|
||||
|
||||
|
@@ -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(
|
||||
|
@@ -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):
|
||||
|
Reference in New Issue
Block a user