Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/rest_framework/urlpatterns.py: 54%

61 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from django.urls import URLResolver, include, path, re_path, register_converter 

2from django.urls.resolvers import RoutePattern 

3 

4from rest_framework.settings import api_settings 

5 

6 

7def _get_format_path_converter(suffix_kwarg, allowed): 

8 if allowed: 8 ↛ 9line 8 didn't jump to line 9, because the condition on line 8 was never true

9 if len(allowed) == 1: 

10 allowed_pattern = allowed[0] 

11 else: 

12 allowed_pattern = '(?:%s)' % '|'.join(allowed) 

13 suffix_pattern = r"\.%s/?" % allowed_pattern 

14 else: 

15 suffix_pattern = r"\.[a-z0-9]+/?" 

16 

17 class FormatSuffixConverter: 

18 regex = suffix_pattern 

19 

20 def to_python(self, value): 

21 return value.strip('./') 

22 

23 def to_url(self, value): 

24 return '.' + value + '/' 

25 

26 converter_name = 'drf_format_suffix' 

27 if allowed: 27 ↛ 28line 27 didn't jump to line 28, because the condition on line 27 was never true

28 converter_name += '_' + '_'.join(allowed) 

29 

30 return converter_name, FormatSuffixConverter 

31 

32 

33def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_route=None): 

34 ret = [] 

35 for urlpattern in urlpatterns: 

36 if isinstance(urlpattern, URLResolver): 36 ↛ 38line 36 didn't jump to line 38, because the condition on line 36 was never true

37 # Set of included URL patterns 

38 regex = urlpattern.pattern.regex.pattern 

39 namespace = urlpattern.namespace 

40 app_name = urlpattern.app_name 

41 kwargs = urlpattern.default_kwargs 

42 # Add in the included patterns, after applying the suffixes 

43 patterns = apply_suffix_patterns(urlpattern.url_patterns, 

44 suffix_pattern, 

45 suffix_required, 

46 suffix_route) 

47 

48 # if the original pattern was a RoutePattern we need to preserve it 

49 if isinstance(urlpattern.pattern, RoutePattern): 

50 assert path is not None 

51 route = str(urlpattern.pattern) 

52 new_pattern = path(route, include((patterns, app_name), namespace), kwargs) 

53 else: 

54 new_pattern = re_path(regex, include((patterns, app_name), namespace), kwargs) 

55 

56 ret.append(new_pattern) 

57 else: 

58 # Regular URL pattern 

59 regex = urlpattern.pattern.regex.pattern.rstrip('$').rstrip('/') + suffix_pattern 

60 view = urlpattern.callback 

61 kwargs = urlpattern.default_args 

62 name = urlpattern.name 

63 # Add in both the existing and the new urlpattern 

64 if not suffix_required: 64 ↛ 68line 64 didn't jump to line 68, because the condition on line 64 was never false

65 ret.append(urlpattern) 

66 

67 # if the original pattern was a RoutePattern we need to preserve it 

68 if isinstance(urlpattern.pattern, RoutePattern): 68 ↛ 69line 68 didn't jump to line 69, because the condition on line 68 was never true

69 assert path is not None 

70 assert suffix_route is not None 

71 route = str(urlpattern.pattern).rstrip('$').rstrip('/') + suffix_route 

72 new_pattern = path(route, view, kwargs, name) 

73 else: 

74 new_pattern = re_path(regex, view, kwargs, name) 

75 

76 ret.append(new_pattern) 

77 

78 return ret 

79 

80 

81def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None): 

82 """ 

83 Supplement existing urlpatterns with corresponding patterns that also 

84 include a '.format' suffix. Retains urlpattern ordering. 

85 

86 urlpatterns: 

87 A list of URL patterns. 

88 

89 suffix_required: 

90 If `True`, only suffixed URLs will be generated, and non-suffixed 

91 URLs will not be used. Defaults to `False`. 

92 

93 allowed: 

94 An optional tuple/list of allowed suffixes. eg ['json', 'api'] 

95 Defaults to `None`, which allows any suffix. 

96 """ 

97 suffix_kwarg = api_settings.FORMAT_SUFFIX_KWARG 

98 if allowed: 98 ↛ 99line 98 didn't jump to line 99, because the condition on line 98 was never true

99 if len(allowed) == 1: 

100 allowed_pattern = allowed[0] 

101 else: 

102 allowed_pattern = '(%s)' % '|'.join(allowed) 

103 suffix_pattern = r'\.(?P<%s>%s)/?$' % (suffix_kwarg, allowed_pattern) 

104 else: 

105 suffix_pattern = r'\.(?P<%s>[a-z0-9]+)/?$' % suffix_kwarg 

106 

107 converter_name, suffix_converter = _get_format_path_converter(suffix_kwarg, allowed) 

108 register_converter(suffix_converter, converter_name) 

109 

110 suffix_route = '<%s:%s>' % (converter_name, suffix_kwarg) 

111 

112 return apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_route)