We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Refactor start page context creation
I got a strange error on the Start page:

```
Traceback (most recent call last):
  File "/home/calitp/.local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/calitp/.local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/calitp/.local/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/calitp/.local/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/calitp/app/benefits/eligibility/views.py", line 127, in start
    ctx["media"].insert(0, auth_media)

Exception Type: UnboundLocalError at /eligibility/start
Exception Value: local variable 'auth_media' referenced before assignment
```

Maybe now that the media list refactor and the login button are merged into dev, we can clean up the logic and context creation for this view?

## Steps to recreate

1. From start page, click "Continue with Login.gov" button
2. Sign in with Login.gov account
3. Land on confirm page (DMV form)
4. Change URL manually to `/eligibility` (the verifier select page)
5. Select "Senior Discount Program", click "Continue"
6. 💥 

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `benefits/eligibility/views.py`
Content:
```
1 """
2 The eligibility application: view definitions for the eligibility verification flow.
3 """
4 from django.contrib import messages
5 from django.shortcuts import redirect
6 from django.template.response import TemplateResponse
7 from django.urls import reverse
8 from django.utils.decorators import decorator_from_middleware
9 from django.utils.translation import pgettext, gettext as _
10 
11 from benefits.core import middleware, recaptcha, session, viewmodels
12 from benefits.core.models import EligibilityVerifier
13 from benefits.core.views import PageTemplateResponse
14 from benefits.settings import OAUTH_CLIENT_NAME
15 from . import analytics, api, forms
16 
17 
18 @decorator_from_middleware(middleware.AgencySessionRequired)
19 def index(request):
20     """View handler for the eligibility verifier selection form."""
21 
22     session.update(request, eligibility_types=[], origin=reverse("eligibility:index"))
23     agency = session.agency(request)
24 
25     eligibility_start = reverse("eligibility:start")
26 
27     page = viewmodels.Page(
28         title=_("eligibility.pages.index.title"),
29         content_title=_("eligibility.pages.index.content_title"),
30         forms=forms.EligibilityVerifierSelectionForm(agency=agency),
31     )
32 
33     if request.method == "POST":
34         form = forms.EligibilityVerifierSelectionForm(data=request.POST, agency=agency)
35 
36         if form.is_valid():
37             verifier_id = form.cleaned_data.get("verifier")
38             verifier = EligibilityVerifier.objects.get(id=verifier_id)
39             session.update(request, verifier=verifier)
40 
41             response = redirect(eligibility_start)
42         else:
43             # form was not valid, allow for correction/resubmission
44             page.forms = [form]
45             response = PageTemplateResponse(request, page)
46     else:
47         if agency.eligibility_verifiers.count() == 1:
48             verifier = agency.eligibility_verifiers.first()
49             session.update(request, verifier=verifier)
50             response = redirect(eligibility_start)
51         else:
52             response = PageTemplateResponse(request, page)
53 
54     return response
55 
56 
57 @decorator_from_middleware(middleware.AgencySessionRequired)
58 @decorator_from_middleware(middleware.VerifierSessionRequired)
59 def start(request):
60     """View handler for the eligibility verification getting started screen."""
61 
62     session.update(request, eligibility_types=[], origin=reverse("eligibility:start"))
63     verifier = session.verifier(request)
64 
65     if verifier.requires_authentication and not session.auth(request):
66         if OAUTH_CLIENT_NAME is None:
67             raise Exception("EligibilityVerifier requires authentication, but OAUTH_CLIENT_NAME is None")
68 
69         auth_provider = verifier.auth_provider
70         button = viewmodels.Button.external(
71             text=_(auth_provider.sign_in_button_label),
72             url=reverse("oauth:login"),
73             id="login",
74         )
75         auth_media = dict(
76             icon=viewmodels.Icon("idscreencheck", pgettext("image alt text", "core.icons.idscreencheck")),
77             heading=_("eligibility.media.heading"),
78             details=_("eligibility.media.details"),
79             links=[
80                 viewmodels.Button.link(
81                     classes="btn-text btn-link",
82                     text=_("eligibility.media.link_text"),
83                     url=_("eligibility.media.link_url"),
84                     target="_blank",
85                     rel="noopener noreferrer",
86                 )
87             ],
88         )
89     else:
90         button = viewmodels.Button.primary(text=_("eligibility.buttons.continue"), url=reverse("eligibility:confirm"))
91 
92     page = viewmodels.Page(
93         title=_("eligibility.pages.start.title"),
94         noimage=True,
95         paragraphs=[_(verifier.start_blurb)],
96         button=button,
97     )
98 
99     ctx = page.context_dict()
100     ctx["title"] = _(verifier.start_content_title)
101     ctx["media"] = [
102         dict(
103             icon=viewmodels.Icon("idcardcheck", pgettext("image alt text", "core.icons.idcardcheck")),
104             heading=_(verifier.start_item_name),
105             details=_(verifier.start_item_description),
106         ),
107         dict(
108             icon=viewmodels.Icon("bankcardcheck", pgettext("image alt text", "core.icons.bankcardcheck")),
109             heading=_("eligibility.pages.start.items[1].title"),
110             details=_("eligibility.pages.start.items[1].text"),
111             links=[
112                 viewmodels.Button.link(
113                     classes="btn-text btn-link",
114                     text=_("eligibility.pages.start.items[1].button[0].link"),
115                     url=_("eligibility.pages.start.items[1].button[0].url"),
116                 ),
117                 viewmodels.Button.link(
118                     classes="btn-text btn-link",
119                     text=_("eligibility.pages.start.items[1].button[1].link"),
120                     url=_("eligibility.pages.start.items[1].button[1].url"),
121                 ),
122             ],
123         ),
124     ]
125 
126     if verifier.requires_authentication:
127         ctx["media"].insert(0, auth_media)
128 
129     return TemplateResponse(request, "eligibility/start.html", ctx)
130 
131 
132 @decorator_from_middleware(middleware.AgencySessionRequired)
133 @decorator_from_middleware(middleware.RateLimit)
134 @decorator_from_middleware(middleware.VerifierSessionRequired)
135 def confirm(request):
136     """View handler for the eligibility verification form."""
137 
138     verifier = session.verifier(request)
139 
140     page = viewmodels.Page(
141         title=_(verifier.form_title),
142         content_title=_(verifier.form_content_title),
143         paragraphs=[_(verifier.form_blurb)],
144         form=forms.EligibilityVerificationForm(auto_id=True, label_suffix="", verifier=verifier),
145         classes="text-lg-center",
146     )
147 
148     if request.method == "POST":
149         analytics.started_eligibility(request)
150 
151         form = forms.EligibilityVerificationForm(data=request.POST, verifier=verifier)
152         response = _verify(request, form)
153 
154         if response is None:
155             # form was not valid, allow for correction/resubmission
156             analytics.returned_error(request, form.errors)
157             page.forms = [form]
158             response = PageTemplateResponse(request, page)
159     elif session.eligible(request):
160         eligibility = session.eligibility(request)
161         response = verified(request, [eligibility.name])
162     else:
163         response = PageTemplateResponse(request, page)
164 
165     return response
166 
167 
168 def _verify(request, form):
169     """Helper calls the eligibility verification API with user input."""
170 
171     if not form.is_valid():
172         if recaptcha.has_error(form):
173             messages.error(request, "Recaptcha failed. Please try again.")
174         return None
175 
176     sub, name = form.cleaned_data.get("sub"), form.cleaned_data.get("name")
177 
178     agency = session.agency(request)
179     verifier = session.verifier(request)
180     client = api.Client(agency, verifier)
181 
182     response = client.verify(sub, name)
183 
184     if response.error and any(response.error):
185         form.add_api_errors(response.error)
186         return None
187     elif any(response.eligibility):
188         return verified(request, response.eligibility)
189     else:
190         return unverified(request)
191 
192 
193 @decorator_from_middleware(middleware.AgencySessionRequired)
194 def verified(request, verified_types):
195     """View handler for the verified eligibility page."""
196 
197     analytics.returned_success(request)
198 
199     enrollment_index = reverse("enrollment:index")
200     session.update(request, eligibility_types=verified_types, origin=enrollment_index)
201 
202     return redirect(enrollment_index)
203 
204 
205 @decorator_from_middleware(middleware.AgencySessionRequired)
206 @decorator_from_middleware(middleware.VerifierSessionRequired)
207 def unverified(request):
208     """View handler for the unverified eligibility page."""
209 
210     analytics.returned_fail(request)
211 
212     # tel: link to agency phone number
213     agency = session.agency(request)
214     buttons = viewmodels.Button.agency_contact_links(agency)
215 
216     verifier = session.verifier(request)
217 
218     page = viewmodels.Page(
219         title=_(verifier.unverified_title),
220         content_title=_(verifier.unverified_content_title),
221         icon=viewmodels.Icon("idcardquestion", pgettext("image alt text", "core.icons.idcardquestion")),
222         paragraphs=[_(verifier.unverified_blurb), _("eligibility.pages.unverified.p[1]")],
223         buttons=buttons,
224         classes="text-lg-center",
225     )
226 
227     return PageTemplateResponse(request, page)
228
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/benefits/eligibility/views.py b/benefits/eligibility/views.py
--- a/benefits/eligibility/views.py
+++ b/benefits/eligibility/views.py
@@ -62,43 +62,8 @@
     session.update(request, eligibility_types=[], origin=reverse("eligibility:start"))
     verifier = session.verifier(request)
 
-    if verifier.requires_authentication and not session.auth(request):
-        if OAUTH_CLIENT_NAME is None:
-            raise Exception("EligibilityVerifier requires authentication, but OAUTH_CLIENT_NAME is None")
-
-        auth_provider = verifier.auth_provider
-        button = viewmodels.Button.external(
-            text=_(auth_provider.sign_in_button_label),
-            url=reverse("oauth:login"),
-            id="login",
-        )
-        auth_media = dict(
-            icon=viewmodels.Icon("idscreencheck", pgettext("image alt text", "core.icons.idscreencheck")),
-            heading=_("eligibility.media.heading"),
-            details=_("eligibility.media.details"),
-            links=[
-                viewmodels.Button.link(
-                    classes="btn-text btn-link",
-                    text=_("eligibility.media.link_text"),
-                    url=_("eligibility.media.link_url"),
-                    target="_blank",
-                    rel="noopener noreferrer",
-                )
-            ],
-        )
-    else:
-        button = viewmodels.Button.primary(text=_("eligibility.buttons.continue"), url=reverse("eligibility:confirm"))
-
-    page = viewmodels.Page(
-        title=_("eligibility.pages.start.title"),
-        noimage=True,
-        paragraphs=[_(verifier.start_blurb)],
-        button=button,
-    )
-
-    ctx = page.context_dict()
-    ctx["title"] = _(verifier.start_content_title)
-    ctx["media"] = [
+    button = viewmodels.Button.primary(text=_("eligibility.buttons.continue"), url=reverse("eligibility:confirm"))
+    media = [
         dict(
             icon=viewmodels.Icon("idcardcheck", pgettext("image alt text", "core.icons.idcardcheck")),
             heading=_(verifier.start_item_name),
@@ -124,7 +89,44 @@
     ]
 
     if verifier.requires_authentication:
-        ctx["media"].insert(0, auth_media)
+        if OAUTH_CLIENT_NAME is None:
+            raise Exception("EligibilityVerifier requires authentication, but OAUTH_CLIENT_NAME is None")
+
+        media.insert(
+            0,
+            dict(
+                icon=viewmodels.Icon("idscreencheck", pgettext("image alt text", "core.icons.idscreencheck")),
+                heading=_("eligibility.media.heading"),
+                details=_("eligibility.media.details"),
+                links=[
+                    viewmodels.Button.link(
+                        classes="btn-text btn-link",
+                        text=_("eligibility.media.link_text"),
+                        url=_("eligibility.media.link_url"),
+                        target="_blank",
+                        rel="noopener noreferrer",
+                    )
+                ],
+            ),
+        )
+
+        if not session.auth(request):
+            button = viewmodels.Button.external(
+                text=_(verifier.auth_provider.sign_in_button_label),
+                url=reverse("oauth:login"),
+                id="login",
+            )
+
+    page = viewmodels.Page(
+        title=_("eligibility.pages.start.title"),
+        noimage=True,
+        paragraphs=[_(verifier.start_blurb)],
+        button=button,
+    )
+
+    ctx = page.context_dict()
+    ctx["title"] = _(verifier.start_content_title)
+    ctx["media"] = media
 
     return TemplateResponse(request, "eligibility/start.html", ctx)

{"golden_diff": "diff --git a/benefits/eligibility/views.py b/benefits/eligibility/views.py\n--- a/benefits/eligibility/views.py\n+++ b/benefits/eligibility/views.py\n@@ -62,43 +62,8 @@\n     session.update(request, eligibility_types=[], origin=reverse(\"eligibility:start\"))\n     verifier = session.verifier(request)\n \n-    if verifier.requires_authentication and not session.auth(request):\n-        if OAUTH_CLIENT_NAME is None:\n-            raise Exception(\"EligibilityVerifier requires authentication, but OAUTH_CLIENT_NAME is None\")\n-\n-        auth_provider = verifier.auth_provider\n-        button = viewmodels.Button.external(\n-            text=_(auth_provider.sign_in_button_label),\n-            url=reverse(\"oauth:login\"),\n-            id=\"login\",\n-        )\n-        auth_media = dict(\n-            icon=viewmodels.Icon(\"idscreencheck\", pgettext(\"image alt text\", \"core.icons.idscreencheck\")),\n-            heading=_(\"eligibility.media.heading\"),\n-            details=_(\"eligibility.media.details\"),\n-            links=[\n-                viewmodels.Button.link(\n-                    classes=\"btn-text btn-link\",\n-                    text=_(\"eligibility.media.link_text\"),\n-                    url=_(\"eligibility.media.link_url\"),\n-                    target=\"_blank\",\n-                    rel=\"noopener noreferrer\",\n-                )\n-            ],\n-        )\n-    else:\n-        button = viewmodels.Button.primary(text=_(\"eligibility.buttons.continue\"), url=reverse(\"eligibility:confirm\"))\n-\n-    page = viewmodels.Page(\n-        title=_(\"eligibility.pages.start.title\"),\n-        noimage=True,\n-        paragraphs=[_(verifier.start_blurb)],\n-        button=button,\n-    )\n-\n-    ctx = page.context_dict()\n-    ctx[\"title\"] = _(verifier.start_content_title)\n-    ctx[\"media\"] = [\n+    button = viewmodels.Button.primary(text=_(\"eligibility.buttons.continue\"), url=reverse(\"eligibility:confirm\"))\n+    media = [\n         dict(\n             icon=viewmodels.Icon(\"idcardcheck\", pgettext(\"image alt text\", \"core.icons.idcardcheck\")),\n             heading=_(verifier.start_item_name),\n@@ -124,7 +89,44 @@\n     ]\n \n     if verifier.requires_authentication:\n-        ctx[\"media\"].insert(0, auth_media)\n+        if OAUTH_CLIENT_NAME is None:\n+            raise Exception(\"EligibilityVerifier requires authentication, but OAUTH_CLIENT_NAME is None\")\n+\n+        media.insert(\n+            0,\n+            dict(\n+                icon=viewmodels.Icon(\"idscreencheck\", pgettext(\"image alt text\", \"core.icons.idscreencheck\")),\n+                heading=_(\"eligibility.media.heading\"),\n+                details=_(\"eligibility.media.details\"),\n+                links=[\n+                    viewmodels.Button.link(\n+                        classes=\"btn-text btn-link\",\n+                        text=_(\"eligibility.media.link_text\"),\n+                        url=_(\"eligibility.media.link_url\"),\n+                        target=\"_blank\",\n+                        rel=\"noopener noreferrer\",\n+                    )\n+                ],\n+            ),\n+        )\n+\n+        if not session.auth(request):\n+            button = viewmodels.Button.external(\n+                text=_(verifier.auth_provider.sign_in_button_label),\n+                url=reverse(\"oauth:login\"),\n+                id=\"login\",\n+            )\n+\n+    page = viewmodels.Page(\n+        title=_(\"eligibility.pages.start.title\"),\n+        noimage=True,\n+        paragraphs=[_(verifier.start_blurb)],\n+        button=button,\n+    )\n+\n+    ctx = page.context_dict()\n+    ctx[\"title\"] = _(verifier.start_content_title)\n+    ctx[\"media\"] = media\n \n     return TemplateResponse(request, \"eligibility/start.html\", ctx)\n", "issue": "Refactor start page context creation\nI got a strange error on the Start page:\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"/home/calitp/.local/lib/python3.9/site-packages/django/core/handlers/exception.py\", line 47, in inner\r\n    response = get_response(request)\r\n  File \"/home/calitp/.local/lib/python3.9/site-packages/django/core/handlers/base.py\", line 181, in _get_response\r\n    response = wrapped_callback(request, *callback_args, **callback_kwargs)\r\n  File \"/home/calitp/.local/lib/python3.9/site-packages/django/utils/decorators.py\", line 130, in _wrapped_view\r\n    response = view_func(request, *args, **kwargs)\r\n  File \"/home/calitp/.local/lib/python3.9/site-packages/django/utils/decorators.py\", line 130, in _wrapped_view\r\n    response = view_func(request, *args, **kwargs)\r\n  File \"/home/calitp/app/benefits/eligibility/views.py\", line 127, in start\r\n    ctx[\"media\"].insert(0, auth_media)\r\n\r\nException Type: UnboundLocalError at /eligibility/start\r\nException Value: local variable 'auth_media' referenced before assignment\r\n```\r\n\r\nMaybe now that the media list refactor and the login button are merged into dev, we can clean up the logic and context creation for this view?\r\n\r\n## Steps to recreate\r\n\r\n1. From start page, click \"Continue with Login.gov\" button\r\n2. Sign in with Login.gov account\r\n3. Land on confirm page (DMV form)\r\n4. Change URL manually to `/eligibility` (the verifier select page)\r\n5. Select \"Senior Discount Program\", click \"Continue\"\r\n6. \ud83d\udca5 \n", "before_files": [{"content": "\"\"\"\nThe eligibility application: view definitions for the eligibility verification flow.\n\"\"\"\nfrom django.contrib import messages\nfrom django.shortcuts import redirect\nfrom django.template.response import TemplateResponse\nfrom django.urls import reverse\nfrom django.utils.decorators import decorator_from_middleware\nfrom django.utils.translation import pgettext, gettext as _\n\nfrom benefits.core import middleware, recaptcha, session, viewmodels\nfrom benefits.core.models import EligibilityVerifier\nfrom benefits.core.views import PageTemplateResponse\nfrom benefits.settings import OAUTH_CLIENT_NAME\nfrom . import analytics, api, forms\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\ndef index(request):\n    \"\"\"View handler for the eligibility verifier selection form.\"\"\"\n\n    session.update(request, eligibility_types=[], origin=reverse(\"eligibility:index\"))\n    agency = session.agency(request)\n\n    eligibility_start = reverse(\"eligibility:start\")\n\n    page = viewmodels.Page(\n        title=_(\"eligibility.pages.index.title\"),\n        content_title=_(\"eligibility.pages.index.content_title\"),\n        forms=forms.EligibilityVerifierSelectionForm(agency=agency),\n    )\n\n    if request.method == \"POST\":\n        form = forms.EligibilityVerifierSelectionForm(data=request.POST, agency=agency)\n\n        if form.is_valid():\n            verifier_id = form.cleaned_data.get(\"verifier\")\n            verifier = EligibilityVerifier.objects.get(id=verifier_id)\n            session.update(request, verifier=verifier)\n\n            response = redirect(eligibility_start)\n        else:\n            # form was not valid, allow for correction/resubmission\n            page.forms = [form]\n            response = PageTemplateResponse(request, page)\n    else:\n        if agency.eligibility_verifiers.count() == 1:\n            verifier = agency.eligibility_verifiers.first()\n            session.update(request, verifier=verifier)\n            response = redirect(eligibility_start)\n        else:\n            response = PageTemplateResponse(request, page)\n\n    return response\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\n@decorator_from_middleware(middleware.VerifierSessionRequired)\ndef start(request):\n    \"\"\"View handler for the eligibility verification getting started screen.\"\"\"\n\n    session.update(request, eligibility_types=[], origin=reverse(\"eligibility:start\"))\n    verifier = session.verifier(request)\n\n    if verifier.requires_authentication and not session.auth(request):\n        if OAUTH_CLIENT_NAME is None:\n            raise Exception(\"EligibilityVerifier requires authentication, but OAUTH_CLIENT_NAME is None\")\n\n        auth_provider = verifier.auth_provider\n        button = viewmodels.Button.external(\n            text=_(auth_provider.sign_in_button_label),\n            url=reverse(\"oauth:login\"),\n            id=\"login\",\n        )\n        auth_media = dict(\n            icon=viewmodels.Icon(\"idscreencheck\", pgettext(\"image alt text\", \"core.icons.idscreencheck\")),\n            heading=_(\"eligibility.media.heading\"),\n            details=_(\"eligibility.media.details\"),\n            links=[\n                viewmodels.Button.link(\n                    classes=\"btn-text btn-link\",\n                    text=_(\"eligibility.media.link_text\"),\n                    url=_(\"eligibility.media.link_url\"),\n                    target=\"_blank\",\n                    rel=\"noopener noreferrer\",\n                )\n            ],\n        )\n    else:\n        button = viewmodels.Button.primary(text=_(\"eligibility.buttons.continue\"), url=reverse(\"eligibility:confirm\"))\n\n    page = viewmodels.Page(\n        title=_(\"eligibility.pages.start.title\"),\n        noimage=True,\n        paragraphs=[_(verifier.start_blurb)],\n        button=button,\n    )\n\n    ctx = page.context_dict()\n    ctx[\"title\"] = _(verifier.start_content_title)\n    ctx[\"media\"] = [\n        dict(\n            icon=viewmodels.Icon(\"idcardcheck\", pgettext(\"image alt text\", \"core.icons.idcardcheck\")),\n            heading=_(verifier.start_item_name),\n            details=_(verifier.start_item_description),\n        ),\n        dict(\n            icon=viewmodels.Icon(\"bankcardcheck\", pgettext(\"image alt text\", \"core.icons.bankcardcheck\")),\n            heading=_(\"eligibility.pages.start.items[1].title\"),\n            details=_(\"eligibility.pages.start.items[1].text\"),\n            links=[\n                viewmodels.Button.link(\n                    classes=\"btn-text btn-link\",\n                    text=_(\"eligibility.pages.start.items[1].button[0].link\"),\n                    url=_(\"eligibility.pages.start.items[1].button[0].url\"),\n                ),\n                viewmodels.Button.link(\n                    classes=\"btn-text btn-link\",\n                    text=_(\"eligibility.pages.start.items[1].button[1].link\"),\n                    url=_(\"eligibility.pages.start.items[1].button[1].url\"),\n                ),\n            ],\n        ),\n    ]\n\n    if verifier.requires_authentication:\n        ctx[\"media\"].insert(0, auth_media)\n\n    return TemplateResponse(request, \"eligibility/start.html\", ctx)\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\n@decorator_from_middleware(middleware.RateLimit)\n@decorator_from_middleware(middleware.VerifierSessionRequired)\ndef confirm(request):\n    \"\"\"View handler for the eligibility verification form.\"\"\"\n\n    verifier = session.verifier(request)\n\n    page = viewmodels.Page(\n        title=_(verifier.form_title),\n        content_title=_(verifier.form_content_title),\n        paragraphs=[_(verifier.form_blurb)],\n        form=forms.EligibilityVerificationForm(auto_id=True, label_suffix=\"\", verifier=verifier),\n        classes=\"text-lg-center\",\n    )\n\n    if request.method == \"POST\":\n        analytics.started_eligibility(request)\n\n        form = forms.EligibilityVerificationForm(data=request.POST, verifier=verifier)\n        response = _verify(request, form)\n\n        if response is None:\n            # form was not valid, allow for correction/resubmission\n            analytics.returned_error(request, form.errors)\n            page.forms = [form]\n            response = PageTemplateResponse(request, page)\n    elif session.eligible(request):\n        eligibility = session.eligibility(request)\n        response = verified(request, [eligibility.name])\n    else:\n        response = PageTemplateResponse(request, page)\n\n    return response\n\n\ndef _verify(request, form):\n    \"\"\"Helper calls the eligibility verification API with user input.\"\"\"\n\n    if not form.is_valid():\n        if recaptcha.has_error(form):\n            messages.error(request, \"Recaptcha failed. Please try again.\")\n        return None\n\n    sub, name = form.cleaned_data.get(\"sub\"), form.cleaned_data.get(\"name\")\n\n    agency = session.agency(request)\n    verifier = session.verifier(request)\n    client = api.Client(agency, verifier)\n\n    response = client.verify(sub, name)\n\n    if response.error and any(response.error):\n        form.add_api_errors(response.error)\n        return None\n    elif any(response.eligibility):\n        return verified(request, response.eligibility)\n    else:\n        return unverified(request)\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\ndef verified(request, verified_types):\n    \"\"\"View handler for the verified eligibility page.\"\"\"\n\n    analytics.returned_success(request)\n\n    enrollment_index = reverse(\"enrollment:index\")\n    session.update(request, eligibility_types=verified_types, origin=enrollment_index)\n\n    return redirect(enrollment_index)\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\n@decorator_from_middleware(middleware.VerifierSessionRequired)\ndef unverified(request):\n    \"\"\"View handler for the unverified eligibility page.\"\"\"\n\n    analytics.returned_fail(request)\n\n    # tel: link to agency phone number\n    agency = session.agency(request)\n    buttons = viewmodels.Button.agency_contact_links(agency)\n\n    verifier = session.verifier(request)\n\n    page = viewmodels.Page(\n        title=_(verifier.unverified_title),\n        content_title=_(verifier.unverified_content_title),\n        icon=viewmodels.Icon(\"idcardquestion\", pgettext(\"image alt text\", \"core.icons.idcardquestion\")),\n        paragraphs=[_(verifier.unverified_blurb), _(\"eligibility.pages.unverified.p[1]\")],\n        buttons=buttons,\n        classes=\"text-lg-center\",\n    )\n\n    return PageTemplateResponse(request, page)\n", "path": "benefits/eligibility/views.py"}], "after_files": [{"content": "\"\"\"\nThe eligibility application: view definitions for the eligibility verification flow.\n\"\"\"\nfrom django.contrib import messages\nfrom django.shortcuts import redirect\nfrom django.template.response import TemplateResponse\nfrom django.urls import reverse\nfrom django.utils.decorators import decorator_from_middleware\nfrom django.utils.translation import pgettext, gettext as _\n\nfrom benefits.core import middleware, recaptcha, session, viewmodels\nfrom benefits.core.models import EligibilityVerifier\nfrom benefits.core.views import PageTemplateResponse\nfrom benefits.settings import OAUTH_CLIENT_NAME\nfrom . import analytics, api, forms\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\ndef index(request):\n    \"\"\"View handler for the eligibility verifier selection form.\"\"\"\n\n    session.update(request, eligibility_types=[], origin=reverse(\"eligibility:index\"))\n    agency = session.agency(request)\n\n    eligibility_start = reverse(\"eligibility:start\")\n\n    page = viewmodels.Page(\n        title=_(\"eligibility.pages.index.title\"),\n        content_title=_(\"eligibility.pages.index.content_title\"),\n        forms=forms.EligibilityVerifierSelectionForm(agency=agency),\n    )\n\n    if request.method == \"POST\":\n        form = forms.EligibilityVerifierSelectionForm(data=request.POST, agency=agency)\n\n        if form.is_valid():\n            verifier_id = form.cleaned_data.get(\"verifier\")\n            verifier = EligibilityVerifier.objects.get(id=verifier_id)\n            session.update(request, verifier=verifier)\n\n            response = redirect(eligibility_start)\n        else:\n            # form was not valid, allow for correction/resubmission\n            page.forms = [form]\n            response = PageTemplateResponse(request, page)\n    else:\n        if agency.eligibility_verifiers.count() == 1:\n            verifier = agency.eligibility_verifiers.first()\n            session.update(request, verifier=verifier)\n            response = redirect(eligibility_start)\n        else:\n            response = PageTemplateResponse(request, page)\n\n    return response\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\n@decorator_from_middleware(middleware.VerifierSessionRequired)\ndef start(request):\n    \"\"\"View handler for the eligibility verification getting started screen.\"\"\"\n\n    session.update(request, eligibility_types=[], origin=reverse(\"eligibility:start\"))\n    verifier = session.verifier(request)\n\n    button = viewmodels.Button.primary(text=_(\"eligibility.buttons.continue\"), url=reverse(\"eligibility:confirm\"))\n    media = [\n        dict(\n            icon=viewmodels.Icon(\"idcardcheck\", pgettext(\"image alt text\", \"core.icons.idcardcheck\")),\n            heading=_(verifier.start_item_name),\n            details=_(verifier.start_item_description),\n        ),\n        dict(\n            icon=viewmodels.Icon(\"bankcardcheck\", pgettext(\"image alt text\", \"core.icons.bankcardcheck\")),\n            heading=_(\"eligibility.pages.start.items[1].title\"),\n            details=_(\"eligibility.pages.start.items[1].text\"),\n            links=[\n                viewmodels.Button.link(\n                    classes=\"btn-text btn-link\",\n                    text=_(\"eligibility.pages.start.items[1].button[0].link\"),\n                    url=_(\"eligibility.pages.start.items[1].button[0].url\"),\n                ),\n                viewmodels.Button.link(\n                    classes=\"btn-text btn-link\",\n                    text=_(\"eligibility.pages.start.items[1].button[1].link\"),\n                    url=_(\"eligibility.pages.start.items[1].button[1].url\"),\n                ),\n            ],\n        ),\n    ]\n\n    if verifier.requires_authentication:\n        if OAUTH_CLIENT_NAME is None:\n            raise Exception(\"EligibilityVerifier requires authentication, but OAUTH_CLIENT_NAME is None\")\n\n        media.insert(\n            0,\n            dict(\n                icon=viewmodels.Icon(\"idscreencheck\", pgettext(\"image alt text\", \"core.icons.idscreencheck\")),\n                heading=_(\"eligibility.media.heading\"),\n                details=_(\"eligibility.media.details\"),\n                links=[\n                    viewmodels.Button.link(\n                        classes=\"btn-text btn-link\",\n                        text=_(\"eligibility.media.link_text\"),\n                        url=_(\"eligibility.media.link_url\"),\n                        target=\"_blank\",\n                        rel=\"noopener noreferrer\",\n                    )\n                ],\n            ),\n        )\n\n        if not session.auth(request):\n            button = viewmodels.Button.external(\n                text=_(verifier.auth_provider.sign_in_button_label),\n                url=reverse(\"oauth:login\"),\n                id=\"login\",\n            )\n\n    page = viewmodels.Page(\n        title=_(\"eligibility.pages.start.title\"),\n        noimage=True,\n        paragraphs=[_(verifier.start_blurb)],\n        button=button,\n    )\n\n    ctx = page.context_dict()\n    ctx[\"title\"] = _(verifier.start_content_title)\n    ctx[\"media\"] = media\n\n    return TemplateResponse(request, \"eligibility/start.html\", ctx)\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\n@decorator_from_middleware(middleware.RateLimit)\n@decorator_from_middleware(middleware.VerifierSessionRequired)\ndef confirm(request):\n    \"\"\"View handler for the eligibility verification form.\"\"\"\n\n    verifier = session.verifier(request)\n\n    page = viewmodels.Page(\n        title=_(verifier.form_title),\n        content_title=_(verifier.form_content_title),\n        paragraphs=[_(verifier.form_blurb)],\n        form=forms.EligibilityVerificationForm(auto_id=True, label_suffix=\"\", verifier=verifier),\n        classes=\"text-lg-center\",\n    )\n\n    if request.method == \"POST\":\n        analytics.started_eligibility(request)\n\n        form = forms.EligibilityVerificationForm(data=request.POST, verifier=verifier)\n        response = _verify(request, form)\n\n        if response is None:\n            # form was not valid, allow for correction/resubmission\n            analytics.returned_error(request, form.errors)\n            page.forms = [form]\n            response = PageTemplateResponse(request, page)\n    elif session.eligible(request):\n        eligibility = session.eligibility(request)\n        response = verified(request, [eligibility.name])\n    else:\n        response = PageTemplateResponse(request, page)\n\n    return response\n\n\ndef _verify(request, form):\n    \"\"\"Helper calls the eligibility verification API with user input.\"\"\"\n\n    if not form.is_valid():\n        if recaptcha.has_error(form):\n            messages.error(request, \"Recaptcha failed. Please try again.\")\n        return None\n\n    sub, name = form.cleaned_data.get(\"sub\"), form.cleaned_data.get(\"name\")\n\n    agency = session.agency(request)\n    verifier = session.verifier(request)\n    client = api.Client(agency, verifier)\n\n    response = client.verify(sub, name)\n\n    if response.error and any(response.error):\n        form.add_api_errors(response.error)\n        return None\n    elif any(response.eligibility):\n        return verified(request, response.eligibility)\n    else:\n        return unverified(request)\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\ndef verified(request, verified_types):\n    \"\"\"View handler for the verified eligibility page.\"\"\"\n\n    analytics.returned_success(request)\n\n    enrollment_index = reverse(\"enrollment:index\")\n    session.update(request, eligibility_types=verified_types, origin=enrollment_index)\n\n    return redirect(enrollment_index)\n\n\n@decorator_from_middleware(middleware.AgencySessionRequired)\n@decorator_from_middleware(middleware.VerifierSessionRequired)\ndef unverified(request):\n    \"\"\"View handler for the unverified eligibility page.\"\"\"\n\n    analytics.returned_fail(request)\n\n    # tel: link to agency phone number\n    agency = session.agency(request)\n    buttons = viewmodels.Button.agency_contact_links(agency)\n\n    verifier = session.verifier(request)\n\n    page = viewmodels.Page(\n        title=_(verifier.unverified_title),\n        content_title=_(verifier.unverified_content_title),\n        icon=viewmodels.Icon(\"idcardquestion\", pgettext(\"image alt text\", \"core.icons.idcardquestion\")),\n        paragraphs=[_(verifier.unverified_blurb), _(\"eligibility.pages.unverified.p[1]\")],\n        buttons=buttons,\n        classes=\"text-lg-center\",\n    )\n\n    return PageTemplateResponse(request, page)\n", "path": "benefits/eligibility/views.py"}]}