Coverage for benefits/core/context_processors.py: 94%
48 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-22 21:13 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-22 21:13 +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 "long_name": agency.long_name,
30 "phone": agency.phone,
31 "short_name": agency.short_name,
32 "slug": agency.slug,
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 origin(request):
107 """Context processor adds session.origin to request context."""
108 origin = session.origin(request)
110 if origin: 110 ↛ 113line 110 didn't jump to line 113 because the condition on line 110 was always true
111 return {"origin": origin}
112 else:
113 return {}
116def routes(request):
117 """Context processor adds information about each application route to the context."""
119 return {"routes": app_routes.to_dict()}