-
Notifications
You must be signed in to change notification settings - Fork 9
/
forms.py
181 lines (138 loc) · 6.44 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
The eligibility application: Form definition for the eligibility verification flow.
"""
import logging
from django import forms
from django.utils.translation import gettext_lazy as _
from benefits.core import models, recaptcha, widgets
logger = logging.getLogger(__name__)
class EligibilityVerifierSelectionForm(forms.Form):
"""Form to capture eligibility verifier selection."""
action_url = "eligibility:index"
id = "form-verifier-selection"
method = "POST"
verifier = forms.ChoiceField(label="", widget=widgets.VerifierRadioSelect)
# sets label to empty string so the radio_select template can override the label style
submit_value = _("Choose this Benefit")
def __init__(self, agency: models.TransitAgency, *args, **kwargs):
super().__init__(*args, **kwargs)
verifiers = agency.eligibility_verifiers.filter(active=True)
self.classes = "col-lg-8"
# second element is not used since we render the whole label using selection_label_template,
# therefore set to None
self.fields["verifier"].choices = [(v.id, None) for v in verifiers]
self.fields["verifier"].widget.selection_label_templates = {v.id: v.selection_label_template for v in verifiers}
def clean(self):
if not recaptcha.verify(self.data):
raise forms.ValidationError("reCAPTCHA failed")
class EligibilityVerificationForm(forms.Form):
"""Form to collect eligibility verification details."""
action_url = "eligibility:confirm"
id = "form-eligibility-verification"
method = "POST"
submit_value = _("Check eligibility")
submitting_value = _("Checking")
_error_messages = {
"invalid": _("Check your input. The format looks wrong."),
"missing": _("This field is required."),
}
def __init__(
self,
title,
headline,
blurb,
name_label,
name_placeholder,
name_help_text,
sub_label,
sub_placeholder,
sub_help_text,
name_max_length=None,
sub_input_mode=None,
sub_max_length=None,
sub_pattern=None,
*args,
**kwargs,
):
"""Initialize a new EligibilityVerifier form.
Args:
title (str): The page (i.e. tab) title for the form's page.
headline (str): The <h1> on the form's page.
blurb (str): Intro <p> on the form's page.
name_label (str): Label for the name form field.
name_placeholder (str): Field placeholder for the name form field.
name_help_text (str): Extra help text for the name form field.
sub_label (str): Label for the sub form field.
sub_placeholder (str): Field placeholder for the sub form field.
sub_help_text (str): Extra help text for the sub form field.
name_max_length (int): The maximum length accepted for the 'name' API field before sending to this verifier
sub_input_mode (str): Input mode can be "numeric", "tel", "search", etc. to override default "text" keyboard on
mobile devices
sub_max_length (int): The maximum length accepted for the 'sub' API field before sending to this verifier
sub_pattern (str): A regular expression used to validate the 'sub' API field before sending to this verifier
Extra args and kwargs are passed through to the underlying django.forms.Form.
"""
super().__init__(auto_id=True, label_suffix="", *args, **kwargs)
self.title = title
self.headline = headline
self.blurb = blurb
self.classes = "col-lg-6"
sub_widget = widgets.FormControlTextInput(placeholder=sub_placeholder)
if sub_pattern:
sub_widget.attrs.update({"pattern": sub_pattern})
if sub_input_mode:
sub_widget.attrs.update({"inputmode": sub_input_mode})
if sub_max_length:
sub_widget.attrs.update({"maxlength": sub_max_length})
self.fields["sub"] = forms.CharField(
label=sub_label,
widget=sub_widget,
help_text=sub_help_text,
)
name_widget = widgets.FormControlTextInput(placeholder=name_placeholder)
if name_max_length:
name_widget.attrs.update({"maxlength": name_max_length})
self.fields["name"] = forms.CharField(label=name_label, widget=name_widget, help_text=name_help_text)
def clean(self):
if not recaptcha.verify(self.data):
raise forms.ValidationError("reCAPTCHA failed")
class MSTCourtesyCard(EligibilityVerificationForm):
"""EligibilityVerification form for the MST Courtesy Card."""
def __init__(self, *args, **kwargs):
super().__init__(
title=_("Agency card information"),
headline=_("Let’s see if we can confirm your eligibility."),
blurb=_("Please input your Courtesy Card number and last name below to confirm your eligibility."),
name_label=_("Last name (as it appears on Courtesy Card)"),
name_placeholder="Garcia",
name_help_text=_("We use this to help confirm your Courtesy Card."),
sub_label=_("MST Courtesy Card number"),
sub_help_text=_("This is a 5-digit number on the front and back of your card."),
sub_placeholder="12345",
name_max_length=255,
sub_input_mode="numeric",
sub_max_length=5,
sub_pattern=r"\d{5}",
*args,
**kwargs,
)
class SBMTDMobilityPass(EligibilityVerificationForm):
"""EligibilityVerification form for the SBMTD Mobility Pass."""
def __init__(self, *args, **kwargs):
super().__init__(
title=_("Agency card information"),
headline=_("Let’s see if we can confirm your eligibility."),
blurb=_("Please input your Mobility Pass number and last name below to confirm your eligibility."),
name_label=_("Last name (as it appears on Mobility Pass card)"),
name_placeholder="Garcia",
name_help_text=_("We use this to help confirm your Mobility Pass."),
sub_label=_("SBMTD Mobility Pass number"),
sub_help_text=_("This is a 4-digit number on the back of your card."),
sub_placeholder="1234",
name_max_length=255,
sub_input_mode="numeric",
sub_max_length=4,
sub_pattern=r"\d{4}",
*args,
**kwargs,
)