Skip to content

Commit c390004

Browse files
committed
refactor(eligibility): use FormView instead of View
1 parent 7f333f2 commit c390004

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

benefits/eligibility/views.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django.template.response import TemplateResponse
88
from django.urls import reverse
99
from django.utils.decorators import decorator_from_middleware
10-
from django.views import View
10+
from django.views.generic import FormView
1111

1212
from benefits.routes import routes
1313
from benefits.core import recaptcha, session
@@ -19,46 +19,52 @@
1919
TEMPLATE_CONFIRM = "eligibility/confirm.html"
2020

2121

22-
class IndexView(AgencySessionRequiredMixin, RecaptchaEnabledMixin, View):
22+
class IndexView(AgencySessionRequiredMixin, RecaptchaEnabledMixin, FormView):
2323
"""View handler for the enrollment flow selection form."""
2424

2525
template_name = "eligibility/index.html"
26+
form_class = forms.EnrollmentFlowSelectionForm
2627

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
3033

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)
3137
# clear any prior OAuth token as the user is choosing their desired flow
3238
# this may or may not require OAuth, with a different set of scope/claims than what is already stored
3339
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)
3841

3942
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)
4245
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
6268

6369

6470
@decorator_from_middleware(AgencySessionRequired)

0 commit comments

Comments
 (0)