mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 21:24:33 +03:00
Merge pull request #5187 from mathpresso-mom/list_route_regex
Fix list_route, detail_route with kwargs contains curly bracket in url_path
This commit is contained in:
commit
9c9525b130
|
@ -35,6 +35,15 @@ DynamicDetailRoute = namedtuple('DynamicDetailRoute', ['url', 'name', 'initkwarg
|
||||||
DynamicListRoute = namedtuple('DynamicListRoute', ['url', 'name', 'initkwargs'])
|
DynamicListRoute = namedtuple('DynamicListRoute', ['url', 'name', 'initkwargs'])
|
||||||
|
|
||||||
|
|
||||||
|
def escape_curly_brackets(url_path):
|
||||||
|
"""
|
||||||
|
Double brackets in regex of url_path for escape string formatting
|
||||||
|
"""
|
||||||
|
if ('{' and '}') in url_path:
|
||||||
|
url_path = url_path.replace('{', '{{').replace('}', '}}')
|
||||||
|
return url_path
|
||||||
|
|
||||||
|
|
||||||
def replace_methodname(format_string, methodname):
|
def replace_methodname(format_string, methodname):
|
||||||
"""
|
"""
|
||||||
Partially format a format_string, swapping out any
|
Partially format a format_string, swapping out any
|
||||||
|
@ -178,6 +187,7 @@ class SimpleRouter(BaseRouter):
|
||||||
initkwargs = route.initkwargs.copy()
|
initkwargs = route.initkwargs.copy()
|
||||||
initkwargs.update(method_kwargs)
|
initkwargs.update(method_kwargs)
|
||||||
url_path = initkwargs.pop("url_path", None) or methodname
|
url_path = initkwargs.pop("url_path", None) or methodname
|
||||||
|
url_path = escape_curly_brackets(url_path)
|
||||||
url_name = initkwargs.pop("url_name", None) or url_path
|
url_name = initkwargs.pop("url_name", None) or url_path
|
||||||
ret.append(Route(
|
ret.append(Route(
|
||||||
url=replace_methodname(route.url, url_path),
|
url=replace_methodname(route.url, url_path),
|
||||||
|
|
|
@ -65,6 +65,19 @@ class EmptyPrefixViewSet(viewsets.ModelViewSet):
|
||||||
return self.queryset[index]
|
return self.queryset[index]
|
||||||
|
|
||||||
|
|
||||||
|
class RegexUrlPathViewSet(viewsets.ViewSet):
|
||||||
|
@list_route(url_path='list/(?P<kwarg>[0-9]{4})')
|
||||||
|
def regex_url_path_list(self, request, *args, **kwargs):
|
||||||
|
kwarg = self.kwargs.get('kwarg', '')
|
||||||
|
return Response({'kwarg': kwarg})
|
||||||
|
|
||||||
|
@detail_route(url_path='detail/(?P<kwarg>[0-9]{4})')
|
||||||
|
def regex_url_path_detail(self, request, *args, **kwargs):
|
||||||
|
pk = self.kwargs.get('pk', '')
|
||||||
|
kwarg = self.kwargs.get('kwarg', '')
|
||||||
|
return Response({'pk': pk, 'kwarg': kwarg})
|
||||||
|
|
||||||
|
|
||||||
notes_router = SimpleRouter()
|
notes_router = SimpleRouter()
|
||||||
notes_router.register(r'notes', NoteViewSet)
|
notes_router.register(r'notes', NoteViewSet)
|
||||||
|
|
||||||
|
@ -80,6 +93,9 @@ empty_prefix_urls = [
|
||||||
url(r'^', include(empty_prefix_router.urls)),
|
url(r'^', include(empty_prefix_router.urls)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
regex_url_path_router = SimpleRouter()
|
||||||
|
regex_url_path_router.register(r'', RegexUrlPathViewSet, base_name='regex')
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^non-namespaced/', include(namespaced_router.urls)),
|
url(r'^non-namespaced/', include(namespaced_router.urls)),
|
||||||
url(r'^namespaced/', include(namespaced_router.urls, namespace='example', app_name='example')),
|
url(r'^namespaced/', include(namespaced_router.urls, namespace='example', app_name='example')),
|
||||||
|
@ -87,6 +103,7 @@ urlpatterns = [
|
||||||
url(r'^example2/', include(kwarged_notes_router.urls)),
|
url(r'^example2/', include(kwarged_notes_router.urls)),
|
||||||
|
|
||||||
url(r'^empty-prefix/', include(empty_prefix_urls)),
|
url(r'^empty-prefix/', include(empty_prefix_urls)),
|
||||||
|
url(r'^regex/', include(regex_url_path_router.urls))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -402,3 +419,19 @@ class TestEmptyPrefix(TestCase):
|
||||||
response = self.client.get('/empty-prefix/1/')
|
response = self.client.get('/empty-prefix/1/')
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert json.loads(response.content.decode('utf-8')) == {'uuid': '111', 'text': 'First'}
|
assert json.loads(response.content.decode('utf-8')) == {'uuid': '111', 'text': 'First'}
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(ROOT_URLCONF='tests.test_routers')
|
||||||
|
class TestRegexUrlPath(TestCase):
|
||||||
|
def test_regex_url_path_list(self):
|
||||||
|
kwarg = '1234'
|
||||||
|
response = self.client.get('/regex/list/{}/'.format(kwarg))
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json.loads(response.content.decode('utf-8')) == {'kwarg': kwarg}
|
||||||
|
|
||||||
|
def test_regex_url_path_detail(self):
|
||||||
|
pk = '1'
|
||||||
|
kwarg = '1234'
|
||||||
|
response = self.client.get('/regex/{}/detail/{}/'.format(pk, kwarg))
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert json.loads(response.content.decode('utf-8')) == {'pk': pk, 'kwarg': kwarg}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user