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

44 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-22 18:00 +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 agency_context = { 

19 "eligibility_index_url": agency.eligibility_index_url, 

20 "help_templates": unique_values([f.help_template for f in agency.enrollment_flows.all() if f.help_template]), 

21 "info_url": agency.info_url, 

22 "long_name": agency.long_name, 

23 "phone": agency.phone, 

24 "short_name": agency.short_name, 

25 "slug": agency.slug, 

26 } 

27 

28 if agency.logo_large and agency.logo_small: 

29 agency_context.update( 

30 { 

31 "logo_small_url": agency.logo_small.url, 

32 "logo_large_url": agency.logo_large.url, 

33 } 

34 ) 

35 

36 return agency_context 

37 

38 

39def agency(request): 

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

41 agency = session.agency(request) 

42 

43 if agency is None: 

44 return {} 

45 

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

47 

48 

49def active_agencies(request): 

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

51 agencies = models.TransitAgency.all_active() 

52 

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

54 

55 

56def analytics(request): 

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

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

59 

60 

61def authentication(request): 

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

63 flow = session.flow(request) 

64 

65 if flow: 

66 data = { 

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

68 } 

69 

70 if flow.uses_claims_verification: 

71 data["sign_out_button_template"] = flow.claims_provider.sign_out_button_template 

72 data["sign_out_link_template"] = flow.claims_provider.sign_out_link_template 

73 

74 return {"authentication": data} 

75 else: 

76 return {} 

77 

78 

79def debug(request): 

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

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

82 

83 

84def enrollment(request): 

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

86 flow = session.flow(request) 

87 expiry = session.enrollment_expiry(request) 

88 reenrollment = session.enrollment_reenrollment(request) 

89 

90 data = { 

91 "expires": expiry, 

92 "reenrollment": reenrollment, 

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

94 } 

95 

96 return {"enrollment": data} 

97 

98 

99def origin(request): 

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

101 origin = session.origin(request) 

102 

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

104 return {"origin": origin} 

105 else: 

106 return {} 

107 

108 

109def routes(request): 

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

111 

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