Coverage for benefits/core/context_processors.py: 94%
48 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-17 22:53 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-17 22:53 +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 _agency_context(agency: models.TransitAgency):
13 # build up a single list of all flow help contexts
14 flows_help = []
15 for flow in agency.enrollment_flows.all():
16 # flow.help_context is a list of context objects
17 if len(flow.help_context) > 0:
18 flows_help.extend(flow.help_context)
20 agency_context = {
21 "eligibility_index_url": agency.eligibility_index_url,
22 "flows_help": flows_help,
23 "info_url": agency.info_url,
24 "littlepay_config": agency.littlepay_config,
25 "long_name": agency.long_name,
26 "phone": agency.phone,
27 "short_name": agency.short_name,
28 "slug": agency.slug,
29 "supported_card_schemes": [
30 models.CardSchemes.CHOICES.get(card_scheme) for card_scheme in agency.supported_card_schemes
31 ],
32 "switchio_config": agency.switchio_config,
33 }
35 if agency.logo_large and agency.logo_small: 35 ↛ 43line 35 didn't jump to line 43 because the condition on line 35 was always true
36 agency_context.update(
37 {
38 "logo_small_url": agency.logo_small.url,
39 "logo_large_url": agency.logo_large.url,
40 }
41 )
43 return agency_context
46def agency(request):
47 """Context processor adds some information about the active agency to the request context."""
48 agency = session.agency(request)
50 if agency is None:
51 return {}
53 return {"agency": _agency_context(agency)}
56def active_agencies(request):
57 """Context processor adds some information about all active agencies to the request context."""
58 agencies = models.TransitAgency.all_active()
60 return {"active_agencies": [_agency_context(agency) for agency in agencies]}
63def analytics(request):
64 """Context processor adds some analytics information to request context."""
65 return {"analytics": {"api_key": settings.ANALYTICS_KEY, "uid": session.uid(request), "did": session.did(request)}}
68def authentication(request):
69 """Context processor adds authentication information to request context."""
70 flow = session.flow(request)
72 if flow:
73 data = {
74 "logged_in": session.logged_in(request),
75 }
77 if flow.uses_claims_verification:
78 data["sign_out_button_template"] = flow.sign_out_button_template
79 data["sign_out_link_template"] = flow.sign_out_link_template
81 return {"authentication": data}
82 else:
83 return {}
86def debug(request):
87 """Context processor adds debug information to request context."""
88 return {"debug": session.context_dict(request)}
91def enrollment(request):
92 """Context processor adds enrollment information to request context."""
93 flow = session.flow(request)
94 expiry = session.enrollment_expiry(request)
95 reenrollment = session.enrollment_reenrollment(request)
97 data = {
98 "expires": expiry,
99 "reenrollment": reenrollment,
100 "supports_expiration": flow.supports_expiration if flow else False,
101 }
103 return {"enrollment": data}
106def feature_flags(request):
107 """Context processor adds feature flags to request context."""
108 return {"feature_flags": {}}
111def origin(request):
112 """Context processor adds session.origin to request context."""
113 origin = session.origin(request)
115 if origin: 115 ↛ 118line 115 didn't jump to line 118 because the condition on line 115 was always true
116 return {"origin": origin}
117 else:
118 return {}
121def routes(request):
122 """Context processor adds information about each application route to the context."""
124 return {"routes": app_routes.to_dict()}