From 896095a44ccc235bc51865744fa41326ea6fc528 Mon Sep 17 00:00:00 2001 From: quark Date: Sat, 25 May 2024 22:34:59 +0200 Subject: [PATCH] Un peu de nettoyage, rajout de commentaires --- apps/food/admin.py | 3 + apps/food/migrations/0001_initial.py | 25 +++++---- .../migrations/0002_auto_20240524_2139.py | 19 ------- .../migrations/0003_auto_20240525_1350.py | 55 ------------------- .../migrations/0004_auto_20240525_1352.py | 18 ------ .../migrations/0005_auto_20240525_1559.py | 18 ------ apps/food/models.py | 12 +++- apps/food/templates/food/basic_food_form.html | 1 + .../templates/food/transformed_food_form.html | 1 + apps/food/urls.py | 8 ++- apps/food/views.py | 49 ++++++----------- 11 files changed, 51 insertions(+), 158 deletions(-) delete mode 100644 apps/food/migrations/0002_auto_20240524_2139.py delete mode 100644 apps/food/migrations/0003_auto_20240525_1350.py delete mode 100644 apps/food/migrations/0004_auto_20240525_1352.py delete mode 100644 apps/food/migrations/0005_auto_20240525_1559.py diff --git a/apps/food/admin.py b/apps/food/admin.py index 7579aff5..d5005adc 100644 --- a/apps/food/admin.py +++ b/apps/food/admin.py @@ -1,3 +1,6 @@ +# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + from django.contrib import admin from note_kfet.admin import admin_site diff --git a/apps/food/migrations/0001_initial.py b/apps/food/migrations/0001_initial.py index 49b7a329..b51c8a59 100644 --- a/apps/food/migrations/0001_initial.py +++ b/apps/food/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.28 on 2024-05-21 12:05 +# Generated by Django 2.2.28 on 2024-05-25 20:32 from django.db import migrations, models import django.db.models.deletion @@ -15,14 +15,15 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='Basic_food', + name='BasicFood', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=255, verbose_name='name')), ('is_DLC', models.BooleanField(default=False, verbose_name='is DLC')), ('is_DDM', models.BooleanField(default=False, verbose_name='is DDM')), - ('expiry_date', models.DateTimeField(blank=True, default=django.utils.timezone.now, verbose_name='expiry date')), - ('label', models.ImageField(default='pic/default.png', max_length=255, upload_to='label/', verbose_name='food label')), + ('arrival_date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='arrival date')), + ('expiry_date', models.DateTimeField(blank=True, null=True, verbose_name='expiry date')), + ('label', models.ImageField(max_length=255, upload_to='label/', verbose_name='food label')), ('was_eaten', models.BooleanField(default=False, verbose_name='was eaten')), ('owner', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='member.Club', verbose_name='owner')), ], @@ -32,7 +33,7 @@ class Migration(migrations.Migration): }, ), migrations.CreateModel( - name='Transformed_food', + name='TransformedFood', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=255, verbose_name='name')), @@ -41,7 +42,7 @@ class Migration(migrations.Migration): ('is_active', models.BooleanField(default=True, verbose_name='is active')), ('was_eaten', models.BooleanField(default=False, verbose_name='was eaten')), ('owner', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='member.Club', verbose_name='owner')), - ('transformed_ingredient', models.ManyToManyField(blank=True, related_name='transformed_ingredient_inv', to='food.Transformed_food', verbose_name='transformed ingredient')), + ('transformed_ingredient', models.ManyToManyField(blank=True, related_name='transformed_ingredient_inv', to='food.TransformedFood', verbose_name='transformed ingredient')), ], options={ 'verbose_name': 'Transformed food', @@ -53,8 +54,8 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('qr_code_number', models.PositiveIntegerField(verbose_name='QR-code number')), - ('basic_food', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='QR_code', to='food.Basic_food', verbose_name='basic food')), - ('transformed_food_container', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='QR_code', to='food.Transformed_food', verbose_name='transformed food container')), + ('basic_food', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='QR_code', to='food.BasicFood', verbose_name='basic food')), + ('transformed_food_container', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='QR_code', to='food.TransformedFood', verbose_name='transformed food container')), ], options={ 'verbose_name': 'QR-code', @@ -62,9 +63,9 @@ class Migration(migrations.Migration): }, ), migrations.AddField( - model_name='basic_food', + model_name='basicfood', name='transformed_food', - field=models.ManyToManyField(blank=True, related_name='Basic_food', to='food.Transformed_food', verbose_name='transformed food'), + field=models.ManyToManyField(blank=True, related_name='BasicFood', to='food.TransformedFood', verbose_name='transformed food'), ), migrations.CreateModel( name='Allergen', @@ -85,8 +86,8 @@ class Migration(migrations.Migration): ('groundnut', models.BooleanField(default=False, verbose_name='groundnut')), ('sesame', models.BooleanField(default=False, verbose_name='sesame')), ('alcohol', models.BooleanField(default=False, verbose_name='alcohol')), - ('basic_food', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='Allergen', to='food.Basic_food', verbose_name='basic food')), - ('transformed_food', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='Allergen', to='food.Transformed_food', verbose_name='transformed food')), + ('basic_food', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='Allergen', to='food.BasicFood', verbose_name='basic food')), + ('transformed_food', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='Allergen', to='food.TransformedFood', verbose_name='transformed food')), ], options={ 'verbose_name': 'Allergen', diff --git a/apps/food/migrations/0002_auto_20240524_2139.py b/apps/food/migrations/0002_auto_20240524_2139.py deleted file mode 100644 index d4423976..00000000 --- a/apps/food/migrations/0002_auto_20240524_2139.py +++ /dev/null @@ -1,19 +0,0 @@ -# Generated by Django 2.2.28 on 2024-05-24 19:39 - -from django.db import migrations, models -import django.utils.timezone - - -class Migration(migrations.Migration): - - dependencies = [ - ('food', '0001_initial'), - ] - - operations = [ - migrations.AlterField( - model_name='basic_food', - name='expiry_date', - field=models.DateTimeField(blank=True, default=django.utils.timezone.now, null=True, verbose_name='expiry date'), - ), - ] diff --git a/apps/food/migrations/0003_auto_20240525_1350.py b/apps/food/migrations/0003_auto_20240525_1350.py deleted file mode 100644 index 23321d64..00000000 --- a/apps/food/migrations/0003_auto_20240525_1350.py +++ /dev/null @@ -1,55 +0,0 @@ -# Generated by Django 2.2.28 on 2024-05-25 11:50 - -from django.db import migrations, models -import django.db.models.deletion -import django.utils.timezone - - -class Migration(migrations.Migration): - - dependencies = [ - ('member', '0011_profile_vss_charter_read'), - ('food', '0002_auto_20240524_2139'), - ] - - operations = [ - migrations.CreateModel( - name='BasicFood', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=255, verbose_name='name')), - ('is_DLC', models.BooleanField(default=False, verbose_name='is DLC')), - ('is_DDM', models.BooleanField(default=False, verbose_name='is DDM')), - ('expiry_date', models.DateTimeField(blank=True, default=django.utils.timezone.now, null=True, verbose_name='expiry date')), - ('label', models.ImageField(default='pic/default.png', max_length=255, upload_to='label/', verbose_name='food label')), - ('was_eaten', models.BooleanField(default=False, verbose_name='was eaten')), - ('owner', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='+', to='member.Club', verbose_name='owner')), - ], - options={ - 'verbose_name': 'Basic food', - 'verbose_name_plural': 'Basic foods', - }, - ), - migrations.AlterField( - model_name='allergen', - name='basic_food', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='Allergen', to='food.BasicFood', verbose_name='basic food'), - ), - migrations.AlterField( - model_name='qr_code', - name='basic_food', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='QR_code', to='food.BasicFood', verbose_name='basic food'), - ), - migrations.RenameModel( - old_name='Transformed_food', - new_name='TransformedFood', - ), - migrations.DeleteModel( - name='Basic_food', - ), - migrations.AddField( - model_name='basicfood', - name='transformed_food', - field=models.ManyToManyField(blank=True, null=True, related_name='BasicFood', to='food.TransformedFood', verbose_name='transformed food'), - ), - ] diff --git a/apps/food/migrations/0004_auto_20240525_1352.py b/apps/food/migrations/0004_auto_20240525_1352.py deleted file mode 100644 index 5c7932c4..00000000 --- a/apps/food/migrations/0004_auto_20240525_1352.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2.28 on 2024-05-25 11:52 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('food', '0003_auto_20240525_1350'), - ] - - operations = [ - migrations.AlterField( - model_name='basicfood', - name='transformed_food', - field=models.ManyToManyField(blank=True, related_name='BasicFood', to='food.TransformedFood', verbose_name='transformed food'), - ), - ] diff --git a/apps/food/migrations/0005_auto_20240525_1559.py b/apps/food/migrations/0005_auto_20240525_1559.py deleted file mode 100644 index 88d794b2..00000000 --- a/apps/food/migrations/0005_auto_20240525_1559.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2.28 on 2024-05-25 13:59 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('food', '0004_auto_20240525_1352'), - ] - - operations = [ - migrations.AlterField( - model_name='basicfood', - name='label', - field=models.ImageField(max_length=255, upload_to='label/', verbose_name='food label'), - ), - ] diff --git a/apps/food/models.py b/apps/food/models.py index 7bc47da1..44a9f595 100644 --- a/apps/food/models.py +++ b/apps/food/models.py @@ -13,6 +13,12 @@ from django.utils import timezone from django.utils.translation import gettext_lazy as _ from member.models import Club +################################################################# +# TO DO +# - link allergen with one food (basic or transformed) with check +# - check on basic food +# - check on transformed food +################################################################# class QR_code(models.Model): """ @@ -173,10 +179,14 @@ class BasicFood(models.Model): verbose_name=_("is DDM"), default=False, ) + + arrival_date = models.DateTimeField( + verbose_name=_('arrival date'), + default=timezone.now, + ) expiry_date = models.DateTimeField( verbose_name=_('expiry date'), - default=timezone.now, blank=True, null = True, ) diff --git a/apps/food/templates/food/basic_food_form.html b/apps/food/templates/food/basic_food_form.html index abce7e9b..43cf1c43 100644 --- a/apps/food/templates/food/basic_food_form.html +++ b/apps/food/templates/food/basic_food_form.html @@ -7,6 +7,7 @@ SPDX-License-Identifier: GPL-3.0-or-later {% block content %}

+ HTML not finished
{{ title }}

diff --git a/apps/food/templates/food/transformed_food_form.html b/apps/food/templates/food/transformed_food_form.html index 917654db..fd18a5f8 100644 --- a/apps/food/templates/food/transformed_food_form.html +++ b/apps/food/templates/food/transformed_food_form.html @@ -7,6 +7,7 @@ SPDX-License-Identifier: GPL-3.0-or-later {% block content %}

+ HTML not finished
{{ title }}

diff --git a/apps/food/urls.py b/apps/food/urls.py index b81bc8fa..7d4f54a3 100644 --- a/apps/food/urls.py +++ b/apps/food/urls.py @@ -5,12 +5,14 @@ from django.urls import path from . import views +############################### +# TO DO +# - name url correctly, thinking about the scheme of the app +############################### + app_name = 'food' urlpatterns = [ - path('test_basic_food_form',views.test_basic_foodform), - path('test_transformed_food_form', views.test_transformed_foodform), - path('test_allergen_form', views.test_allergenform), path('0', views.BasicFoodCreateView.as_view(), name = 'basic_food'), path('1', views.TransformedFoodCreateView.as_view(), name = 'transformed_food'), ] diff --git a/apps/food/views.py b/apps/food/views.py index 1768fd38..edd00e74 100644 --- a/apps/food/views.py +++ b/apps/food/views.py @@ -1,57 +1,35 @@ # Copyright (C) 2018-2024 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -''' -from crispy_forms.helper import FormHelper -from django.contrib.auth.mixins import LoginRequiredMixin -from django.core.exceptions import ValidationError, PermissionDenied -from django.db.models import Q -from django.forms import Form -from django.http import HttpResponse -from django.shortcuts import redirect -''' + from django.urls import reverse_lazy from django.db import transaction from django.utils.translation import gettext_lazy as _ from django.utils import timezone from datetime import timedelta -''' -from django.views.generic import UpdateView, DetailView -from django.views.generic.base import View, TemplateView -from django.views.generic.edit import BaseFormView, DeleteView -from django_tables2 import SingleTableView -''' from permission.views import ProtectQuerysetMixin, ProtectedCreateView - from django.shortcuts import render from .forms import BasicFoodForms, TransformedFoodForms, AllergenForms from .models import BasicFood, TransformedFood, Allergen -def test_basic_foodform(request): - return render(request,'food/test.html', {"form": BasicFoodForms()}) - -def test_transformed_foodform(request): - return render(request,'food/transformed_food_form.html', {"form": TransformedFoodForms()}) - -def test_allergenform(request): - return render(request,'food/test.html', {"form": AllergenForms()}) - class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): +##################################################################### +# TO DO +# - fix picture save +# - implement solution crop and convert image (reuse or recode ImageForm from members apps +# - implement AllergenForms +# - redirect to another view after the poll is submitted +##################################################################### """ A view to add a basic food """ model = BasicFood form_class = BasicFoodForms template_name = 'food/basic_food_form.html' - second_form = AllergenForms extra_context = {"title": _("Add a new aliment")} def get_sample_object(self): - return Allergen( - groundnut = "False", - ) - ''' return BasicFood( name="", is_DLC=False, @@ -59,7 +37,6 @@ class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): expiry_date=timezone.now(), label='pic/default.png', ) - ''' @transaction.atomic def form_valid(self, form): @@ -71,6 +48,8 @@ class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): # Save the aliment and the allergens associed basic_food = form.save(commit=False) + # We assume the date of labeling and the same as the date of arrival + basic_food.arrival_date = timezone.now basic_food._force_save = True basic_food.save() basic_food.refresh_from_db() @@ -78,10 +57,15 @@ class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): def get_success_url(self, **kwargs): self.object.refresh_from_db() + # TEMPORARY, I create a fonctionnal view before # return reverse_lazy('food:basicfood', kwargs={"pk": self.object.pk}) return '0' class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): +############################################### +# TO DO +# -redirect to another view after submit +############################################### """ A view to add a tranformed food """ @@ -105,7 +89,7 @@ class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): # Save the aliment and allergens associated transformed_food = form.save(commit=False) - # Without microbiologic analysis, the time of conservation is 3 days + # Without microbiological analyzes, the storage time is 3 days transformed_food.expiry_date = transformed_food.creation_date + timedelta(days = 3) transformed_food._force_save = True transformed_food.save() @@ -115,5 +99,6 @@ class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView): def get_success_url(self, **kwargs): self.object.refresh_from_db() + # TEMPORARY, I create a fonctionnal view before # return reverse_lazy('food:tranformed_food', kwargs={"pk": self.object.pk}) return '1'