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-07-03 17:20:01 +00:00
|
|
|
from note_kfet.inputs import Autocomplete, DateTimePickerInput
|
2024-05-23 21:53:33 +00:00
|
|
|
from note_kfet.middlewares import get_current_request
|
|
|
|
from permission.backends import PermissionBackend
|
|
|
|
|
2024-08-17 00:28:27 +00:00
|
|
|
from .models import BasicFood, QRCode, TransformedFood
|
2024-07-05 09:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddIngredientForms(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Form for add an ingredient
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2024-08-17 00:28:27 +00:00
|
|
|
self.fields['ingredient'].queryset = self.fields['ingredient'].queryset.filter(
|
|
|
|
polymorphic_ctype__model='transformedfood',
|
|
|
|
is_ready=False,
|
|
|
|
is_active=True,
|
|
|
|
was_eaten=False,
|
|
|
|
)
|
|
|
|
# Caution, the logic is inverted here, we flip the logic on saving in AddIngredientView
|
|
|
|
self.fields['is_active'].initial = True
|
|
|
|
self.fields['is_active'].label = _("Fully used")
|
2024-07-05 09:57:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = TransformedFood
|
2024-08-17 00:28:27 +00:00
|
|
|
fields = ('ingredient', 'is_active')
|
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
|
|
|
|
|
|
|
|
# Some example
|
2024-08-17 00:28:27 +00:00
|
|
|
self.fields['name'].widget.attrs.update({"placeholder": _("Pasta METRO 5kg")})
|
2024-05-23 21:53:33 +00:00
|
|
|
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 = BasicFood
|
2024-08-13 23:32:55 +00:00
|
|
|
fields = ('name', 'owner', 'date_type', 'expiry_date', 'is_active', 'was_eaten', 'allergens',)
|
2024-05-24 19:49:23 +00:00
|
|
|
widgets = {
|
2024-05-23 21:53:33 +00:00
|
|
|
"owner": Autocomplete(
|
2024-07-03 17:20:01 +00:00
|
|
|
model=Club,
|
|
|
|
attrs={"api_url": "/api/members/club/"},
|
2024-05-23 21:53:33 +00:00
|
|
|
),
|
2024-07-03 17:20:01 +00:00
|
|
|
'expiry_date': DateTimePickerInput(),
|
2024-05-23 21:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-05 09:57:44 +00:00
|
|
|
class QRCodeForms(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Form for create QRCode
|
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2024-08-27 08:45:53 +00:00
|
|
|
self.fields['food_container'].queryset = self.fields['food_container'].queryset.filter(
|
|
|
|
is_active=True,
|
|
|
|
was_eaten=False,
|
|
|
|
polymorphic_ctype__model='transformedfood',
|
|
|
|
)
|
2024-07-05 09:57:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = QRCode
|
|
|
|
fields = ('food_container',)
|
|
|
|
|
|
|
|
|
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
|
2024-08-13 23:32:55 +00:00
|
|
|
self.fields['is_ready'].initial = False
|
|
|
|
self.fields['was_eaten'].initial = False
|
2024-05-23 21:53:33 +00:00
|
|
|
|
|
|
|
# Some example
|
2024-08-17 00:28:27 +00:00
|
|
|
self.fields['name'].widget.attrs.update({"placeholder": _("Lasagna")})
|
2024-05-23 21:53:33 +00:00
|
|
|
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-08-13 23:32:55 +00:00
|
|
|
fields = ('name', 'creation_date', 'owner', 'is_active', 'is_ready', 'was_eaten', 'shelf_life')
|
2024-05-25 13:27:26 +00:00
|
|
|
widgets = {
|
2024-05-23 21:53:33 +00:00
|
|
|
"owner": Autocomplete(
|
2024-07-03 17:20:01 +00:00
|
|
|
model=Club,
|
|
|
|
attrs={"api_url": "/api/members/club/"},
|
2024-05-23 21:53:33 +00:00
|
|
|
),
|
2024-05-25 13:27:26 +00:00
|
|
|
'creation_date': DateTimePickerInput(),
|
2024-05-23 21:53:33 +00:00
|
|
|
}
|