Coverage for benefits/core/views.py: 90%
103 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-23 20:53 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-23 20:53 +0000
1"""
2The core application: view definition for the root of the webapp.
3"""
5import logging
6from dataclasses import asdict, dataclass
8from django.http import HttpResponse
9from django.utils import translation
10from django.utils.decorators import method_decorator
11from django.views.generic import RedirectView, TemplateView, View
12from django.views.generic.edit import FormView
14from benefits.core import models, session
15from benefits.core.context_processors import formatted_gettext_lazy as _
16from benefits.core.forms import ChooseAgencyForm
17from benefits.core.middleware import pageview_decorator, user_error
18from benefits.core.mixins import AgencySessionRequiredMixin
19from benefits.core.models import EligibilityApiVerificationRequest, SystemName
20from benefits.routes import routes
22logger = logging.getLogger(__name__)
25class IndexView(FormView):
26 """View handler for the main entry page."""
28 template_name = "core/index.html"
29 form_class = ChooseAgencyForm
31 # this form cannot use an action_url because the redirect is determined
32 # *after* user interaction
33 def form_valid(self, form):
34 agency = form.selected_transit_agency
35 session.reset(self.request)
36 session.update(self.request, agency=form.selected_transit_agency, origin=form.selected_transit_agency.index_url)
37 self.success_url = agency.entrypoint_url
38 return super().form_valid(form)
40 @method_decorator(pageview_decorator)
41 def get(self, request, *args, **kwargs):
42 session.reset(request)
43 language = translation.get_language()
44 logger.debug(f"Language: {language}")
45 return super().get(request, *args, **kwargs)
48class AgencyIndexView(TemplateView):
49 """View handler for an agency entry page."""
51 template_name = "core/index--agency.html"
53 @method_decorator(pageview_decorator)
54 def get(self, request, *args, **kwargs):
55 agency = self.kwargs.get("agency")
56 session.reset(request)
57 session.update(request, agency=agency, origin=agency.index_url)
58 return super().get(request, *args, **kwargs)
60 def get_context_data(self, **kwargs):
61 context = super().get_context_data(**kwargs)
62 agency = self.kwargs.get("agency")
63 short_name = agency.short_name
64 headline = _("Get your reduced fare when you tap to ride on {short_name}", short_name=short_name)
66 context |= {"headline": headline, "next_url": agency.entrypoint_url}
67 return context
70class AgencyCardView(RedirectView):
71 """View handler forwards the request to the agency's Agency Card (e.g. Eligibility API) flow, or returns a user error."""
73 pattern_name = routes.ELIGIBILITY_CONFIRM
75 @method_decorator(pageview_decorator)
76 def get(self, request, *args, **kwargs):
77 # keep a reference to the agency before removing from kwargs
78 # since the eventual reverse() lookup doesn't expect this key in the kwargs for routes.ELIGIBILITY_CONFIRM
79 # self.kwargs still contains the agency if needed
80 agency = kwargs.pop("agency")
81 session.reset(request)
82 session.update(request, agency=agency, origin=agency.index_url)
84 eligibility_api_flow = agency.enrollment_flows.exclude(api_request=None).order_by("id").last()
85 if eligibility_api_flow:
86 session.update(request, flow=eligibility_api_flow)
87 return super().get(request, *args, **kwargs)
88 else:
89 return user_error(request)
92class AgencyPublicKeyView(View):
93 """View handler returns an agency's public key as plain text."""
95 @method_decorator(pageview_decorator)
96 def get(self, request, *args, **kwargs):
97 # in the URL, a TransitAgency argument is required, but we just need to return the single
98 # EligibilityApiVerificationRequest client public key that is shared across all agencies
99 eligibility_api_public_key_data = EligibilityApiVerificationRequest.objects.first().client_public_key_data
100 return HttpResponse(eligibility_api_public_key_data, content_type="text/plain")
103@dataclass
104class FlowHelp:
106 id: str
107 headline: str
108 text: str
110 def dict(self):
111 return asdict(self)
114class HelpView(TemplateView):
115 """View handler for the help page."""
117 template_name = "core/help.html"
119 @method_decorator(pageview_decorator)
120 def get(self, request, *args, **kwargs):
121 return super().get(request, *args, **kwargs)
123 def get_context_data(self, **kwargs):
124 context = super().get_context_data(**kwargs)
126 if not session.active_agency(self.request):
127 choices = models.CardSchemes.CHOICES
128 context["all_card_schemes"] = [choices[card_scheme] for card_scheme in choices.keys()]
129 else:
130 agency = session.agency(self.request)
132 # build up a single list of all flow help contexts
133 flows_help = []
134 for flow in agency.enrollment_flows.all(): 134 ↛ 135line 134 didn't jump to line 135 because the loop on line 134 never started
135 help_contexts = self.get_help_contexts(flow)
136 if help_contexts:
137 flows_help.extend(help_contexts)
139 context["flows_help"] = flows_help
140 context["transit_processor"] = dict(
141 name=agency.transit_processor_display_name,
142 website=agency.transit_processor_info_url,
143 )
145 return context
147 def get_help_contexts(self, flow) -> list[dict]:
148 flows_help = {
149 SystemName.CALFRESH.value: [
150 FlowHelp(
151 id="calfresh-transit-benefit",
152 headline=_("How do I know if I’m eligible for the transit benefit for CalFresh Cardholders?"),
153 text=_(
154 "We verify your eligibility as a CalFresh Cardholder by confirming you have received funds in your "
155 "CalFresh account at any point in the last three months. This means you are eligible for a transit "
156 "benefit even if you did not receive funds in your CalFresh account this month or last month."
157 ),
158 ),
159 FlowHelp(
160 id="calfresh-transit-benefit-no-account-changes",
161 headline=_("Will this transit benefit change my CalFresh account?"),
162 text=_("No. Your monthly CalFresh allotment will not change."),
163 ),
164 FlowHelp(
165 id="calfresh-transit-benefit-enrollment",
166 headline=_("Do I need my Golden State Advantage card to enroll?"),
167 text=_(
168 "No, you do not need your physical EBT card to enroll. We use information from Login.gov and the "
169 "California Department of Social Services to enroll you in the benefit."
170 ),
171 ),
172 FlowHelp(
173 id="calfresh-transit-benefit-payment",
174 headline=_("Can I use my Golden State Advantage card to pay for transit rides?"),
175 text=_(
176 "No. You can not use your EBT or P-EBT card to pay for public transportation. "
177 "When you tap to ride, use your personal contactless debit or credit card to pay for public transportation." # noqa: E501
178 ),
179 ),
180 ],
181 SystemName.COURTESY_CARD.value: [
182 FlowHelp(
183 id="mst-agency-card",
184 headline=_("What is a Courtesy Card?"),
185 text=_(
186 "Monterey-Salinas Transit issues Courtesy Cards to riders who qualify for a number of reduced fare programs. " # noqa: E501
187 "This transit benefit may need to be renewed in the future based on the expiration date of the Courtesy Card. " # noqa: E501
188 'Learn more at the <a href="https://mst.org/riders-guide/how-to-ride/courtesy-card/" target="_blank" rel="noopener noreferrer">MST Riders Guide</a>.' # noqa: E501
189 ),
190 )
191 ],
192 SystemName.MEDICARE.value: [
193 FlowHelp(
194 id="medicare-transit-benefit",
195 headline=_("How do I know if I qualify for the Medicare Cardholder option?"),
196 text=_(
197 "You qualify for this option if you have a Medicare card. To enroll you will need an account with Medicare.gov. " # noqa: E501
198 "You will need to sign up for a Medicare.gov account if you do not currently have one. Deceased Medicare cardholders do not qualify." # noqa: E501
199 ),
200 ),
201 FlowHelp(
202 id="medicare-transit-benefit-enrollment",
203 headline=_("Do I need my Medicare card to enroll?"),
204 text=_(
205 "No, you do not need your physical Medicare card to enroll in a transit benefit. "
206 "You will need the information on your card to create an account at Medicare.gov if you do not currently have an online account." # noqa: E501
207 ),
208 ),
209 FlowHelp(
210 id="medicare-transit-benefit-payment",
211 headline=_("Do I need to bring my Medicare card when I ride public transportation?"),
212 text=_(
213 "No, you do not need your physical Medicare card to use your transit benefit on public transportation. " # noqa: E501
214 "Once you have enrolled you can use your contactless debit or credit card to tap to ride with a reduced fare." # noqa: E501
215 ),
216 ),
217 FlowHelp(
218 id="medicare-transit-benefit-recommended",
219 headline=_("What if I qualify for more than one option?"),
220 text=_(
221 "You can enroll in any option you qualify for. We recommend enrolling in the Medicare Cardholder option if you qualify for it." # noqa: 501
222 ),
223 ),
224 ],
225 SystemName.REDUCED_FARE_MOBILITY_ID.value: [
226 FlowHelp(
227 id="sbmtd-agency-card",
228 headline=_("What is a Reduced Fare Mobility ID?"),
229 text=_(
230 "The Santa Barbara Metropolitan Transit District issues Reduced Fare Mobility ID cards to eligible riders. " # noqa: E501
231 "This transit benefit may need to be renewed in the future based on the expiration date of the Reduced Fare Mobility ID. " # noqa: E501
232 'Learn more at the <a href="https://sbmtd.gov/fares-passes/" target="_blank" rel="noopener noreferrer">SBMTD Fares & Passes</a>.' # noqa: E501
233 ),
234 )
235 ],
236 }
238 ctx = flows_help.get(flow.system_name)
239 return [c.dict() for c in ctx] if ctx else []
242class LoggedOutView(TemplateView):
243 """View handler for the final log out confirmation message."""
245 template_name = "core/logged-out.html"
248class AdditionalAgenciesView(AgencySessionRequiredMixin, TemplateView):
249 """View handler for nearby/additional providers."""
251 template_name = "core/additional-agencies.html"
252 only_active_agencies = True
254 def get_context_data(self, **kwargs):
255 context = super().get_context_data(**kwargs)
256 agency = self.agency
257 agencies = agency.group_agency_short_names(only_active=self.only_active_agencies)
259 context |= {
260 "title": _("Nearby transit providers"),
261 "headline": _("Weʼll also enroll you at nearby transit providers"),
262 "blurb": _("Youʼll get reduced fares when you tap to pay at {count} transit providers.", count=len(agencies)),
263 "agencies": agencies,
264 }
266 return context