Ability to use Router with empty path with and without trailing_slash

This commit is contained in:
Artem Homenko 2017-02-06 15:08:34 +02:00
parent e946a496dc
commit 93d568c7d4

View File

@ -96,13 +96,13 @@ class SimpleRouter(BaseRouter):
# Generated using @list_route decorator # Generated using @list_route decorator
# on methods of the viewset. # on methods of the viewset.
DynamicListRoute( DynamicListRoute(
url=r'^{prefix}/{methodname}{trailing_slash}$', url=r'^{prefix}{methodname}{trailing_slash}$',
name='{basename}-{methodnamehyphen}', name='{basename}-{methodnamehyphen}',
initkwargs={} initkwargs={}
), ),
# Detail route. # Detail route.
Route( Route(
url=r'^{prefix}/{lookup}{trailing_slash}$', url=r'^{prefix}{lookup}{trailing_slash}$',
mapping={ mapping={
'get': 'retrieve', 'get': 'retrieve',
'put': 'update', 'put': 'update',
@ -115,7 +115,7 @@ class SimpleRouter(BaseRouter):
# Dynamically generated detail routes. # Dynamically generated detail routes.
# Generated using @detail_route decorator on methods of the viewset. # Generated using @detail_route decorator on methods of the viewset.
DynamicDetailRoute( DynamicDetailRoute(
url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$', url=r'^{prefix}{lookup}/{methodname}{trailing_slash}$',
name='{basename}-{methodnamehyphen}', name='{basename}-{methodnamehyphen}',
initkwargs={} initkwargs={}
), ),
@ -247,11 +247,18 @@ class SimpleRouter(BaseRouter):
continue continue
# Build the url pattern # Build the url pattern
if prefix:
prefix += "/"
regex = route.url.format( regex = route.url.format(
prefix=prefix, prefix=prefix,
lookup=lookup, lookup=lookup,
trailing_slash=self.trailing_slash trailing_slash=self.trailing_slash
) )
if regex == '^/$':
regex = '^$'
view = viewset.as_view(mapping, **route.initkwargs) view = viewset.as_view(mapping, **route.initkwargs)
name = route.name.format(basename=basename) name = route.name.format(basename=basename)
ret.append(url(regex, view, name=name)) ret.append(url(regex, view, name=name))