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

25 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-19 16:31 +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.routes import routes 

10from . import models, views 

11 

12 

13logger = logging.getLogger(__name__) 

14 

15 

16class TransitAgencyPathConverter: 

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

18 

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

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

21 

22 def to_python(self, value): 

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

24 value = str(value).lower() 

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

26 

27 agency = models.TransitAgency.by_slug(value) 

28 if agency and agency.active: 

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

30 return agency 

31 else: 

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

33 raise ValueError("value") 

34 

35 def to_url(self, agency): 

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

37 try: 

38 return agency.slug 

39 except AttributeError: 

40 return str(agency) 

41 

42 

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

44register_converter(TransitAgencyPathConverter, "agency") 

45 

46app_name = "core" 

47 

48urlpatterns = [ 

49 path("", views.index, name=routes.name(routes.INDEX)), 

50 path("help", views.help, name=routes.name(routes.HELP)), 

51 path("<agency:agency>", views.agency_index, name=routes.name(routes.AGENCY_INDEX)), 

52 path("<agency:agency>/agency-card", views.agency_card, name=routes.name(routes.AGENCY_CARD)), 

53 path("<agency:agency>/eligibility", views.agency_eligibility_index, name=routes.name(routes.AGENCY_ELIGIBILITY_INDEX)), 

54 path("<agency:agency>/publickey", views.agency_public_key, name=routes.name(routes.AGENCY_PUBLIC_KEY)), 

55 path("logged_out", views.logged_out, name=routes.name(routes.LOGGED_OUT)), 

56 path("error", views.server_error, name=routes.name(routes.SERVER_ERROR)), 

57]