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

48 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-11-15 00:14 +0000

1""" 

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

3""" 

4 

5from django.conf import settings 

6 

7from benefits.routes import routes as app_routes 

8 

9from . import models, session 

10 

11 

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: 17 ↛ 18line 17 didn't jump to line 18 because the condition on line 17 was never true

18 flows_help.extend(flow.help_context) 

19 

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 } 

34 

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

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

37 

38 return agency_context 

39 

40 

41def agency(request): 

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

43 agency = session.agency(request) 

44 

45 if agency is None: 

46 return {} 

47 

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

49 

50 

51def active_agencies(request): 

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

53 agencies = models.TransitAgency.all_active() 

54 

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

56 

57 

58def analytics(request): 

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

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

61 

62 

63def authentication(request): 

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

65 flow = session.flow(request) 

66 

67 if flow: 

68 data = { 

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

70 } 

71 

72 if flow.uses_claims_verification: 72 ↛ 73line 72 didn't jump to line 73 because the condition on line 72 was never true

73 data["sign_out_button_template"] = flow.sign_out_button_template 

74 data["sign_out_link_template"] = flow.sign_out_link_template 

75 

76 return {"authentication": data} 

77 else: 

78 return {} 

79 

80 

81def debug(request): 

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

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

84 

85 

86def enrollment(request): 

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

88 flow = session.flow(request) 

89 expiry = session.enrollment_expiry(request) 

90 reenrollment = session.enrollment_reenrollment(request) 

91 

92 data = { 

93 "expires": expiry, 

94 "reenrollment": reenrollment, 

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

96 } 

97 

98 return {"enrollment": data} 

99 

100 

101def feature_flags(request): 

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

103 return {"feature_flags": {}} 

104 

105 

106def origin(request): 

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

108 origin = session.origin(request) 

109 

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

114 

115 

116def routes(request): 

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

118 

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