def apply_suffix_patterns(urlpatterns, suffix_pattern, suffix_required, suffix_route=None): ret = [] for urlpattern in urlpatterns: if isinstance(urlpattern, URLResolver): # Set of included URL patterns regex = get_regex_pattern(urlpattern) namespace = urlpattern.namespace app_name = urlpattern.app_name kwargs = urlpattern.default_kwargs # Add in the included patterns, after applying the suffixes patterns = apply_suffix_patterns(urlpattern.url_patterns, suffix_pattern, suffix_required, suffix_route) # if the original pattern was a RoutePattern we need to preserve it if is_route_pattern(urlpattern): assert path is not None route = str(urlpattern.pattern) new_pattern = path(route, include((patterns, app_name), namespace), kwargs) else: new_pattern = url(regex, include((patterns, app_name), namespace), kwargs) ret.append(new_pattern) else: # Regular URL pattern regex = get_regex_pattern(urlpattern).rstrip('$').rstrip('/') + suffix_pattern view = urlpattern.callback kwargs = urlpattern.default_args name = urlpattern.name # Add in both the existing and the new urlpattern if not suffix_required: ret.append(urlpattern) # if the original pattern was a RoutePattern we need to preserve it if is_route_pattern(urlpattern): assert path is not None assert suffix_route is not None route = str(urlpattern.pattern).rstrip('$').rstrip('/') + suffix_route new_pattern = path(route, view, kwargs, name) else: new_pattern = url(regex, view, kwargs, name) ret.append(new_pattern) return ret
def test_urls_can_have_trailing_slash_removed(self): expected = ['^notes$', '^notes/(?P<pk>[^/.]+)$'] for idx in range(len(expected)): assert expected[idx] == get_regex_pattern(self.urls[idx])
def test_custom_lookup_url_kwarg_route(self): detail_route = kwarged_notes_router.urls[-1] detail_url_pattern = get_regex_pattern(detail_route) assert '^notes/(?P<text>' in detail_url_pattern
def test_urls_limited_by_lookup_value_regex(self): expected = ['^notes/$', '^notes/(?P<uuid>[0-9a-f]{32})/$'] for idx in range(len(expected)): assert expected[idx] == get_regex_pattern(self.urls[idx])
def test_custom_lookup_field_route(self): detail_route = notes_router.urls[-1] detail_url_pattern = get_regex_pattern(detail_route) assert '<uuid>' in detail_url_pattern