mirror of
https://gitlab.crans.org/bde/nk20
synced 2025-04-23 23:22:38 +00:00
287 lines
9.4 KiB
Python
287 lines
9.4 KiB
Python
# Copyright (C) 2018-2025 by BDE ENS Paris-Saclay
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from datetime import timedelta
|
|
|
|
from django.db import models, transaction
|
|
from django.utils import timezone
|
|
from django.utils.translation import gettext_lazy as _
|
|
from polymorphic.models import PolymorphicModel
|
|
from member.models import Club
|
|
|
|
|
|
class Allergen(models.Model):
|
|
"""
|
|
Allergen and alimentary restrictions
|
|
"""
|
|
name = models.CharField(
|
|
verbose_name=_('name'),
|
|
max_length=255,
|
|
)
|
|
|
|
class Meta:
|
|
verbose_name = _("Allergen")
|
|
verbose_name_plural = _("Allergens")
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
|
|
class Food(PolymorphicModel):
|
|
"""
|
|
Describe any type of food
|
|
"""
|
|
name = models.CharField(
|
|
verbose_name=_("name"),
|
|
max_length=255,
|
|
)
|
|
|
|
owner = models.ForeignKey(
|
|
Club,
|
|
on_delete=models.PROTECT,
|
|
related_name='+',
|
|
verbose_name=_('owner'),
|
|
)
|
|
|
|
allergens = models.ManyToManyField(
|
|
Allergen,
|
|
blank=True,
|
|
verbose_name=_('allergens'),
|
|
)
|
|
|
|
expiry_date = models.DateTimeField(
|
|
verbose_name=_('expiry date'),
|
|
null=False,
|
|
)
|
|
|
|
end_of_life = models.CharField(
|
|
blank=True,
|
|
verbose_name=_('end of life'),
|
|
max_length=255,
|
|
)
|
|
|
|
is_ready = models.BooleanField(
|
|
verbose_name=_('is ready'),
|
|
max_length=255,
|
|
)
|
|
|
|
order = models.CharField(
|
|
blank=True,
|
|
verbose_name=_('order'),
|
|
max_length=255,
|
|
)
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
@transaction.atomic
|
|
def update_allergens(self):
|
|
# update parents
|
|
for parent in self.transformed_ingredient_inv.iterator():
|
|
old_allergens = list(parent.allergens.all()).copy()
|
|
parent.allergens.clear()
|
|
for child in parent.ingredients.iterator():
|
|
if child.pk != self.pk:
|
|
parent.allergens.set(parent.allergens.union(child.allergens.all()))
|
|
parent.allergens.set(parent.allergens.union(self.allergens.all()))
|
|
if old_allergens != list(parent.allergens.all()):
|
|
parent.save(old_allergens=old_allergens)
|
|
|
|
def update_expiry_date(self):
|
|
# update parents
|
|
for parent in self.transformed_ingredient_inv.iterator():
|
|
old_expiry_date = parent.expiry_date
|
|
parent.expiry_date = parent.shelf_life + parent.creation_date
|
|
for child in parent.ingredients.iterator():
|
|
if (child.pk != self.pk
|
|
and not (child.polymorphic_ctype.model == 'basicfood'
|
|
and child.date_type == 'DDM')):
|
|
parent.expiry_date = min(parent.expiry_date, child.expiry_date)
|
|
|
|
if self.polymorphic_ctype.model == 'basicfood' and self.date_type == 'DLC':
|
|
parent.expiry_date = min(parent.expiry_date, self.expiry_date)
|
|
if old_expiry_date != parent.expiry_date:
|
|
parent.save()
|
|
|
|
class Meta:
|
|
verbose_name = _('Food')
|
|
verbose_name_plural = _('Foods')
|
|
|
|
|
|
class BasicFood(Food):
|
|
"""
|
|
A basic food is a food directly buy and stored
|
|
"""
|
|
arrival_date = models.DateTimeField(
|
|
default=timezone.now,
|
|
verbose_name=_('arrival date'),
|
|
)
|
|
|
|
date_type = models.CharField(
|
|
max_length=255,
|
|
choices=(
|
|
("DLC", "DLC"),
|
|
("DDM", "DDM"),
|
|
)
|
|
)
|
|
|
|
@transaction.atomic
|
|
def save(self, force_insert=False, force_update=False, using=None, update_fields=None, **kwargs):
|
|
created = self.pk is None
|
|
if not created:
|
|
# Check if important fields are updated
|
|
old_food = Food.objects.select_for_update().get(pk=self.pk)
|
|
if not hasattr(self, "_force_save"):
|
|
# Allergens
|
|
|
|
if ('old_allergens' in kwargs
|
|
and list(self.allergens.all()) != kwargs['old_allergens']):
|
|
self.update_allergens()
|
|
|
|
# Expiry date
|
|
if ((self.expiry_date != old_food.expiry_date
|
|
and self.date_type == 'DLC')
|
|
or old_food.date_type != self.date_type):
|
|
self.update_expiry_date()
|
|
|
|
return super().save(force_insert, force_update, using, update_fields)
|
|
|
|
@staticmethod
|
|
def get_lastests_objects(number, distinct_field, order_by_field):
|
|
"""
|
|
Get the last object with distinct field and ranked with order_by
|
|
This methods exist because we can't distinct with one field and
|
|
order with another
|
|
"""
|
|
foods = BasicFood.objects.order_by(order_by_field).all()
|
|
field = []
|
|
for food in foods:
|
|
if getattr(food, distinct_field) in field:
|
|
continue
|
|
else:
|
|
field.append(getattr(food, distinct_field))
|
|
number -= 1
|
|
yield food
|
|
if not number:
|
|
return
|
|
|
|
class Meta:
|
|
verbose_name = _('Basic food')
|
|
verbose_name_plural = _('Basic foods')
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
|
|
class TransformedFood(Food):
|
|
"""
|
|
A transformed food is a food with ingredients
|
|
"""
|
|
creation_date = models.DateTimeField(
|
|
default=timezone.now,
|
|
verbose_name=_('creation date'),
|
|
)
|
|
|
|
# Without microbiological analyzes, the storage time is 3 days
|
|
shelf_life = models.DurationField(
|
|
default=timedelta(days=3),
|
|
verbose_name=_('shelf life'),
|
|
)
|
|
|
|
ingredients = models.ManyToManyField(
|
|
Food,
|
|
blank=True,
|
|
symmetrical=False,
|
|
related_name='transformed_ingredient_inv',
|
|
verbose_name=_('transformed ingredient'),
|
|
)
|
|
|
|
def check_cycle(self, ingredients, origin, checked):
|
|
for ingredient in ingredients:
|
|
if ingredient == origin:
|
|
# We break the cycle
|
|
self.ingredients.remove(ingredient)
|
|
if ingredient.polymorphic_ctype.model == 'transformedfood' and ingredient not in checked:
|
|
ingredient.check_cycle(ingredient.ingredients.all(), origin, checked)
|
|
checked.append(ingredient)
|
|
|
|
@transaction.atomic
|
|
def save(self, force_insert=False, force_update=False, using=None, update_fields=None, **kwargs):
|
|
created = self.pk is None
|
|
if not created:
|
|
# Check if important fields are updated
|
|
update = {'allergens': False, 'expiry_date': False}
|
|
old_food = Food.objects.select_for_update().get(pk=self.pk)
|
|
if not hasattr(self, "_force_save"):
|
|
# Allergens
|
|
# Unfortunately with the many-to-many relation we can't access
|
|
# to old allergens
|
|
if ('old_allergens' in kwargs
|
|
and list(self.allergens.all()) != kwargs['old_allergens']):
|
|
update['allergens'] = True
|
|
|
|
# Expiry date
|
|
update['expiry_date'] = (self.shelf_life != old_food.shelf_life
|
|
or self.creation_date != old_food.creation_date)
|
|
if update['expiry_date']:
|
|
self.expiry_date = self.creation_date + self.shelf_life
|
|
# Unfortunately with the set method ingredients are already save,
|
|
# we check cycle after if possible
|
|
if ('old_ingredients' in kwargs
|
|
and list(self.ingredients.all()) != list(kwargs['old_ingredients'])):
|
|
update['allergens'] = True
|
|
update['expiry_date'] = True
|
|
|
|
# it's preferable to keep a queryset but we allow list too
|
|
if type(kwargs['old_ingredients']) is list:
|
|
kwargs['old_ingredients'] = Food.objects.filter(
|
|
pk__in=[food.pk for food in kwargs['old_ingredients']])
|
|
self.check_cycle(self.ingredients.all().difference(kwargs['old_ingredients']), self, [])
|
|
if update['allergens']:
|
|
self.update_allergens()
|
|
if update['expiry_date']:
|
|
self.update_expiry_date()
|
|
|
|
if created:
|
|
self.expiry_date = self.shelf_life + self.creation_date
|
|
|
|
# We save here because we need pk for many-to-many relation
|
|
super().save(force_insert, force_update, using, update_fields)
|
|
|
|
for child in self.ingredients.iterator():
|
|
self.allergens.set(self.allergens.union(child.allergens.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, force_update, using, update_fields)
|
|
|
|
class Meta:
|
|
verbose_name = _('Transformed food')
|
|
verbose_name_plural = _('Transformed foods')
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
|
|
class QRCode(models.Model):
|
|
"""
|
|
QR-code for register food
|
|
"""
|
|
qr_code_number = models.PositiveIntegerField(
|
|
unique=True,
|
|
verbose_name=_('qr code number'),
|
|
)
|
|
|
|
food_container = models.ForeignKey(
|
|
Food,
|
|
on_delete=models.CASCADE,
|
|
related_name='QR_code',
|
|
verbose_name=_('food container'),
|
|
)
|
|
|
|
class Meta:
|
|
verbose_name = _('QR-code')
|
|
verbose_name_plural = _('QR-codes')
|
|
|
|
def __str__(self):
|
|
return _('QR-code number') + ' ' + str(self.qr_code_number)
|