1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-11-07 23:39:50 +01:00

Add field 'traces' for model Food

This commit is contained in:
Ehouarn
2025-11-02 18:43:33 +01:00
parent 4f016fed38
commit 48b1ef9ec8
5 changed files with 68 additions and 8 deletions

View File

@@ -53,6 +53,13 @@ class Food(PolymorphicModel):
verbose_name=_('allergens'),
)
traces = models.ManyToManyField(
Allergen,
blank=True,
verbose_name=_('traces'),
related_name='food_with_traces'
)
expiry_date = models.DateTimeField(
verbose_name=_('expiry date'),
null=False,
@@ -91,6 +98,19 @@ class Food(PolymorphicModel):
if old_allergens != list(parent.allergens.all()):
parent.save(old_allergens=old_allergens)
@transaction.atomic
def update_traces(self):
# update parents
for parent in self.transformed_ingredient_inv.iterator():
old_traces = list(parent.traces.all()).copy()
parent.traces.clear()
for child in parent.ingredients.iterator():
if child.pk != self.pk:
parent.traces.set(parent.traces.union(child.traces.all()))
parent.traces.set(parent.traces.union(self.traces.all()))
if old_traces != list(parent.traces.all()):
parent.save(old_traces=old_traces)
def update_expiry_date(self):
# update parents
for parent in self.transformed_ingredient_inv.iterator():
@@ -142,6 +162,10 @@ class BasicFood(Food):
and list(self.allergens.all()) != kwargs['old_allergens']):
self.update_allergens()
if ('old_traces' in kwargs
and list(self.traces.all()) != kwargs['old_traces']):
self.update_traces()
# Expiry date
if ((self.expiry_date != old_food.expiry_date
and self.date_type == 'DLC')
@@ -214,7 +238,7 @@ class TransformedFood(Food):
created = self.pk is None
if not created:
# Check if important fields are updated
update = {'allergens': False, 'expiry_date': False}
update = {'allergens': False, 'traces': False, 'expiry_date': False}
old_food = Food.objects.select_for_update().get(pk=self.pk)
if not hasattr(self, "_force_save"):
# Allergens
@@ -224,6 +248,10 @@ class TransformedFood(Food):
and list(self.allergens.all()) != kwargs['old_allergens']):
update['allergens'] = True
if ('old_traces' in kwargs
and list(self.traces.all()) != kwargs['old_traces']):
update['traces'] = True
# Expiry date
update['expiry_date'] = (self.shelf_life != old_food.shelf_life
or self.creation_date != old_food.creation_date)
@@ -234,6 +262,7 @@ class TransformedFood(Food):
if ('old_ingredients' in kwargs
and list(self.ingredients.all()) != list(kwargs['old_ingredients'])):
update['allergens'] = True
update['traces'] = True
update['expiry_date'] = True
# it's preferable to keep a queryset but we allow list too
@@ -243,6 +272,8 @@ class TransformedFood(Food):
self.check_cycle(self.ingredients.all().difference(kwargs['old_ingredients']), self, [])
if update['allergens']:
self.update_allergens()
if update['traces']:
self.update_traces()
if update['expiry_date']:
self.update_expiry_date()
@@ -254,6 +285,7 @@ class TransformedFood(Food):
for child in self.ingredients.iterator():
self.allergens.set(self.allergens.union(child.allergens.all()))
self.traces.set(self.traces.union(child.traces.all()))
if not (child.polymorphic_ctype.model == 'basicfood' and child.date_type == 'DDM'):
self.expiry_date = min(self.expiry_date, child.expiry_date)
return super().save(force_insert=False, force_update=force_update, using=using, update_fields=update_fields)