2024-05-17 18:46:38 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-07-06 17:26:21 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
from django.db import models, transaction
|
2024-05-17 18:46:38 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from member.models import Club
|
2024-07-03 17:20:01 +00:00
|
|
|
from polymorphic.models import PolymorphicModel
|
2024-05-17 18:46:38 +00:00
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
|
|
|
|
class QRCode(models.Model):
|
2024-05-17 18:46:38 +00:00
|
|
|
"""
|
2024-07-03 17:20:01 +00:00
|
|
|
An QRCode model
|
2024-05-17 18:46:38 +00:00
|
|
|
"""
|
|
|
|
qr_code_number = models.PositiveIntegerField(
|
2024-05-21 12:07:35 +00:00
|
|
|
verbose_name=_("QR-code number"),
|
2024-07-05 09:57:44 +00:00
|
|
|
unique=True,
|
2024-05-21 12:07:35 +00:00
|
|
|
)
|
|
|
|
|
2024-07-05 09:57:44 +00:00
|
|
|
food_container = models.OneToOneField(
|
2024-07-03 17:20:01 +00:00
|
|
|
'Food',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='QR_code',
|
|
|
|
verbose_name=_('food container'),
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("QR-code")
|
|
|
|
verbose_name_plural = _("QR-codes")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return _("QR-code number {qr_code_number}").format(qr_code_number=self.qr_code_number)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
|
2024-05-17 18:46:38 +00:00
|
|
|
class Allergen(models.Model):
|
|
|
|
"""
|
|
|
|
A list of allergen and alimentary restrictions
|
|
|
|
"""
|
2024-07-03 17:20:01 +00:00
|
|
|
name = models.CharField(
|
|
|
|
verbose_name=_('name'),
|
|
|
|
max_length=255,
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Allergen')
|
|
|
|
verbose_name_plural = _('Allergens')
|
2024-05-17 18:46:38 +00:00
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2024-05-17 18:46:38 +00:00
|
|
|
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
class Food(PolymorphicModel):
|
|
|
|
name = models.CharField(
|
|
|
|
verbose_name=_('name'),
|
|
|
|
max_length=255,
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
owner = models.ForeignKey(
|
|
|
|
Club,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name='+',
|
|
|
|
verbose_name=_('owner'),
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
allergens = models.ManyToManyField(
|
|
|
|
Allergen,
|
|
|
|
blank=True,
|
|
|
|
verbose_name=_('allergen'),
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
expiry_date = models.DateTimeField(
|
|
|
|
verbose_name=_('expiry date'),
|
2024-07-05 09:57:44 +00:00
|
|
|
null=False,
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
was_eaten = models.BooleanField(
|
|
|
|
default=False,
|
|
|
|
verbose_name=_('was eaten'),
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-05 09:57:44 +00:00
|
|
|
is_ready = models.BooleanField(
|
|
|
|
default=False,
|
|
|
|
verbose_name=_('is ready'),
|
|
|
|
)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2024-05-21 12:07:35 +00:00
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
|
|
|
|
return super().save(force_insert, force_update, using, update_fields)
|
2024-05-21 12:07:35 +00:00
|
|
|
|
2024-05-17 18:46:38 +00:00
|
|
|
class Meta:
|
2024-07-03 17:20:01 +00:00
|
|
|
verbose_name = _('food')
|
|
|
|
verbose_name = _('foods')
|
2024-05-17 18:46:38 +00:00
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
class BasicFood(Food):
|
2024-05-17 18:46:38 +00:00
|
|
|
"""
|
|
|
|
Food which has been directly buy on supermarket
|
|
|
|
"""
|
2024-07-03 17:20:01 +00:00
|
|
|
date_type = models.CharField(
|
2024-05-17 18:46:38 +00:00
|
|
|
max_length=255,
|
2024-07-03 17:20:01 +00:00
|
|
|
choices=(
|
|
|
|
("DLC", "DLC"),
|
|
|
|
("DDM", "DDM"),
|
|
|
|
)
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
2024-05-25 20:34:59 +00:00
|
|
|
|
|
|
|
arrival_date = models.DateTimeField(
|
|
|
|
verbose_name=_('arrival date'),
|
|
|
|
default=timezone.now,
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
# label = models.ImageField(
|
|
|
|
# verbose_name=_('food label'),
|
|
|
|
# max_length=255,
|
|
|
|
# blank=False,
|
|
|
|
# null=False,
|
|
|
|
# upload_to='label/',
|
|
|
|
# )
|
2024-05-21 12:07:35 +00:00
|
|
|
|
2024-07-06 17:26:21 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def update_allergens(self):
|
|
|
|
# update parents
|
|
|
|
for parent in self.transformed_ingredient_inv.iterator():
|
|
|
|
parent.update_allergens()
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def update_expiry_date(self):
|
|
|
|
# update parents
|
|
|
|
for parent in self.transformed_ingredient_inv.iterator():
|
|
|
|
parent.update_expiry_date()
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def update(self):
|
|
|
|
self.update_allergens()
|
|
|
|
self.update_expiry_date()
|
|
|
|
|
2024-05-17 18:46:38 +00:00
|
|
|
class Meta:
|
2024-07-03 17:20:01 +00:00
|
|
|
verbose_name = _('Basic food')
|
|
|
|
verbose_name_plural = _('Basic foods')
|
2024-05-17 18:46:38 +00:00
|
|
|
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
class TransformedFood(Food):
|
2024-05-17 18:46:38 +00:00
|
|
|
"""
|
|
|
|
Transformed food are a mix between basic food and meal
|
|
|
|
"""
|
|
|
|
creation_date = models.DateTimeField(
|
2024-07-03 17:20:01 +00:00
|
|
|
verbose_name=_('creation date'),
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 17:20:01 +00:00
|
|
|
ingredient = models.ManyToManyField(
|
|
|
|
Food,
|
|
|
|
blank=True,
|
|
|
|
symmetrical=False,
|
|
|
|
related_name='transformed_ingredient_inv',
|
|
|
|
verbose_name=_('transformed ingredient'),
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
is_active = models.BooleanField(
|
2024-07-03 17:20:01 +00:00
|
|
|
default=True,
|
|
|
|
verbose_name=_('is active'),
|
2024-05-17 18:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-07-07 19:25:26 +00:00
|
|
|
# Without microbiological analyzes, the storage time is 3 days
|
|
|
|
shelf_life = models.DurationField(
|
|
|
|
verbose_name=_("shelf life"),
|
|
|
|
default=timedelta(days=3),
|
|
|
|
)
|
|
|
|
|
2024-07-06 17:26:21 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def update_allergens(self):
|
|
|
|
# When allergens are changed, simply update the parents' allergens
|
|
|
|
old_allergens = list(self.allergens.all())
|
|
|
|
self.allergens.clear()
|
|
|
|
for ingredient in self.ingredient.iterator():
|
|
|
|
self.allergens.set(self.allergens.union(ingredient.allergens.all()))
|
|
|
|
|
|
|
|
if old_allergens == list(self.allergens.all()):
|
|
|
|
return
|
|
|
|
super().save()
|
|
|
|
|
|
|
|
# update parents
|
|
|
|
for parent in self.transformed_ingredient_inv.iterator():
|
|
|
|
parent.update_allergens()
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def update_expiry_date(self):
|
|
|
|
# When expiry_date is changed, simply update the parents' expiry_date
|
|
|
|
old_expiry_date = self.expiry_date
|
2024-07-07 19:25:26 +00:00
|
|
|
self.expiry_date = self.creation_date + self.shelf_life
|
2024-07-06 17:26:21 +00:00
|
|
|
for ingredient in self.ingredient.iterator():
|
|
|
|
self.expiry_date = min(self.expiry_date, ingredient.expiry_date)
|
|
|
|
|
|
|
|
if old_expiry_date == self.expiry_date:
|
|
|
|
return
|
|
|
|
super().save()
|
|
|
|
|
|
|
|
# update parents
|
|
|
|
for parent in self.transformed_ingredient_inv.iterator():
|
|
|
|
parent.update_expiry_date()
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def update(self):
|
|
|
|
self.update_allergens()
|
|
|
|
self.update_expiry_date()
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2024-05-17 18:46:38 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Transformed food')
|
|
|
|
verbose_name_plural = _('Transformed foods')
|