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

50 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-08 16:26 +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 unique_values(original_list): 

13 # dict.fromkeys gets the unique values and preserves order 

14 return list(dict.fromkeys(original_list)) 

15 

16 

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) 

24 

25 agency_context = { 

26 "eligibility_index_url": agency.eligibility_index_url, 

27 "flows_help": flows_help, 

28 "info_url": agency.info_url, 

29 "littlepay_config": agency.littlepay_config, 

30 "long_name": agency.long_name, 

31 "phone": agency.phone, 

32 "short_name": agency.short_name, 

33 "slug": agency.slug, 

34 "switchio_config": agency.switchio_config, 

35 } 

36 

37 if agency.logo_large and agency.logo_small: 37 ↛ 45line 37 didn't jump to line 45 because the condition on line 37 was always true

38 agency_context.update( 

39 { 

40 "logo_small_url": agency.logo_small.url, 

41 "logo_large_url": agency.logo_large.url, 

42 } 

43 ) 

44 

45 return agency_context 

46 

47 

48def agency(request): 

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

50 agency = session.agency(request) 

51 

52 if agency is None: 

53 return {} 

54 

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

56 

57 

58def active_agencies(request): 

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

60 agencies = models.TransitAgency.all_active() 

61 

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

63 

64 

65def analytics(request): 

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

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

68 

69 

70def authentication(request): 

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

72 flow = session.flow(request) 

73 

74 if flow: 

75 data = { 

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

77 } 

78 

79 if flow.uses_claims_verification: 

80 data["sign_out_button_template"] = flow.sign_out_button_template 

81 data["sign_out_link_template"] = flow.sign_out_link_template 

82 

83 return {"authentication": data} 

84 else: 

85 return {} 

86 

87 

88def debug(request): 

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

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

91 

92 

93def enrollment(request): 

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

95 flow = session.flow(request) 

96 expiry = session.enrollment_expiry(request) 

97 reenrollment = session.enrollment_reenrollment(request) 

98 

99 data = { 

100 "expires": expiry, 

101 "reenrollment": reenrollment, 

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

103 } 

104 

105 return {"enrollment": data} 

106 

107 

108def feature_flags(request): 

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

110 return {"feature_flags": {"LITTLEPAY_ADDITIONAL_CARDTYPES": settings.LITTLEPAY_ADDITIONAL_CARDTYPES}} 

111 

112 

113def origin(request): 

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

115 origin = session.origin(request) 

116 

117 if origin: 117 ↛ 120line 117 didn't jump to line 120 because the condition on line 117 was always true

118 return {"origin": origin} 

119 else: 

120 return {} 

121 

122 

123def routes(request): 

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

125 

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