Coverage for benefits/core/views.py: 100%
65 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 00:56 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-19 00:56 +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.exclude(eligibility_api_url="").exclude(eligibility_form_class="").order_by("id").last()
64 )
66 if eligibility_api_flow:
67 session.update(request, flow=eligibility_api_flow)
68 return redirect(routes.ELIGIBILITY_CONFIRM)
69 else:
70 return user_error(request)
73@pageview_decorator
74def help(request):
75 """View handler for the help page."""
76 return TemplateResponse(request, TEMPLATE_HELP)
79@pageview_decorator
80@index_or_agencyindex_origin_decorator
81def bad_request(request, exception, template_name=TEMPLATE_BAD_REQUEST):
82 """View handler for HTTP 400 Bad Request responses."""
83 t = loader.get_template(template_name)
85 return HttpResponseBadRequest(t.render(request=request))
88@pageview_decorator
89@index_or_agencyindex_origin_decorator
90def csrf_failure(request, reason):
91 """
92 View handler for CSRF_FAILURE_VIEW with custom data.
93 """
94 t = loader.get_template(TEMPLATE_BAD_REQUEST)
96 return HttpResponseNotFound(t.render(request=request))
99@pageview_decorator
100@index_or_agencyindex_origin_decorator
101def page_not_found(request, exception, template_name=TEMPLATE_NOT_FOUND):
102 """View handler for HTTP 404 Not Found responses."""
103 t = loader.get_template(template_name)
105 return HttpResponseNotFound(t.render(request=request))
108@pageview_decorator
109@index_or_agencyindex_origin_decorator
110def server_error(request, template_name=TEMPLATE_SERVER_ERROR):
111 """View handler for HTTP 500 Server Error responses."""
112 t = loader.get_template(template_name)
114 return HttpResponseServerError(t.render(request=request))
117def logged_out(request):
118 """View handler for the final log out confirmation message."""
119 return TemplateResponse(request, TEMPLATE_LOGGED_OUT)