MCPcopy Create free account
hub / github.com/encode/django-rest-framework / apply_suffix_patterns

Function apply_suffix_patterns

rest_framework/urlpatterns.py:37–82  ·  view source on GitHub ↗
(urlpatterns, suffix_pattern, suffix_required, suffix_route=None)

Source from the content-addressed store, hash-verified

35
36
37def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_route=None):
38 ret = []
39 for urlpattern in urlpatterns:
40 if isinstance(urlpattern, URLResolver):
41 # Set of included URL patterns
42 regex = urlpattern.pattern.regex.pattern
43 namespace = urlpattern.namespace
44 app_name = urlpattern.app_name
45 kwargs = urlpattern.default_kwargs
46 # Add in the included patterns, after applying the suffixes
47 patterns = apply_suffix_patterns(urlpattern.url_patterns,
48 suffix_pattern,
49 suffix_required,
50 suffix_route)
51
52 # if the original pattern was a RoutePattern we need to preserve it
53 if isinstance(urlpattern.pattern, RoutePattern):
54 assert path is not None
55 route = str(urlpattern.pattern)
56 new_pattern = path(route, include((patterns, app_name), namespace), kwargs)
57 else:
58 new_pattern = re_path(regex, include((patterns, app_name), namespace), kwargs)
59
60 ret.append(new_pattern)
61 else:
62 # Regular URL pattern
63 regex = urlpattern.pattern.regex.pattern.rstrip('$').rstrip('/') + suffix_pattern
64 view = urlpattern.callback
65 kwargs = urlpattern.default_args
66 name = urlpattern.name
67 # Add in both the existing and the new urlpattern
68 if not suffix_required:
69 ret.append(urlpattern)
70
71 # if the original pattern was a RoutePattern we need to preserve it
72 if isinstance(urlpattern.pattern, RoutePattern):
73 assert path is not None
74 assert suffix_route is not None
75 route = str(urlpattern.pattern).rstrip('$').rstrip('/') + suffix_route
76 new_pattern = path(route, view, kwargs, name)
77 else:
78 new_pattern = re_path(regex, view, kwargs, name)
79
80 ret.append(new_pattern)
81
82 return ret
83
84
85def format_suffix_patterns(urlpatterns, suffix_required=False, allowed=None):

Callers 1

format_suffix_patternsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected