Coverage for benefits/core/views.py: 90%

75 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-17 22:53 +0000

1""" 

2The core application: view definition for the root of the webapp. 

3""" 

4 

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 

11 

12from benefits.routes import routes 

13from . import models, session 

14from .middleware import pageview_decorator, index_or_agencyindex_origin_decorator, user_error 

15 

16TEMPLATE_AGENCY = "core/index--agency-base.html" 

17TEMPLATE_LOGGED_OUT = "core/logged-out.html" 

18 

19TEMPLATE_BAD_REQUEST = "400.html" 

20TEMPLATE_NOT_FOUND = "404.html" 

21TEMPLATE_SERVER_ERROR = "500.html" 

22 

23 

24class IndexView(TemplateView): 

25 """View handler for the main entry page.""" 

26 

27 template_name = "core/index.html" 

28 

29 @method_decorator(pageview_decorator) 

30 def get(self, request, *args, **kwargs): 

31 session.reset(request) 

32 return super().get(request, *args, **kwargs) 

33 

34 

35@pageview_decorator 

36def agency_index(request, agency: models.TransitAgency): 

37 """View handler for an agency entry page.""" 

38 session.reset(request) 

39 session.update(request, agency=agency, origin=agency.index_url) 

40 

41 return TemplateResponse(request, "core/index--agency.html", agency.index_context) 

42 

43 

44@pageview_decorator 

45def agency_eligibility_index(request, agency: models.TransitAgency): 

46 """View handler forwards the request to the agency's Eligibility Index (e.g. flow selection) page.""" 

47 session.reset(request) 

48 session.update(request, agency=agency, origin=agency.index_url) 

49 

50 return redirect(routes.ELIGIBILITY_INDEX) 

51 

52 

53@pageview_decorator 

54def agency_public_key(request, agency: models.TransitAgency): 

55 """View handler returns an agency's public key as plain text.""" 

56 return HttpResponse(agency.eligibility_api_public_key_data, content_type="text/plain") 

57 

58 

59@pageview_decorator 

60def agency_card(request, agency: models.TransitAgency): 

61 """View handler forwards the request to the agency's Agency Card (e.g. Eligibility API) flow, or returns a user error.""" 

62 session.reset(request) 

63 session.update(request, agency=agency, origin=agency.index_url) 

64 

65 eligibility_api_flow = agency.enrollment_flows.exclude(eligibility_api_url="").order_by("id").last() 

66 

67 if eligibility_api_flow: 

68 session.update(request, flow=eligibility_api_flow) 

69 return redirect(routes.ELIGIBILITY_CONFIRM) 

70 else: 

71 return user_error(request) 

72 

73 

74class HelpView(TemplateView): 

75 """View handler for the help page.""" 

76 

77 template_name = "core/help.html" 

78 

79 @method_decorator(pageview_decorator) 

80 def get(self, request, *args, **kwargs): 

81 return super().get(request, *args, **kwargs) 

82 

83 def get_context_data(self, **kwargs): 

84 context = super().get_context_data(**kwargs) 

85 

86 if not session.active_agency(self.request): 

87 choices = models.CardSchemes.CHOICES 

88 context["all_card_schemes"] = [choices[card_scheme] for card_scheme in choices.keys()] 

89 

90 return context 

91 

92 

93@pageview_decorator 

94@index_or_agencyindex_origin_decorator 

95def bad_request(request, exception, template_name=TEMPLATE_BAD_REQUEST): 

96 """View handler for HTTP 400 Bad Request responses.""" 

97 t = loader.get_template(template_name) 

98 

99 return HttpResponseBadRequest(t.render(request=request)) 

100 

101 

102@pageview_decorator 

103@index_or_agencyindex_origin_decorator 

104def csrf_failure(request, reason): 

105 """ 

106 View handler for CSRF_FAILURE_VIEW with custom data. 

107 """ 

108 t = loader.get_template(TEMPLATE_BAD_REQUEST) 

109 

110 return HttpResponseNotFound(t.render(request=request)) 

111 

112 

113@pageview_decorator 

114@index_or_agencyindex_origin_decorator 

115def page_not_found(request, exception, template_name=TEMPLATE_NOT_FOUND): 

116 """View handler for HTTP 404 Not Found responses.""" 

117 t = loader.get_template(template_name) 

118 

119 return HttpResponseNotFound(t.render(request=request)) 

120 

121 

122@pageview_decorator 

123@index_or_agencyindex_origin_decorator 

124def server_error(request, template_name=TEMPLATE_SERVER_ERROR): 

125 """View handler for HTTP 500 Server Error responses.""" 

126 t = loader.get_template(template_name) 

127 

128 return HttpResponseServerError(t.render(request=request)) 

129 

130 

131def logged_out(request): 

132 """View handler for the final log out confirmation message.""" 

133 return TemplateResponse(request, TEMPLATE_LOGGED_OUT)