From 5174c84b338a0ce2c9d5f1cfbf5d419c655a721f Mon Sep 17 00:00:00 2001 From: Emmy D'ANELLO Date: Thu, 18 Aug 2022 14:50:45 +0200 Subject: [PATCH] Manage food options Signed-off-by: Emmy D'ANELLO --- apps/sheets/forms.py | 30 ++++++++- apps/sheets/templates/sheets/food_form.html | 67 ++++++++++++++++++- .../sheets/templates/sheets/sheet_detail.html | 12 ++-- apps/sheets/views.py | 60 ++++++++++++++++- locale/fr/LC_MESSAGES/django.po | 20 ++++-- 5 files changed, 172 insertions(+), 17 deletions(-) diff --git a/apps/sheets/forms.py b/apps/sheets/forms.py index 32fe0b72..00c313ec 100644 --- a/apps/sheets/forms.py +++ b/apps/sheets/forms.py @@ -1,12 +1,12 @@ # Copyright (C) 2018-2022 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later - +from crispy_forms.helper import FormHelper from django import forms from member.models import Club from note_kfet.inputs import AmountInput, Autocomplete, DateTimePickerInput -from .models import Food, Meal, Sheet +from .models import Food, FoodOption, Meal, Sheet class SheetForm(forms.ModelForm): @@ -31,6 +31,32 @@ class FoodForm(forms.ModelForm): } +class FoodOptionForm(forms.ModelForm): + class Meta: + model = FoodOption + fields = '__all__' + widgets = { + 'extra_cost': AmountInput(), + } + + +FoodOptionsFormSet = forms.inlineformset_factory( + Food, + FoodOption, + form=FoodOptionForm, + extra=0, +) + + +class FoodOptionFormSetHelper(FormHelper): + def __init__(self, form=None): + super().__init__(form) + self.form_tag = False + self.form_method = 'POST' + self.form_class = 'form-inline' + self.template = 'bootstrap4/table_inline_formset.html' + + class MealForm(forms.ModelForm): class Meta: model = Meal diff --git a/apps/sheets/templates/sheets/food_form.html b/apps/sheets/templates/sheets/food_form.html index c62fec40..0b13f206 100644 --- a/apps/sheets/templates/sheets/food_form.html +++ b/apps/sheets/templates/sheets/food_form.html @@ -14,8 +14,73 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% csrf_token %} {{ form|crispy }} + + {# The next part concerns the option formset #} + {# Generate some hidden fields that manage the number of options, and make easier the parsing #} + {{ formset.management_form }} + + {# Fill initial data #} + {% for form in formset %} + {% if forloop.first %} + + + + + + + + + {% endif %} + + + + + {# These fields are hidden but handled by the formset to link the id and the invoice id #} + {{ form.food }} + {{ form.id }} + + {% endfor %} + +
{{ form.name.label }}*{{ form.extra_cost.label }}*{{ form.available.label }}*
{{ form.name }}{{ form.extra_cost }}{{ form.available }}
+ + {# Display buttons to add and remove options #} +
+ +
+
-{% endblock %} \ No newline at end of file + +{# Hidden div that store an empty product form, to be copied into new forms #} + +{% endblock %} + +{% block extrajavascript %} + +{% endblock %} diff --git a/apps/sheets/templates/sheets/sheet_detail.html b/apps/sheets/templates/sheets/sheet_detail.html index a52a69ff..3163d8e9 100644 --- a/apps/sheets/templates/sheets/sheet_detail.html +++ b/apps/sheets/templates/sheets/sheet_detail.html @@ -28,7 +28,7 @@

{% trans "menu"|capfirst %} :