Coverage for benefits/core/views.py: 100%
65 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-19 16:31 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-19 16:31 +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
10from benefits.routes import routes
11from . import models, session
12from .middleware import pageview_decorator, index_or_agencyindex_origin_decorator, user_error
14TEMPLATE_INDEX = "core/index.html"
15TEMPLATE_AGENCY = "core/agency-index.html"
16TEMPLATE_HELP = "core/help.html"
17TEMPLATE_LOGGED_OUT = "core/logged-out.html"
19TEMPLATE_BAD_REQUEST = "400.html"
20TEMPLATE_NOT_FOUND = "404.html"
21TEMPLATE_SERVER_ERROR = "500.html"
24@pageview_decorator
25def index(request):
26 """View handler for the main entry page."""
27 session.reset(request)
29 return TemplateResponse(request, TEMPLATE_INDEX)
32@pageview_decorator
33def agency_index(request, agency: models.TransitAgency):
34 """View handler for an agency entry page."""
35 session.reset(request)
36 session.update(request, agency=agency, origin=agency.index_url)
38 return TemplateResponse(request, agency.index_template)
41@pageview_decorator
42def agency_eligibility_index(request, agency: models.TransitAgency):
43 """View handler forwards the request to the agency's Eligibility Index (e.g. flow selection) page."""
44 session.reset(request)
45 session.update(request, agency=agency, origin=agency.index_url)
47 return redirect(routes.ELIGIBILITY_INDEX)
50@pageview_decorator
51def agency_public_key(request, agency: models.TransitAgency):
52 """View handler returns an agency's public key as plain text."""
53 return HttpResponse(agency.eligibility_api_public_key_data, content_type="text/plain")
56@pageview_decorator
57def agency_card(request, agency: models.TransitAgency):
58 """View handler forwards the request to the agency's Agency Card (e.g. Eligibility API) flow, or returns a user error."""
59 session.reset(request)
60 session.update(request, agency=agency, origin=agency.index_url)
62 eligibility_api_flow = (
63 agency.enrollment_flows.filter(eligibility_api_url__isnull=False, eligibility_form_class__isnull=False)
64 .order_by("id")
65 .last()
66 )
68 if eligibility_api_flow:
69 session.update(request, flow=eligibility_api_flow)
70 return redirect(routes.ELIGIBILITY_CONFIRM)
71 else:
72 return user_error(request)
75@pageview_decorator
76def help(request):
77 """View handler for the help page."""
78 return TemplateResponse(request, TEMPLATE_HELP)
81@pageview_decorator
82@index_or_agencyindex_origin_decorator
83def bad_request(request, exception, template_name=TEMPLATE_BAD_REQUEST):
84 """View handler for HTTP 400 Bad Request responses."""
85 t = loader.get_template(template_name)
87 return HttpResponseBadRequest(t.render(request=request))
90@pageview_decorator
91@index_or_agencyindex_origin_decorator
92def csrf_failure(request, reason):
93 """
94 View handler for CSRF_FAILURE_VIEW with custom data.
95 """
96 t = loader.get_template(TEMPLATE_BAD_REQUEST)
98 return HttpResponseNotFound(t.render(request=request))
101@pageview_decorator
102@index_or_agencyindex_origin_decorator
103def page_not_found(request, exception, template_name=TEMPLATE_NOT_FOUND):
104 """View handler for HTTP 404 Not Found responses."""
105 t = loader.get_template(template_name)
107 return HttpResponseNotFound(t.render(request=request))
110@pageview_decorator
111@index_or_agencyindex_origin_decorator
112def server_error(request, template_name=TEMPLATE_SERVER_ERROR):
113 """View handler for HTTP 500 Server Error responses."""
114 t = loader.get_template(template_name)
116 return HttpResponseServerError(t.render(request=request))
119def logged_out(request):
120 """View handler for the final log out confirmation message."""
121 return TemplateResponse(request, TEMPLATE_LOGGED_OUT)