diff --git a/apps/food/admin.py b/apps/food/admin.py index 820036c3..7579aff5 100644 --- a/apps/food/admin.py +++ b/apps/food/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from note_kfet.admin import admin_site -from .models import QR_code, Basic_food, Transformed_food, Allergen +from .models import QR_code, BasicFood, TransformedFood, Allergen @admin.register(QR_code, site = admin_site) @@ -10,13 +10,13 @@ class QR_codeAdmin(admin.ModelAdmin): TEMPORARY """ -@admin.register(Basic_food, site = admin_site) +@admin.register(BasicFood, site = admin_site) class Basic_foodAdmin(admin.ModelAdmin): """ TEMPORARY """ -@admin.register(Transformed_food, site = admin_site) +@admin.register(TransformedFood, site = admin_site) class Transformed_foodAdmin(admin.ModelAdmin): """ TEMPORARY diff --git a/apps/food/forms.py b/apps/food/forms.py index 92887c32..5855c936 100644 --- a/apps/food/forms.py +++ b/apps/food/forms.py @@ -7,14 +7,14 @@ from django import forms from django.utils.translation import gettext_lazy as _ from django.utils import timezone from member.models import Club -from note_kfet.inputs import Autocomplete, DatePickerInput +from note_kfet.inputs import Autocomplete, DatePickerInput, DateTimePickerInput from note_kfet.middlewares import get_current_request from permission.backends import PermissionBackend -from .models import QR_code, Allergen, Basic_food, Transformed_food +from .models import QR_code, Allergen, BasicFood, TransformedFood -class Basic_foodForms(forms.ModelForm): +class BasicFoodForms(forms.ModelForm): """ Form for add non-transformed food """ @@ -38,7 +38,7 @@ class Basic_foodForms(forms.ModelForm): return is_ddm class Meta: - model = Basic_food + model = BasicFood fields = ('name', 'owner', 'is_DLC', 'is_DDM', 'expiry_date', 'label') widgets = { "owner": Autocomplete( @@ -50,7 +50,7 @@ class Basic_foodForms(forms.ModelForm): -class Transformed_foodForms(forms.ModelForm): +class TransformedFoodForms(forms.ModelForm): """ Form for add transformed food """ @@ -71,14 +71,14 @@ class Transformed_foodForms(forms.ModelForm): class Meta: - model = Transformed_food + model = TransformedFood fields = ('name', 'creation_date', 'owner', 'is_active',) - widget = { + widgets = { "owner": Autocomplete( model = Club, attrs = {"api_url": "/api/members/club/"}, ), - 'creation_date': DatePickerInput(), + 'creation_date': DateTimePickerInput(), } class AllergenForms(forms.ModelForm): """ diff --git a/apps/food/migrations/0003_auto_20240525_1350.py b/apps/food/migrations/0003_auto_20240525_1350.py new file mode 100644 index 00000000..23321d64 --- /dev/null +++ b/apps/food/migrations/0003_auto_20240525_1350.py @@ -0,0 +1,55 @@ +# 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 new file mode 100644 index 00000000..5c7932c4 --- /dev/null +++ b/apps/food/migrations/0004_auto_20240525_1352.py @@ -0,0 +1,18 @@ +# 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/models.py b/apps/food/models.py index e957ae99..b0a222f4 100644 --- a/apps/food/models.py +++ b/apps/food/models.py @@ -7,7 +7,7 @@ from django.conf import settings from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.core.validators import MinValueValidator -from django.db import models +from django.db import models, transaction from django.db.models import Q from django.utils import timezone from django.utils.translation import gettext_lazy as _ @@ -23,7 +23,7 @@ class QR_code(models.Model): ) transformed_food_container = models.ForeignKey( - 'Transformed_food', + 'TransformedFood', on_delete = models.PROTECT, related_name = 'QR_code', null = True, @@ -32,7 +32,7 @@ class QR_code(models.Model): ) basic_food = models.ForeignKey( - 'Basic_food', + 'BasicFood', on_delete = models.PROTECT, related_name = 'QR_code', null = True, @@ -128,7 +128,7 @@ class Allergen(models.Model): ) transformed_food = models.ForeignKey( - 'Transformed_food', + 'TransformedFood', on_delete = models.CASCADE, related_name = 'Allergen', blank = True, @@ -137,7 +137,7 @@ class Allergen(models.Model): ) basic_food = models.ForeignKey( - 'Basic_food', + 'BasicFood', on_delete = models.CASCADE, related_name = 'Allergen', blank = True, @@ -150,10 +150,12 @@ class Allergen(models.Model): verbose_name_plural = _('Allergens') def __str__(self): - return _('Allergens of #{id}').format(id=self.id) + return _('Allergens of #{id}').format(id=self.id) + + -class Basic_food(models.Model): +class BasicFood(models.Model): """ Food which has been directly buy on supermarket """ @@ -201,8 +203,8 @@ class Basic_food(models.Model): ) transformed_food = models.ManyToManyField( - 'Transformed_food', - related_name= 'Basic_food', + 'TransformedFood', + related_name= 'BasicFood', blank = True, verbose_name = _('transformed food'), ) @@ -215,7 +217,15 @@ class Basic_food(models.Model): def __str__(self): return self.name -class Transformed_food(models.Model): + @transaction.atomic + def save(self, force_insert=False, force_update=False, using= None, update_fields=None): + # Check if is_DLC and is DDM are not both True + if self.is_DLC and self.is_DDM: + raise ValidationError("The product cannot be a DLC and a DDM") + return super().save(force_insert, force_update, using, update_fields) + + +class TransformedFood(models.Model): """ Transformed food are a mix between basic food and meal """ @@ -265,4 +275,6 @@ class Transformed_food(models.Model): def __str__(self): return self.name - + @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) diff --git a/apps/food/templates/food/basic_food_form.html b/apps/food/templates/food/basic_food_form.html index a56aa063..abce7e9b 100644 --- a/apps/food/templates/food/basic_food_form.html +++ b/apps/food/templates/food/basic_food_form.html @@ -13,6 +13,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
diff --git a/apps/food/templates/food/transformed_food_form.html b/apps/food/templates/food/transformed_food_form.html new file mode 100644 index 00000000..917654db --- /dev/null +++ b/apps/food/templates/food/transformed_food_form.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags %} + +{% block content %} +