def test_included_urls_django2_mixed_args(self):
        nested_patterns = [
            path('path/<int:child>', dummy_view),
            url('^url/(?P<child>[0-9]+)$', dummy_view)
        ]
        urlpatterns = [
            url('^purl/(?P<parent>[0-9]+)/', include(nested_patterns), {'foo': 'bar'}),
            path('ppath/<int:parent>/', include(nested_patterns), {'foo': 'bar'}),
        ]
        test_paths = [
            # parent url() nesting child path()
            URLTestPath('/purl/87/path/42', (), {'parent': '87', 'child': 42, 'foo': 'bar', }),
            URLTestPath('/purl/87/path/42.api', (), {'parent': '87', 'child': 42, 'foo': 'bar', 'format': 'api'}),
            URLTestPath('/purl/87/path/42.asdf', (), {'parent': '87', 'child': 42, 'foo': 'bar', 'format': 'asdf'}),

            # parent path() nesting child url()
            URLTestPath('/ppath/87/url/42', (), {'parent': 87, 'child': '42', 'foo': 'bar', }),
            URLTestPath('/ppath/87/url/42.api', (), {'parent': 87, 'child': '42', 'foo': 'bar', 'format': 'api'}),
            URLTestPath('/ppath/87/url/42.asdf', (), {'parent': 87, 'child': '42', 'foo': 'bar', 'format': 'asdf'}),

            # parent path() nesting child path()
            URLTestPath('/ppath/87/path/42', (), {'parent': 87, 'child': 42, 'foo': 'bar', }),
            URLTestPath('/ppath/87/path/42.api', (), {'parent': 87, 'child': 42, 'foo': 'bar', 'format': 'api'}),
            URLTestPath('/ppath/87/path/42.asdf', (), {'parent': 87, 'child': 42, 'foo': 'bar', 'format': 'asdf'}),

            # parent url() nesting child url()
            URLTestPath('/purl/87/url/42', (), {'parent': '87', 'child': '42', 'foo': 'bar', }),
            URLTestPath('/purl/87/url/42.api', (), {'parent': '87', 'child': '42', 'foo': 'bar', 'format': 'api'}),
            URLTestPath('/purl/87/url/42.asdf', (), {'parent': '87', 'child': '42', 'foo': 'bar', 'format': 'asdf'}),
        ]
        self._resolve_urlpatterns(urlpatterns, test_paths)
 def test_included_urls_django2(self):
     nested_patterns = [
         path('path', dummy_view)
     ]
     urlpatterns = [
         path('test/', include(nested_patterns), {'foo': 'bar'}),
     ]
     self._test_included_urls(urlpatterns)
Esempio n. 3
0
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_format_suffix_django2_args(self):
     urlpatterns = [
         path('convtest/<int:pk>', dummy_view),
         re_path(r'^retest/(?P<pk>[0-9]+)$', dummy_view),
     ]
     test_paths = [
         URLTestPath('/convtest/42', (), {'pk': 42}),
         URLTestPath('/convtest/42.api', (), {
             'pk': 42,
             'format': 'api'
         }),
         URLTestPath('/convtest/42.asdf', (), {
             'pk': 42,
             'format': 'asdf'
         }),
         URLTestPath('/retest/42', (), {'pk': '42'}),
         URLTestPath('/retest/42.api', (), {
             'pk': '42',
             'format': 'api'
         }),
         URLTestPath('/retest/42.asdf', (), {
             'pk': '42',
             'format': 'asdf'
         }),
     ]
     self._resolve_urlpatterns(urlpatterns, test_paths)
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_format_suffix_django2_args(self):
     urlpatterns = [
         path('convtest/<int:pk>', dummy_view),
         re_path(r'^retest/(?P<pk>[0-9]+)$', dummy_view),
     ]
     test_paths = [
         URLTestPath('/convtest/42', (), {'pk': 42}),
         URLTestPath('/convtest/42.api', (), {'pk': 42, 'format': 'api'}),
         URLTestPath('/convtest/42.asdf', (), {'pk': 42, 'format': 'asdf'}),
         URLTestPath('/retest/42', (), {'pk': '42'}),
         URLTestPath('/retest/42.api', (), {'pk': '42', 'format': 'api'}),
         URLTestPath('/retest/42.asdf', (), {'pk': '42', 'format': 'asdf'}),
     ]
     self._resolve_urlpatterns(urlpatterns, test_paths)
 def test_default_args_django2(self):
     urlpatterns = [
         path('test', dummy_view, {'foo': 'bar'}),
     ]
     self._test_default_args(urlpatterns)
 def setUp(self):
     self.patterns = [
         path('example/', ExampleListView.as_view()),
         path('example/<int:pk>/', ExampleDetailView.as_view()),
         path('example/<int:pk>/sub/', ExampleDetailView.as_view()),
     ]
 def setUp(self):
     self.patterns = [
         path('example/', ExampleListView.as_view()),
         path('example/<int:pk>/', ExampleDetailView.as_view()),
         path('example/<int:pk>/sub/', ExampleDetailView.as_view()),
     ]
 def test_format_suffix_django2(self):
     urlpatterns = [
         path('test', dummy_view),
     ]
     self._test_format_suffix(urlpatterns)
 def test_trailing_slash_django2(self):
     urlpatterns = [
         path('test/', dummy_view),
     ]
     self._test_trailing_slash(urlpatterns)
 def test_allowed_formats_django2(self):
     urlpatterns = [
         path('test', dummy_view),
     ]
     self._test_allowed_formats(urlpatterns)
    def test_included_urls_django2_mixed_args(self):
        nested_patterns = [
            path('path/<int:child>', dummy_view),
            url('^url/(?P<child>[0-9]+)$', dummy_view)
        ]
        urlpatterns = [
            url('^purl/(?P<parent>[0-9]+)/', include(nested_patterns),
                {'foo': 'bar'}),
            path('ppath/<int:parent>/', include(nested_patterns),
                 {'foo': 'bar'}),
        ]
        test_paths = [
            # parent url() nesting child path()
            URLTestPath('/purl/87/path/42', (), {
                'parent': '87',
                'child': 42,
                'foo': 'bar',
            }),
            URLTestPath('/purl/87/path/42.api', (), {
                'parent': '87',
                'child': 42,
                'foo': 'bar',
                'format': 'api'
            }),
            URLTestPath('/purl/87/path/42.asdf', (), {
                'parent': '87',
                'child': 42,
                'foo': 'bar',
                'format': 'asdf'
            }),

            # parent path() nesting child url()
            URLTestPath('/ppath/87/url/42', (), {
                'parent': 87,
                'child': '42',
                'foo': 'bar',
            }),
            URLTestPath('/ppath/87/url/42.api', (), {
                'parent': 87,
                'child': '42',
                'foo': 'bar',
                'format': 'api'
            }),
            URLTestPath('/ppath/87/url/42.asdf', (), {
                'parent': 87,
                'child': '42',
                'foo': 'bar',
                'format': 'asdf'
            }),

            # parent path() nesting child path()
            URLTestPath('/ppath/87/path/42', (), {
                'parent': 87,
                'child': 42,
                'foo': 'bar',
            }),
            URLTestPath('/ppath/87/path/42.api', (), {
                'parent': 87,
                'child': 42,
                'foo': 'bar',
                'format': 'api'
            }),
            URLTestPath('/ppath/87/path/42.asdf', (), {
                'parent': 87,
                'child': 42,
                'foo': 'bar',
                'format': 'asdf'
            }),

            # parent url() nesting child url()
            URLTestPath('/purl/87/url/42', (), {
                'parent': '87',
                'child': '42',
                'foo': 'bar',
            }),
            URLTestPath('/purl/87/url/42.api', (), {
                'parent': '87',
                'child': '42',
                'foo': 'bar',
                'format': 'api'
            }),
            URLTestPath('/purl/87/url/42.asdf', (), {
                'parent': '87',
                'child': '42',
                'foo': 'bar',
                'format': 'asdf'
            }),
        ]
        self._resolve_urlpatterns(urlpatterns, test_paths)
 def test_included_urls_django2_mixed(self):
     nested_patterns = [path('path', dummy_view)]
     urlpatterns = [
         url('^test/', include(nested_patterns), {'foo': 'bar'}),
     ]
     self._test_included_urls(urlpatterns)
 def test_default_args_django2(self):
     urlpatterns = [
         path('test', dummy_view, {'foo': 'bar'}),
     ]
     self._test_default_args(urlpatterns)
 def test_allowed_formats_django2(self):
     urlpatterns = [
         path('test', dummy_view),
     ]
     self._test_allowed_formats(urlpatterns)
 def test_format_suffix_django2(self):
     urlpatterns = [
         path('test', dummy_view),
     ]
     self._test_format_suffix(urlpatterns)
 def test_trailing_slash_django2(self):
     urlpatterns = [
         path('test/', dummy_view),
     ]
     self._test_trailing_slash(urlpatterns)