# Copyright (C) 2018-2025 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later from random import shuffle from bootstrap_datepicker_plus.widgets import DateTimePickerInput from django import forms from django.forms.widgets import NumberInput from django.utils.translation import gettext_lazy as _ from member.models import Club from note_kfet.inputs import Autocomplete from note_kfet.middlewares import get_current_request from permission.backends import PermissionBackend from .models import BasicFood, TransformedFood, QRCode class QRCodeForms(forms.ModelForm): """ Form for create QRCode for container """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['food_container'].queryset = self.fields['food_container'].queryset.filter( is_ready=False, end_of_life__isnull=True, polymorphic_ctype__model='transformedfood', ).filter(PermissionBackend.filter_queryset( get_current_request(), TransformedFood, "view", )) class Meta: model = QRCode fields = ('food_container',) class BasicFoodForms(forms.ModelForm): """ Form for add basicfood """ 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 # Some example self.fields['name'].widget.attrs.update({"placeholder": _("Pasta METRO 5kg")}) 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]) + ", ..." self.fields['order'].widget.attrs["placeholder"] = _("Specific order given to GCKs") class Meta: model = BasicFood fields = ('name', 'owner', 'date_type', 'expiry_date', 'allergens', 'order',) widgets = { "owner": Autocomplete( model=Club, attrs={"api_url": "/api/members/club/"}, ), "expiry_date": DateTimePickerInput(), } class TransformedFoodForms(forms.ModelForm): """ Form for add transformedfood """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['name'].required = True self.fields['owner'].required = 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]) + ", ..." self.fields['order'].widget.attrs["placeholder"] = _("Specific order given to GCKs") class Meta: model = TransformedFood fields = ('name', 'owner', 'order',) widgets = { "owner": Autocomplete( model=Club, attrs={"api_url": "/api/members/club/"}, ), } class BasicFoodUpdateForms(forms.ModelForm): """ Form for update basicfood object """ class Meta: model = BasicFood fields = ('name', 'owner', 'date_type', 'expiry_date', 'end_of_life', 'is_ready', 'order', 'allergens') widgets = { "owner": Autocomplete( model=Club, attrs={"api_url": "/api/members/club/"}, ), "expiry_date": DateTimePickerInput(), } class TransformedFoodUpdateForms(forms.ModelForm): """ Form for update transformedfood object """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['shelf_life'].label = _('Shelf life (in hours)') class Meta: model = TransformedFood fields = ('name', 'owner', 'end_of_life', 'is_ready', 'order', 'shelf_life') widgets = { "owner": Autocomplete( model=Club, attrs={"api_url": "/api/members/club/"}, ), "expiry_date": DateTimePickerInput(), "shelf_life": NumberInput(), } class AddIngredientForms(forms.ModelForm): """ Form for add an ingredient """ fully_used = forms.BooleanField() fully_used.initial = True fully_used.required = False fully_used.label = _("Fully used") def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # TODO find a better way to get pk (be not url scheme dependant) pk = get_current_request().path.split('/')[-1] self.fields['ingredients'].queryset = self.fields['ingredients'].queryset.filter( polymorphic_ctype__model="transformedfood", is_ready=False, end_of_life='', ).filter(PermissionBackend.filter_queryset(get_current_request(), TransformedFood, "change")).exclude(pk=pk) class Meta: model = TransformedFood fields = ('ingredients',)