Coverage for benefits/core/context_processors.py: 81%

52 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-08 19:50 +0000

1""" 

2The core application: context processors for enriching request context data. 

3""" 

4 

5from django.conf import settings 

6from django.utils.text import format_lazy 

7from django.utils.translation import gettext_lazy 

8 

9from benefits.routes import routes as app_routes 

10 

11from . import models, session 

12 

13 

14def _agency_context(agency: models.TransitAgency): 

15 agency_context = { 

16 "entrypoint_url": agency.entrypoint_url, 

17 "info_url": agency.info_url, 

18 "transit_processor": agency.transit_processor_system_name, 

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 } 

27 

28 if agency.logo: 28 ↛ 31line 28 didn't jump to line 31 because the condition on line 28 was always true

29 agency_context.update({"logo_url": agency.logo.url}) 

30 

31 return agency_context 

32 

33 

34def agency(request): 

35 """Context processor adds some information about the active agency to the request context.""" 

36 agency = session.agency(request) 

37 

38 if agency is None: 

39 return {} 

40 

41 return {"agency": _agency_context(agency)} 

42 

43 

44def active_agencies(request): 

45 """Context processor adds some information about all active agencies to the request context.""" 

46 agencies = models.TransitAgency.all_active().select_related( 

47 "transit_processor_config", "transit_processor_config__littlepayconfig", "transit_processor_config__switchioconfig" 

48 ) 

49 

50 return {"active_agencies": [_agency_context(agency) for agency in agencies]} 

51 

52 

53def analytics(request): 

54 """Context processor adds some analytics information to request context.""" 

55 return {"analytics": {"api_key": settings.ANALYTICS_KEY, "uid": session.uid(request), "did": session.did(request)}} 

56 

57 

58def authentication(request): 

59 """Context processor adds authentication information to request context.""" 

60 flow = session.flow(request) 

61 

62 if flow: 62 ↛ 63line 62 didn't jump to line 63 because the condition on line 62 was never true

63 data = { 

64 "logged_in": session.logged_in(request), 

65 } 

66 

67 if flow.uses_claims_verification: 

68 data["sign_out_button_template"] = flow.sign_out_button_template 

69 data["sign_out_link_template"] = flow.sign_out_link_template 

70 

71 return {"authentication": data} 

72 else: 

73 return {} 

74 

75 

76def debug(request): 

77 """Context processor adds debug information to request context.""" 

78 return {"debug": session.context_dict(request)} 

79 

80 

81def enrollment(request): 

82 """Context processor adds enrollment information to request context.""" 

83 flow = session.flow(request) 

84 expiry = session.enrollment_expiry(request) 

85 reenrollment = session.enrollment_reenrollment(request) 

86 

87 data = { 

88 "expires": expiry, 

89 "reenrollment": reenrollment, 

90 "supports_expiration": flow.supports_expiration if flow else False, 

91 } 

92 

93 return {"enrollment": data} 

94 

95 

96def feature_flags(request): 

97 """Context processor adds feature flags to request context.""" 

98 return {"feature_flags": {}} 

99 

100 

101def formatted_gettext_lazy(string, *args, **kwargs): 

102 """Wraps format_lazy around gettext_lazy for simpler calling.""" 

103 return format_lazy(gettext_lazy(string), *args, **kwargs) 

104 

105 

106def is_prod(request): 

107 """Context processor adds environment name to request context.""" 

108 return {"is_prod": settings.RUNTIME_ENVIRONMENT() == settings.RUNTIME_ENVS.PROD} 

109 

110 

111def origin(request): 

112 """Context processor adds session.origin to request context.""" 

113 origin = session.origin(request) 

114 

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 {} 

119 

120 

121def routes(request): 

122 """Context processor adds information about each application route to the context.""" 

123 

124 return {"routes": app_routes.to_dict()} 

125 

126 

127def runtime_env(request): 

128 """Context processor adds environment name to request context.""" 

129 return {"runtime_env": settings.RUNTIME_ENVIRONMENT()}