Skip to content

Commit a669ae7

Browse files
committed
Initial commit
1 parent 2882ce2 commit a669ae7

25 files changed

Lines changed: 1192 additions & 37 deletions

app/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from app.lib.template_filters import slugify
77
from flask import Flask
88
from jinja2 import ChoiceLoader, PackageLoader
9+
from tna_frontend_jinja.wtforms.helpers import WTFormsHelpers
910

1011

1112
def create_app(config_class):
@@ -75,6 +76,8 @@ def apply_extra_headers(response):
7576
]
7677
)
7778

79+
WTFormsHelpers(app)
80+
7881
app.add_template_filter(slugify)
7982

8083
@app.context_processor
@@ -93,10 +96,12 @@ def context_processor():
9396
feature={},
9497
)
9598

99+
from .forms import bp as forms_bp
96100
from .healthcheck import bp as healthcheck_bp
97101
from .main import bp as site_bp
98102

99103
app.register_blueprint(site_bp)
100104
app.register_blueprint(healthcheck_bp, url_prefix="/healthcheck")
105+
app.register_blueprint(forms_bp)
101106

102107
return app

app/forms/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from flask import Blueprint
2+
3+
bp = Blueprint("forms", __name__)
4+
5+
from app.forms import routes # noqa: E402,F401

app/forms/config/example.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)