2024-05-23 21:53:33 +00:00
|
|
|
# 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
|
2024-05-25 13:27:26 +00:00
|
|
|
from note_kfet.inputs import Autocomplete, DatePickerInput, DateTimePickerInput
|
2024-05-23 21:53:33 +00:00
|
|
|
from note_kfet.middlewares import get_current_request
|
|
|
|
from permission.backends import PermissionBackend
|
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
from .models import QR_code, Allergen, BasicFood, TransformedFood
|
2024-05-23 21:53:33 +00:00
|
|
|
|
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
class BasicFoodForms(forms.ModelForm):
|
2024-05-23 21:53:33 +00:00
|
|
|
"""
|
|
|
|
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:
|
2024-05-25 13:27:26 +00:00
|
|
|
model = BasicFood
|
2024-05-23 21:53:33 +00:00
|
|
|
fields = ('name', 'owner', 'is_DLC', 'is_DDM', 'expiry_date', 'label')
|
2024-05-24 19:49:23 +00:00
|
|
|
widgets = {
|
2024-05-23 21:53:33 +00:00
|
|
|
"owner": Autocomplete(
|
|
|
|
model = Club,
|
|
|
|
attrs = {"api_url": "/api/members/club/"},
|
|
|
|
),
|
2024-05-24 19:49:23 +00:00
|
|
|
'expiry_date': DatePickerInput(),
|
2024-05-23 21:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
class TransformedFoodForms(forms.ModelForm):
|
2024-05-23 21:53:33 +00:00
|
|
|
"""
|
|
|
|
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:
|
2024-05-25 13:27:26 +00:00
|
|
|
model = TransformedFood
|
2024-05-23 21:53:33 +00:00
|
|
|
fields = ('name', 'creation_date', 'owner', 'is_active',)
|
2024-05-25 13:27:26 +00:00
|
|
|
widgets = {
|
2024-05-23 21:53:33 +00:00
|
|
|
"owner": Autocomplete(
|
|
|
|
model = Club,
|
|
|
|
attrs = {"api_url": "/api/members/club/"},
|
|
|
|
),
|
2024-05-25 13:27:26 +00:00
|
|
|
'creation_date': DateTimePickerInput(),
|
2024-05-23 21:53:33 +00:00
|
|
|
}
|
|
|
|
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']
|