Coverage for benefits / core / urls.py: 90%

27 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-13 19:35 +0000

1""" 

2The core application: URLConf for the root of the webapp. 

3""" 

4 

5import logging 

6 

7from django.urls import path, register_converter 

8 

9from benefits.core import models 

10from benefits.core.views import ( 

11 AgencyCardView, 

12 AgencyEligibilityIndexView, 

13 AgencyIndexView, 

14 AgencyPublicKeyView, 

15 HelpView, 

16 IndexView, 

17 LoggedOutView, 

18) 

19from benefits.routes import routes 

20from benefits.views import ServerErrorView 

21 

22logger = logging.getLogger(__name__) 

23 

24 

25class TransitAgencyPathConverter: 

26 """Path converter to parse valid TransitAgency objects from URL paths.""" 

27 

28 # used to test the url fragment, determines if this PathConverter is used 

29 regex = "[a-zA-Z]{3,}" 

30 

31 def to_python(self, value): 

32 """Determine if the matched fragment corresponds to an active Agency.""" 

33 value = str(value).lower() 

34 logger.debug(f"Matched fragment from path: {value}") 

35 

36 agency = models.TransitAgency.by_slug(value) 

37 if agency and agency.active: 37 ↛ 38line 37 didn't jump to line 38 because the condition on line 37 was never true

38 logger.debug("Path fragment is an active agency") 

39 return agency 

40 else: 

41 logger.error("Path fragment is not an active agency") 

42 raise ValueError("value") 

43 

44 def to_url(self, agency): 

45 """Convert the Agency back into a string for a URL.""" 

46 try: 

47 return agency.slug 

48 except AttributeError: 

49 return str(agency) 

50 

51 

52logger.debug(f"Register path converter: {TransitAgencyPathConverter.__name__}") 

53register_converter(TransitAgencyPathConverter, "agency") 

54 

55app_name = "core" 

56 

57urlpatterns = [ 

58 path("", IndexView.as_view(), name=routes.name(routes.INDEX)), 

59 path("help", HelpView.as_view(), name=routes.name(routes.HELP)), 

60 path("logged_out", LoggedOutView.as_view(), name=routes.name(routes.LOGGED_OUT)), 

61 path("error", ServerErrorView.as_view(), name=routes.name(routes.SERVER_ERROR)), 

62 path("<agency:agency>", AgencyIndexView.as_view(), name=routes.name(routes.AGENCY_INDEX)), 

63 path("<agency:agency>/agency-card", AgencyCardView.as_view(), name=routes.name(routes.AGENCY_CARD)), 

64 path( 

65 "<agency:agency>/eligibility", 

66 AgencyEligibilityIndexView.as_view(), 

67 name=routes.name(routes.AGENCY_ELIGIBILITY_INDEX), 

68 ), 

69 path("<agency:agency>/publickey", AgencyPublicKeyView.as_view(), name=routes.name(routes.AGENCY_PUBLIC_KEY)), 

70]