Coverage for benefits/core/context_processors.py: 94%
50 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: context processors for enriching request context data.
3"""
5from django.conf import settings
7from benefits.routes import routes as app_routes
9from . import models, session
12def unique_values(original_list):
13 # dict.fromkeys gets the unique values and preserves order
14 return list(dict.fromkeys(original_list))
17def _agency_context(agency: models.TransitAgency):
18 # build up a single list of all flow help contexts
19 flows_help = []
20 for flow in agency.enrollment_flows.all():
21 # flow.help_context is a list of context objects
22 if len(flow.help_context) > 0:
23 flows_help.extend(flow.help_context)
25 agency_context = {
26 "eligibility_index_url": agency.eligibility_index_url,
27 "flows_help": flows_help,
28 "info_url": agency.info_url,
29 "littlepay_config": agency.littlepay_config,
30 "long_name": agency.long_name,
31 "phone": agency.phone,
32 "short_name": agency.short_name,
33 "slug": agency.slug,
34 "switchio_config": agency.switchio_config,
35 }
37 if agency.logo_large and agency.logo_small: 37 ↛ 45line 37 didn't jump to line 45 because the condition on line 37 was always true
38 agency_context.update(
39 {
40 "logo_small_url": agency.logo_small.url,
41 "logo_large_url": agency.logo_large.url,
42 }
43 )
45 return agency_context
48def agency(request):
49 """Context processor adds some information about the active agency to the request context."""
50 agency = session.agency(request)
52 if agency is None:
53 return {}
55 return {"agency": _agency_context(agency)}
58def active_agencies(request):
59 """Context processor adds some information about all active agencies to the request context."""
60 agencies = models.TransitAgency.all_active()
62 return {"active_agencies": [_agency_context(agency) for agency in agencies]}
65def analytics(request):
66 """Context processor adds some analytics information to request context."""
67 return {"analytics": {"api_key": settings.ANALYTICS_KEY, "uid": session.uid(request), "did": session.did(request)}}
70def authentication(request):
71 """Context processor adds authentication information to request context."""
72 flow = session.flow(request)
74 if flow:
75 data = {
76 "logged_in": session.logged_in(request),
77 }
79 if flow.uses_claims_verification:
80 data["sign_out_button_template"] = flow.sign_out_button_template
81 data["sign_out_link_template"] = flow.sign_out_link_template
83 return {"authentication": data}
84 else:
85 return {}
88def debug(request):
89 """Context processor adds debug information to request context."""
90 return {"debug": session.context_dict(request)}
93def enrollment(request):
94 """Context processor adds enrollment information to request context."""
95 flow = session.flow(request)
96 expiry = session.enrollment_expiry(request)
97 reenrollment = session.enrollment_reenrollment(request)
99 data = {
100 "expires": expiry,
101 "reenrollment": reenrollment,
102 "supports_expiration": flow.supports_expiration if flow else False,
103 }
105 return {"enrollment": data}
108def feature_flags(request):
109 """Context processor adds feature flags to request context."""
110 return {"feature_flags": {"LITTLEPAY_ADDITIONAL_CARDTYPES": settings.LITTLEPAY_ADDITIONAL_CARDTYPES}}
113def origin(request):
114 """Context processor adds session.origin to request context."""
115 origin = session.origin(request)
117 if origin: 117 ↛ 120line 117 didn't jump to line 120 because the condition on line 117 was always true
118 return {"origin": origin}
119 else:
120 return {}
123def routes(request):
124 """Context processor adds information about each application route to the context."""
126 return {"routes": app_routes.to_dict()}