Use shorter IDs for form steps and separate sidebar titles

This commit is contained in:
Neil Martinsen-Burrell 2022-10-28 10:32:53 -05:00
parent 2f463b810b
commit f8588ebe24
No known key found for this signature in database
GPG key ID: 6A3C818CC10D0184
4 changed files with 29 additions and 10 deletions

View file

@ -49,20 +49,25 @@ class ContactForm(forms.Form):
street_address = forms.CharField(label="Street address") street_address = forms.CharField(label="Street address")
ORGANIZATION_TITLE = "About your organization"
CONTACT_TITLE = "Your organization's contact information"
# List of forms in our wizard. Each entry is a tuple of a name and a form # List of forms in our wizard. Each entry is a tuple of a name and a form
# subclass # subclass
FORMS = [ FORMS = [
(ORGANIZATION_TITLE, OrganizationForm), ("organization", OrganizationForm),
(CONTACT_TITLE, ContactForm), ("contact", ContactForm),
] ]
# Dict to match up the right template with the right step. Keys here must # Dict to match up the right template with the right step. Keys here must
# match the first elements of the tuples above # match the first elements of the tuples in FORMS
TEMPLATES = { TEMPLATES = {
ORGANIZATION_TITLE: "application_organization.html", "organization": "application_organization.html",
CONTACT_TITLE: "application_contact.html", "contact": "application_contact.html",
}
# We need to pass our page titles as context to the templates, indexed
# by the step names
TITLES = {
"organization": "About your organization",
"contact": "Your organization's contact information",
} }
@ -85,5 +90,11 @@ class ApplicationWizard(LoginRequiredMixin, NamedUrlSessionWizardView):
""" """
return [TEMPLATES[self.steps.current]] return [TEMPLATES[self.steps.current]]
def get_context_data(self, form, **kwargs):
"""Add title information to the context for all steps."""
context = super().get_context_data(form=form, **kwargs)
context["form_titles"] = TITLES
return context
def done(self, form_list, **kwargs): def done(self, form_list, **kwargs):
logger.info("Application form submitted.") logger.info("Application form submitted.")

View file

@ -6,10 +6,10 @@
{% if forloop.counter <= wizard.steps.step1 %} {% if forloop.counter <= wizard.steps.step1 %}
<a href="{% url wizard.url_name step=this_step %}" <a href="{% url wizard.url_name step=this_step %}"
{% if this_step == wizard.steps.current %}class="usa-current"{% endif%}> {% if this_step == wizard.steps.current %}class="usa-current"{% endif%}>
{{ this_step }} {{ form_titles|get_item:this_step }}
</a> </a>
{% else %} {% else %}
{{ this_step }} {{ form_titles|get_item:this_step }}
{% endif %} {% endif %}
</li> </li>
{% endfor %} {% endfor %}

View file

@ -0,0 +1,8 @@
"""Custom template tags to make our lives easier."""
from django.template.defaulttags import register
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)

View file

@ -90,7 +90,7 @@ class FormTests(TestWithUser, WebTest):
# 302 redirect to the first form # 302 redirect to the first form
page = self.app.get(reverse("application")).follow() page = self.app.get(reverse("application")).follow()
form = page.form form = page.form
form["About your organization-organization_type"] = "Federal" form["organization-organization_type"] = "Federal"
result = page.form.submit().follow() result = page.form.submit().follow()
# Got the next form page # Got the next form page
self.assertIn("contact information", result) self.assertIn("contact information", result)