Coverage for benefits / core / context_processors.py: 79%
48 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-01 15:39 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-01 15:39 +0000
1"""
2The core application: context processors for enriching request context data.
3"""
5from django.conf import settings
6from django.utils.text import format_lazy
7from django.utils.translation import gettext_lazy
9from benefits.routes import routes as app_routes
11from . import models, session
14def _agency_context(agency: models.TransitAgency):
15 agency_context = {
16 "entrypoint_url": agency.entrypoint_url,
17 "info_url": agency.info_url,
18 "littlepay_config": agency.littlepay_config,
19 "long_name": agency.long_name,
20 "phone": agency.phone,
21 "short_name": agency.short_name,
22 "slug": agency.slug,
23 "supported_card_schemes": [
24 models.CardSchemes.CHOICES.get(card_scheme) for card_scheme in agency.supported_card_schemes
25 ],
26 "switchio_config": agency.switchio_config,
27 }
29 if agency.logo: 29 ↛ 32line 29 didn't jump to line 32 because the condition on line 29 was always true
30 agency_context.update({"logo_url": agency.logo.url})
32 return agency_context
35def agency(request):
36 """Context processor adds some information about the active agency to the request context."""
37 agency = session.agency(request)
39 if agency is None:
40 return {}
42 return {"agency": _agency_context(agency)}
45def active_agencies(request):
46 """Context processor adds some information about all active agencies to the request context."""
47 agencies = models.TransitAgency.all_active()
49 return {"active_agencies": [_agency_context(agency) for agency in agencies]}
52def analytics(request):
53 """Context processor adds some analytics information to request context."""
54 return {"analytics": {"api_key": settings.ANALYTICS_KEY, "uid": session.uid(request), "did": session.did(request)}}
57def authentication(request):
58 """Context processor adds authentication information to request context."""
59 flow = session.flow(request)
61 if flow: 61 ↛ 62line 61 didn't jump to line 62 because the condition on line 61 was never true
62 data = {
63 "logged_in": session.logged_in(request),
64 }
66 if flow.uses_claims_verification:
67 data["sign_out_button_template"] = flow.sign_out_button_template
68 data["sign_out_link_template"] = flow.sign_out_link_template
70 return {"authentication": data}
71 else:
72 return {}
75def debug(request):
76 """Context processor adds debug information to request context."""
77 return {"debug": session.context_dict(request)}
80def enrollment(request):
81 """Context processor adds enrollment information to request context."""
82 flow = session.flow(request)
83 expiry = session.enrollment_expiry(request)
84 reenrollment = session.enrollment_reenrollment(request)
86 data = {
87 "expires": expiry,
88 "reenrollment": reenrollment,
89 "supports_expiration": flow.supports_expiration if flow else False,
90 }
92 return {"enrollment": data}
95def feature_flags(request):
96 """Context processor adds feature flags to request context."""
97 return {"feature_flags": {}}
100def formatted_gettext_lazy(string, *args, **kwargs):
101 """Wraps format_lazy around gettext_lazy for simpler calling."""
102 return format_lazy(gettext_lazy(string), *args, **kwargs)
105def origin(request):
106 """Context processor adds session.origin to request context."""
107 origin = session.origin(request)
109 if origin: 109 ↛ 112line 109 didn't jump to line 112 because the condition on line 109 was always true
110 return {"origin": origin}
111 else:
112 return {}
115def routes(request):
116 """Context processor adds information about each application route to the context."""
118 return {"routes": app_routes.to_dict()}