From 7f12ee63f2943c9b36880830166f4a021ceca43d Mon Sep 17 00:00:00 2001 From: korenstin Date: Sun, 7 Jul 2024 21:25:26 +0200 Subject: [PATCH] Open table and shelf life --- apps/food/forms.py | 2 +- .../0002_transformedfood_shelf_life.py | 19 +++++++++++++++ apps/food/models.py | 8 ++++++- .../templates/food/transformedfood_list.html | 10 +++++--- apps/food/views.py | 23 +++++++++++++------ 5 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 apps/food/migrations/0002_transformedfood_shelf_life.py diff --git a/apps/food/forms.py b/apps/food/forms.py index fe0775a0..59226a52 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') + fields = ('name', 'creation_date', 'owner', 'is_active', 'shelf_life') widgets = { "owner": Autocomplete( model=Club, diff --git a/apps/food/migrations/0002_transformedfood_shelf_life.py b/apps/food/migrations/0002_transformedfood_shelf_life.py new file mode 100644 index 00000000..46673643 --- /dev/null +++ b/apps/food/migrations/0002_transformedfood_shelf_life.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.28 on 2024-07-06 20:37 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('food', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='transformedfood', + name='shelf_life', + field=models.DurationField(default=datetime.timedelta(days=3), verbose_name='shelf life'), + ), + ] diff --git a/apps/food/models.py b/apps/food/models.py index e68a5c65..50db24f3 100644 --- a/apps/food/models.py +++ b/apps/food/models.py @@ -165,6 +165,12 @@ class TransformedFood(Food): verbose_name=_('is active'), ) + # Without microbiological analyzes, the storage time is 3 days + shelf_life = models.DurationField( + verbose_name=_("shelf life"), + default=timedelta(days=3), + ) + @transaction.atomic def update_allergens(self): # When allergens are changed, simply update the parents' allergens @@ -185,7 +191,7 @@ class TransformedFood(Food): 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) + self.expiry_date = self.creation_date + self.shelf_life for ingredient in self.ingredient.iterator(): self.expiry_date = min(self.expiry_date, ingredient.expiry_date) diff --git a/apps/food/templates/food/transformedfood_list.html b/apps/food/templates/food/transformedfood_list.html index a7b276fc..98d89aa4 100644 --- a/apps/food/templates/food/transformedfood_list.html +++ b/apps/food/templates/food/transformedfood_list.html @@ -7,14 +7,18 @@ SPDX-License-Identifier: GPL-3.0-or-later {% block content %}
-

- Transformed food -

+

+ In preparation +

{% render_table table %} +

+ Open +

+ {% render_table open_table %}
{% endblock %} diff --git a/apps/food/views.py b/apps/food/views.py index b6fb4bb5..ec7b7429 100644 --- a/apps/food/views.py +++ b/apps/food/views.py @@ -1,8 +1,6 @@ # 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 transaction from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponseRedirect @@ -234,12 +232,13 @@ class TransformedFoodFormView(ProtectQuerysetMixin): # Save the aliment and allergens associated transformed_food = form.save(commit=False) - # Without microbiological analyzes, the storage time is 3 days - transformed_food.expiry_date = transformed_food.creation_date + timedelta(days=3) + transformed_food.expiry_date = transformed_food.creation_date transformed_food._force_save = True transformed_food.save() transformed_food.refresh_from_db() - return super().form_valid(form) + ans = super().form_valid(form) + transformed_food.update() + return ans def get_success_url(self, **kwargs): self.object.refresh_from_db() @@ -260,14 +259,24 @@ class TransformedFoodCreateView(TransformedFoodFormView, ProtectedCreateView): class TransfomedListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): """ - Displays all Activities, and classify if they are on-going or upcoming ones. + Displays not ready TransformedFood """ model = TransformedFood table_class = TransformedFoodTable - ordering = ('-name',) + ordering = ('name',) extra_context = {"title": _("Transformed food")} def get_queryset(self, **kwargs): return super().get_queryset(**kwargs)\ .filter(is_ready=False)\ .distinct() + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['open_table'] = TransformedFoodTable( + TransformedFood.objects.filter( + was_eaten=False, + expiry_date__lt=timezone.now() + ), + prefix="open-") + return context