From adacc293f50bca3b4a76beecff0fb39936be2a85 Mon Sep 17 00:00:00 2001 From: quark Date: Thu, 23 May 2024 23:53:33 +0200 Subject: [PATCH] First forms --- apps/food/forms.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 apps/food/forms.py diff --git a/apps/food/forms.py b/apps/food/forms.py new file mode 100644 index 00000000..e072c00a --- /dev/null +++ b/apps/food/forms.py @@ -0,0 +1,92 @@ +# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from random import shuffle + +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, DateTimePickerInput +from note_kfet.middlewares import get_current_request +from permission.backends import PermissionBackend + +from .models import QR_code, Allergen, Basic_food, Transformed_food + + +class Basic_foodForms(forms.ModelForm): + """ + Form for add non-transformed food + """ + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['name'].widget.attrs.update({"autofocus": "autofocus"}) + self.fields['name'].required = True + self.fields['owner'].required = True + self.fields['label'].help_text = _('The lot number must be contained in the picture') + + # Some example + self.fields['name'].widget.attrs.update({"placeholder": _("pasta")}) + clubs = list(Club.objects.filter(PermissionBackend.filter_queryset(get_current_request(), Club, "change")).all()) + shuffle(clubs) + self.fields['owner'].widget.attrs["placeholder"] = ", ".join(club.name for club in clubs[:4]) + ", ..." + def clean_dlm_or_dlc(self): + is_dlc = self.cleaned_data["is_DLC"] + is_ddm = self.cleaned_data["is_DDM"] + if is_dlc and is_ddm: + self.add_error("is_ddm", _("the product cannot be a DLC and a DDM")) + return is_ddm + + class Meta: + model = Basic_food + fields = ('name', 'owner', 'is_DLC', 'is_DDM', 'expiry_date', 'label') + widget = { + "owner": Autocomplete( + model = Club, + attrs = {"api_url": "/api/members/club/"}, + ), + 'expiry_date': DateTimePickerInput(), + } + + + +class Transformed_foodForms(forms.ModelForm): + """ + Form for add transformed food + """ + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['name'].widget.attrs.update({"autofocus": "autofocus"}) + self.fields['name'].required = True + self.fields['owner'].required = True + self.fields['creation_date'].required = True + self.fields['creation_date'].initial = timezone.now + self.fields['is_active'].initial = True + + # Some example + self.fields['name'].widget.attrs.update({"placeholder": _("lasagna")}) + clubs = list(Club.objects.filter(PermissionBackend.filter_queryset(get_current_request(), Club, "change")).all()) + shuffle(clubs) + self.fields['owner'].widget.attrs["placeholder"] = ", ".join(club.name for club in clubs[:4]) + ", ..." + + + class Meta: + model = Transformed_food + fields = ('name', 'creation_date', 'owner', 'is_active',) + widget = { + "owner": Autocomplete( + model = Club, + attrs = {"api_url": "/api/members/club/"}, + ), + 'creation_date': DateTimePickerInput(), + } +class AllergenForms(forms.ModelForm): + """ + Form for allergen + """ + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + class Meta: + model = Allergen + exclude = ['basic_food', 'transformed_food']