|
| 1 | +from app.forms.models import FormFlow |
| 2 | +from app.forms.parts import ( |
| 3 | + AddressForm, |
| 4 | + PizzaBrand, |
| 5 | + PizzaOrChocolate, |
| 6 | + PizzaToppings, |
| 7 | + TypeOfChocolate, |
| 8 | +) |
| 9 | + |
| 10 | +# Put everything together in a FormPages instance so we can handle all the logic |
| 11 | +example_form_flow = FormFlow(slug="example") |
| 12 | + |
| 13 | +# Set up all the required pages |
| 14 | +pizza_or_chocolate = example_form_flow.create_starting_page( |
| 15 | + name="Pizza or Chocolate", slug="pizza-or-chocolate", form=PizzaOrChocolate |
| 16 | +) |
| 17 | +neither_page = example_form_flow.create_page( |
| 18 | + name="Neither", |
| 19 | + slug="neither", |
| 20 | + description="You selected neither pizza nor chocolate.", |
| 21 | + template="forms/example/neither.html", |
| 22 | +) |
| 23 | +pizza_topping = example_form_flow.create_page( |
| 24 | + name="Pizza toppings", slug="pizza-topping", form=PizzaToppings |
| 25 | +) |
| 26 | +pizza_brand = example_form_flow.create_page( |
| 27 | + name="Pizza brand", slug="pizza-brand", form=PizzaBrand |
| 28 | +) |
| 29 | +type_of_chocolate = example_form_flow.create_page( |
| 30 | + name="Type of Chocolate", slug="type-of-chocolate", form=TypeOfChocolate |
| 31 | +) |
| 32 | +address = example_form_flow.create_page( |
| 33 | + name="Enter your address", |
| 34 | + slug="address", |
| 35 | + description="We need this to deliver your pizza or chocolate.", |
| 36 | + form=AddressForm, |
| 37 | +) |
| 38 | +final_page = example_form_flow.create_page( |
| 39 | + name="Final Page", |
| 40 | + slug="final-page", |
| 41 | + description="This is the final page of the flow.", |
| 42 | + template="forms/final.html", |
| 43 | +) |
| 44 | + |
| 45 | +# Handle the flow logic for different options |
| 46 | +pizza_or_chocolate.redirect_when_complete( |
| 47 | + page=pizza_topping, |
| 48 | + when=("food", "pizza"), |
| 49 | +).redirect_when_complete( |
| 50 | + page=type_of_chocolate, |
| 51 | + when=("food", "chocolate"), |
| 52 | +).redirect_when_complete( |
| 53 | + page=neither_page, |
| 54 | + when=("food", "neither"), |
| 55 | +).redirect_when_complete( |
| 56 | + url="https://www.reddit.com/r/catpictures/", |
| 57 | + when=("food", "cats"), |
| 58 | +) |
| 59 | + |
| 60 | +# Require certain responses before proceeding else redirect to that page |
| 61 | +pizza_topping.require_response( |
| 62 | + pizza_or_chocolate, "food", "pizza" |
| 63 | +).redirect_when_complete(page=pizza_brand) |
| 64 | +type_of_chocolate.require_response( |
| 65 | + pizza_or_chocolate, "food", "chocolate" |
| 66 | +).redirect_when_complete(page=address) |
| 67 | + |
| 68 | +# Require the completion of previous pages |
| 69 | +pizza_brand.require_completion_of(pizza_topping).redirect_when_complete( |
| 70 | + url="https://www.dominos.co.uk/", |
| 71 | + condition=lambda form_data: form_data.get("brand") != "dominos", |
| 72 | +).redirect_when_complete(page=address) |
| 73 | + |
| 74 | +address.require_completion_of_any( |
| 75 | + [pizza_brand, type_of_chocolate], pizza_or_chocolate |
| 76 | +).redirect_when_complete(page=final_page) |
| 77 | + |
| 78 | +# Require completion of any of the previous pages |
| 79 | +final_page.require_completion_of_any( |
| 80 | + [pizza_brand, type_of_chocolate], pizza_or_chocolate |
| 81 | +) |
| 82 | + |
| 83 | +forms = { |
| 84 | + "example": example_form_flow, |
| 85 | +} |
0 commit comments