|
7 | 7 | from django.template.response import TemplateResponse
|
8 | 8 | from django.urls import reverse
|
9 | 9 | from django.utils.decorators import decorator_from_middleware
|
10 |
| -from django.views import View |
| 10 | +from django.views.generic import FormView |
11 | 11 |
|
12 | 12 | from benefits.routes import routes
|
13 | 13 | from benefits.core import recaptcha, session
|
|
19 | 19 | TEMPLATE_CONFIRM = "eligibility/confirm.html"
|
20 | 20 |
|
21 | 21 |
|
22 |
| -class IndexView(AgencySessionRequiredMixin, RecaptchaEnabledMixin, View): |
| 22 | +class IndexView(AgencySessionRequiredMixin, RecaptchaEnabledMixin, FormView): |
23 | 23 | """View handler for the enrollment flow selection form."""
|
24 | 24 |
|
25 | 25 | template_name = "eligibility/index.html"
|
| 26 | + form_class = forms.EnrollmentFlowSelectionForm |
26 | 27 |
|
27 |
| - def get(self, request, *args, **kwargs): |
28 |
| - agency = session.agency(request) |
29 |
| - session.update(request, eligible=False, origin=agency.index_url) |
| 28 | + def get_form_kwargs(self): |
| 29 | + """Return the keyword arguments for instantiating the form.""" |
| 30 | + kwargs = super().get_form_kwargs() |
| 31 | + kwargs["agency"] = self.agency |
| 32 | + return kwargs |
30 | 33 |
|
| 34 | + def get(self, request, *args, **kwargs): |
| 35 | + """Handle GET requests: initialize session and clear any prior OAuth tokens.""" |
| 36 | + session.update(request, eligible=False, origin=self.agency.index_url) |
31 | 37 | # clear any prior OAuth token as the user is choosing their desired flow
|
32 | 38 | # this may or may not require OAuth, with a different set of scope/claims than what is already stored
|
33 | 39 | session.logout(request)
|
34 |
| - |
35 |
| - context = {"form": forms.EnrollmentFlowSelectionForm(agency=agency)} |
36 |
| - context.update(agency.eligibility_index_context) |
37 |
| - return TemplateResponse(request, self.template_name, context) |
| 40 | + return super().get(request, *args, **kwargs) |
38 | 41 |
|
39 | 42 | def post(self, request, *args, **kwargs):
|
40 |
| - agency = session.agency(request) |
41 |
| - session.update(request, eligible=False, origin=agency.index_url) |
| 43 | + """Handle POST requests: initialize session before form processing.""" |
| 44 | + session.update(request, eligible=False, origin=self.agency.index_url) |
42 | 45 | session.logout(request)
|
43 |
| - |
44 |
| - form = forms.EnrollmentFlowSelectionForm(data=request.POST, agency=agency) |
45 |
| - |
46 |
| - if form.is_valid(): |
47 |
| - flow_id = form.cleaned_data.get("flow") |
48 |
| - flow = EnrollmentFlow.objects.get(id=flow_id) |
49 |
| - session.update(request, flow=flow) |
50 |
| - |
51 |
| - analytics.selected_flow(request, flow) |
52 |
| - |
53 |
| - eligibility_start = reverse(routes.ELIGIBILITY_START) |
54 |
| - return redirect(eligibility_start) |
55 |
| - else: |
56 |
| - context = {"form": form} |
57 |
| - # form was not valid, allow for correction/resubmission |
58 |
| - if recaptcha.has_error(form): |
59 |
| - messages.error(request, "Recaptcha failed. Please try again.") |
60 |
| - context.update(agency.eligibility_index_context) |
61 |
| - return TemplateResponse(request, self.template_name, context) |
| 46 | + return super().post(request, *args, **kwargs) |
| 47 | + |
| 48 | + def form_valid(self, form): |
| 49 | + """If the form is valid, set enrollment flow and redirect.""" |
| 50 | + flow_id = form.cleaned_data.get("flow") |
| 51 | + flow = EnrollmentFlow.objects.get(id=flow_id) |
| 52 | + session.update(self.request, flow=flow) |
| 53 | + |
| 54 | + analytics.selected_flow(self.request, flow) |
| 55 | + return redirect(reverse(routes.ELIGIBILITY_START)) |
| 56 | + |
| 57 | + def form_invalid(self, form): |
| 58 | + """If the form is invalid, display error messages.""" |
| 59 | + if recaptcha.has_error(form): |
| 60 | + messages.error(self.request, "Recaptcha failed. Please try again.") |
| 61 | + return super().form_invalid(form) |
| 62 | + |
| 63 | + def get_context_data(self, **kwargs): |
| 64 | + """Add agency-specific context data.""" |
| 65 | + context = super().get_context_data(**kwargs) |
| 66 | + context.update(self.agency.eligibility_index_context) |
| 67 | + return context |
62 | 68 |
|
63 | 69 |
|
64 | 70 | @decorator_from_middleware(AgencySessionRequired)
|
|
0 commit comments