diff --git a/apps/food/admin.py b/apps/food/admin.py index 2bb1e302..23384316 100644 --- a/apps/food/admin.py +++ b/apps/food/admin.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.contrib import admin +from django.db import transaction from note_kfet.admin import admin_site from .models import Allergen, BasicFood, QRCode, TransformedFood @@ -9,27 +10,29 @@ from .models import Allergen, BasicFood, QRCode, TransformedFood @admin.register(QRCode, site=admin_site) class QRCodeAdmin(admin.ModelAdmin): - """ - TEMPORARY - """ + pass @admin.register(BasicFood, site=admin_site) class BasicFoodAdmin(admin.ModelAdmin): - """ - TEMPORARY - """ + @transaction.atomic + def save_related(self, *args, **kwargs): + ans = super().save_related(*args, **kwargs) + args[1].instance.update() + return ans @admin.register(TransformedFood, site=admin_site) class TransformedFoodAdmin(admin.ModelAdmin): - """ - TEMPORARY - """ + exclude = ["allergens", "expiry_date"] + + @transaction.atomic + def save_related(self, *args, **kwargs): + ans = super().save_related(*args, **kwargs) + args[1].instance.update() + return ans @admin.register(Allergen, site=admin_site) class AllergenAdmin(admin.ModelAdmin): - """ - TEMPORARY - """ + pass diff --git a/apps/food/forms.py b/apps/food/forms.py index 34a4f5f0..fe0775a0 100644 --- a/apps/food/forms.py +++ b/apps/food/forms.py @@ -89,7 +89,7 @@ class TransformedFoodForms(forms.ModelForm): class Meta: model = TransformedFood - fields = ('name', 'creation_date', 'owner', 'is_active', 'allergens') + fields = ('name', 'creation_date', 'owner', 'is_active') widgets = { "owner": Autocomplete( model=Club, diff --git a/apps/food/models.py b/apps/food/models.py index bc5103b5..e68a5c65 100644 --- a/apps/food/models.py +++ b/apps/food/models.py @@ -1,6 +1,8 @@ # Copyright (C) 2018-2024 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 _ @@ -120,6 +122,23 @@ class BasicFood(Food): # upload_to='label/', # ) + @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() + class Meta: verbose_name = _('Basic food') verbose_name_plural = _('Basic foods') @@ -146,6 +165,47 @@ class TransformedFood(Food): verbose_name=_('is active'), ) + @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 + self.expiry_date = self.creation_date + timedelta(days=3) + 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) + class Meta: verbose_name = _('Transformed food') verbose_name_plural = _('Transformed foods') diff --git a/apps/food/templates/food/basicfood_detail.html b/apps/food/templates/food/basicfood_detail.html index b0629f58..8daefaf8 100644 --- a/apps/food/templates/food/basicfood_detail.html +++ b/apps/food/templates/food/basicfood_detail.html @@ -12,6 +12,15 @@ SPDX-License-Identifier: GPL-3.0-or-later
name : {{ food.name }}
+owner : {{ food.owner }}
+arrival_date : {{ food.arrival_date }}
+expiry_date : {{ food.expiry_date }}
+allergens :
+