1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-06-21 09:58:23 +02:00

Improve/modify form, view, template. Add permissions

This commit is contained in:
quark
2024-08-17 02:28:27 +02:00
parent 6d7076b03e
commit debeb33d46
11 changed files with 605 additions and 132 deletions

View File

@ -11,7 +11,7 @@ from note_kfet.inputs import Autocomplete, DateTimePickerInput
from note_kfet.middlewares import get_current_request
from permission.backends import PermissionBackend
from .models import BasicFood, QRCode, TransformedFood, Food
from .models import BasicFood, QRCode, TransformedFood
class AddIngredientForms(forms.ModelForm):
@ -20,11 +20,20 @@ class AddIngredientForms(forms.ModelForm):
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['ingredient'].queryset = self.fields['ingredient'].queryset.filter(is_ready=False, is_active=True, was_eaten=False)
self.fields['ingredient'].queryset = self.fields['ingredient'].queryset.filter(
polymorphic_ctype__model='transformedfood',
owner_id=self.instance.owner_id,
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")
class Meta:
model = TransformedFood
fields = ('ingredient',)
fields = ('ingredient', 'is_active')
class BasicFoodForms(forms.ModelForm):
@ -38,7 +47,7 @@ class BasicFoodForms(forms.ModelForm):
self.fields['owner'].required = True
# Some example
self.fields['name'].widget.attrs.update({"placeholder": _("pasta")})
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]) + ", ..."
@ -84,7 +93,7 @@ class TransformedFoodForms(forms.ModelForm):
self.fields['was_eaten'].initial = False
# Some example
self.fields['name'].widget.attrs.update({"placeholder": _("lasagna")})
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]) + ", ..."
@ -99,13 +108,3 @@ class TransformedFoodForms(forms.ModelForm):
),
'creation_date': DateTimePickerInput(),
}
class FoodForms(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['was_eaten'].initial = True
class Meta:
model = Food
fields = ('was_eaten',)