Coverage for benefits/core/views.py: 100%
68 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-08 16:26 +0000
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-08 16:26 +0000
1"""
2The core application: view definition for the root of the webapp.
3"""
5from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound, HttpResponseServerError
6from django.shortcuts import redirect
7from django.template import loader
8from django.template.response import TemplateResponse
9from django.utils.decorators import method_decorator
10from django.views.generic import TemplateView
12from benefits.routes import routes
13from . import models, session
14from .middleware import pageview_decorator, index_or_agencyindex_origin_decorator, user_error
16TEMPLATE_AGENCY = "core/index--agency-base.html"
17TEMPLATE_HELP = "core/help.html"
18TEMPLATE_LOGGED_OUT = "core/logged-out.html"
20TEMPLATE_BAD_REQUEST = "400.html"
21TEMPLATE_NOT_FOUND = "404.html"
22TEMPLATE_SERVER_ERROR = "500.html"
25class IndexView(TemplateView):
26 """View handler for the main entry page."""
28 template_name = "core/index.html"
30 @method_decorator(pageview_decorator)
31 def get(self, request, *args, **kwargs):
32 session.reset(request)
33 return super().get(request, *args, **kwargs)
36@pageview_decorator
37def agency_index(request, agency: models.TransitAgency):
38 """View handler for an agency entry page."""
39 session.reset(request)
40 session.update(request, agency=agency, origin=agency.index_url)
42 return TemplateResponse(request, "core/index--agency.html", agency.index_context)
45@pageview_decorator
46def agency_eligibility_index(request, agency: models.TransitAgency):
47 """View handler forwards the request to the agency's Eligibility Index (e.g. flow selection) page."""
48 session.reset(request)
49 session.update(request, agency=agency, origin=agency.index_url)
51 return redirect(routes.ELIGIBILITY_INDEX)
54@pageview_decorator
55def agency_public_key(request, agency: models.TransitAgency):
56 """View handler returns an agency's public key as plain text."""
57 return HttpResponse(agency.eligibility_api_public_key_data, content_type="text/plain")
60@pageview_decorator
61def agency_card(request, agency: models.TransitAgency):
62 """View handler forwards the request to the agency's Agency Card (e.g. Eligibility API) flow, or returns a user error."""
63 session.reset(request)
64 session.update(request, agency=agency, origin=agency.index_url)
66 eligibility_api_flow = (
67 agency.enrollment_flows.exclude(eligibility_api_url="").exclude(eligibility_form_class="").order_by("id").last()
68 )
70 if eligibility_api_flow:
71 session.update(request, flow=eligibility_api_flow)
72 return redirect(routes.ELIGIBILITY_CONFIRM)
73 else:
74 return user_error(request)
77@pageview_decorator
78def help(request):
79 """View handler for the help page."""
80 return TemplateResponse(request, TEMPLATE_HELP)
83@pageview_decorator
84@index_or_agencyindex_origin_decorator
85def bad_request(request, exception, template_name=TEMPLATE_BAD_REQUEST):
86 """View handler for HTTP 400 Bad Request responses."""
87 t = loader.get_template(template_name)
89 return HttpResponseBadRequest(t.render(request=request))
92@pageview_decorator
93@index_or_agencyindex_origin_decorator
94def csrf_failure(request, reason):
95 """
96 View handler for CSRF_FAILURE_VIEW with custom data.
97 """
98 t = loader.get_template(TEMPLATE_BAD_REQUEST)
100 return HttpResponseNotFound(t.render(request=request))
103@pageview_decorator
104@index_or_agencyindex_origin_decorator
105def page_not_found(request, exception, template_name=TEMPLATE_NOT_FOUND):
106 """View handler for HTTP 404 Not Found responses."""
107 t = loader.get_template(template_name)
109 return HttpResponseNotFound(t.render(request=request))
112@pageview_decorator
113@index_or_agencyindex_origin_decorator
114def server_error(request, template_name=TEMPLATE_SERVER_ERROR):
115 """View handler for HTTP 500 Server Error responses."""
116 t = loader.get_template(template_name)
118 return HttpResponseServerError(t.render(request=request))
121def logged_out(request):
122 """View handler for the final log out confirmation message."""
123 return TemplateResponse(request, TEMPLATE_LOGGED_OUT)