From e72e58ce8446b1299fb3987879463e573bd66400 Mon Sep 17 00:00:00 2001 From: Jacob Rief Date: Wed, 4 Sep 2024 23:25:09 +0200 Subject: [PATCH] fix #146: Dialog forms with siblings Add demo to show how to use dialogs in collections with siblings --- testapp/forms/cafeteria.py | 54 ++++++++++++++++++++++++++++++++++++++ testapp/views.py | 4 +++ 2 files changed, 58 insertions(+) create mode 100644 testapp/forms/cafeteria.py diff --git a/testapp/forms/cafeteria.py b/testapp/forms/cafeteria.py new file mode 100644 index 00000000..8ea306cc --- /dev/null +++ b/testapp/forms/cafeteria.py @@ -0,0 +1,54 @@ +from django.forms.fields import CharField, ChoiceField +from django.forms.forms import Form +from django.forms.widgets import RadioSelect +from formset.collection import FormCollection +from formset.dialog import ApplyButton, CancelButton, DialogForm +from formset.fields import Activator + + +class CoffeeForm(Form): + nickname = CharField() + flavors = Activator( + label="Add flavors", + help_text="Open the dialog to edit flavors", + ) + + +class FlavorForm(DialogForm): + title = "Choose a Flavor" + induce_open = '..coffee.flavors:active' + induce_close = '.cancel:active || .apply:active' + + flavors = ChoiceField( + choices=( + ('caramel', "Caramel Macchiato"), + ('cinnamon', "Cinnamon Dolce Latte"), + ('hazelnut', "Turkish Hazelnut"), + ('vanilla', "Vanilla Latte"), + ('chocolate', "Chocolate Fudge"), + ('almonds', "Roasted Almonds"), + ('cream', "Irish Cream"), + ), + widget=RadioSelect, + required=False, + ) + cancel = Activator( + label="Close", + widget=CancelButton, + ) + apply = Activator( + label="Apply", + widget=ApplyButton, + ) + +class CoffeeOrderCollection(FormCollection): + legend = "Order your coffee" + add_label = "Add Coffee Order" + min_siblings = 1 + extra_siblings = 1 + coffee = CoffeeForm() + flavor = FlavorForm() + + +class CafeteriaCollection(FormCollection): + coffee_order = CoffeeOrderCollection() diff --git a/testapp/views.py b/testapp/views.py index a8412197..77298e11 100644 --- a/testapp/views.py +++ b/testapp/views.py @@ -41,6 +41,7 @@ ) from testapp.forms.birthdate import BirthdateBoxForm, BirthdateCalendarForm, BirthdateInputForm, BirthdatePickerForm from testapp.forms.booking import BookingBoxForm, BookingCalendarForm, BookingPickerForm +from testapp.forms.cafeteria import CafeteriaCollection from testapp.forms.country import CountryForm from testapp.forms.county import CountyForm from testapp.forms.customer import CustomerCollection @@ -573,6 +574,9 @@ class CompleteForm(FormMixin, forms.Form): template_name='testapp/form-collection-no-buttons.html', ), name='simplecontact'), path('issue', IssueCollectionView.as_view(), name='issue'), + path('cafeteria', DemoFormCollectionView.as_view( + collection_class=CafeteriaCollection, + ), name='simplecontact'), path('customer', DemoFormCollectionView.as_view( collection_class=CustomerCollection, ), name='customer'),