Revert route name change and add key to Route object to identify different route types

This commit is contained in:
Alex Burgel 2013-06-16 12:43:59 -04:00
parent 57cf8b5fa4
commit 8d521c068a

View File

@ -25,7 +25,7 @@ from rest_framework.reverse import reverse
from rest_framework.urlpatterns import format_suffix_patterns from rest_framework.urlpatterns import format_suffix_patterns
Route = namedtuple('Route', ['url', 'mapping', 'name', 'initkwargs']) Route = namedtuple('Route', ['key', 'url', 'mapping', 'name', 'initkwargs'])
def replace_methodname(format_string, methodname): def replace_methodname(format_string, methodname):
@ -80,6 +80,7 @@ class SimpleRouter(BaseRouter):
routes = [ routes = [
# List route. # List route.
Route( Route(
key='list',
url=r'^{prefix}{trailing_slash}$', url=r'^{prefix}{trailing_slash}$',
mapping={ mapping={
'get': 'list', 'get': 'list',
@ -92,15 +93,17 @@ class SimpleRouter(BaseRouter):
# Generated using @collection_action or @collection_link decorators # Generated using @collection_action or @collection_link decorators
# on methods of the viewset. # on methods of the viewset.
Route( Route(
key='collection',
url=r'^{prefix}/{methodname}{trailing_slash}$', url=r'^{prefix}/{methodname}{trailing_slash}$',
mapping={ mapping={
'{httpmethod}': '{methodname}', '{httpmethod}': '{methodname}',
}, },
name='{basename}-collection-{methodnamehyphen}', name='{basename}-{methodnamehyphen}',
initkwargs={} initkwargs={}
), ),
# Detail route. # Detail route.
Route( Route(
key='detail',
url=r'^{prefix}/{lookup}{trailing_slash}$', url=r'^{prefix}/{lookup}{trailing_slash}$',
mapping={ mapping={
'get': 'retrieve', 'get': 'retrieve',
@ -114,11 +117,12 @@ class SimpleRouter(BaseRouter):
# Dynamically generated routes. # Dynamically generated routes.
# Generated using @action or @link decorators on methods of the viewset. # Generated using @action or @link decorators on methods of the viewset.
Route( Route(
key='dynamic',
url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$', url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$',
mapping={ mapping={
'{httpmethod}': '{methodname}', '{httpmethod}': '{methodname}',
}, },
name='{basename}-dynamic-{methodnamehyphen}', name='{basename}-{methodnamehyphen}',
initkwargs={} initkwargs={}
), ),
] ]
@ -171,23 +175,25 @@ class SimpleRouter(BaseRouter):
ret = [] ret = []
for route in self.routes: for route in self.routes:
if route.name == '{basename}-dynamic-{methodnamehyphen}': if route.key == 'dynamic':
# Dynamic routes (@link or @action decorator) # Dynamic routes (@link or @action decorator)
for httpmethods, methodname in dynamic_routes: for httpmethods, methodname in dynamic_routes:
initkwargs = route.initkwargs.copy() initkwargs = route.initkwargs.copy()
initkwargs.update(getattr(viewset, methodname).kwargs) initkwargs.update(getattr(viewset, methodname).kwargs)
ret.append(Route( ret.append(Route(
key=route.key,
url=replace_methodname(route.url, methodname), url=replace_methodname(route.url, methodname),
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
name=replace_methodname(route.name, methodname), name=replace_methodname(route.name, methodname),
initkwargs=initkwargs, initkwargs=initkwargs,
)) ))
elif route.name == '{basename}-collection-{methodnamehyphen}': elif route.key == 'collection':
# Dynamic routes (@collection_link or @collection_action decorator) # Dynamic routes (@collection_link or @collection_action decorator)
for httpmethods, methodname in collection_routes: for httpmethods, methodname in collection_routes:
initkwargs = route.initkwargs.copy() initkwargs = route.initkwargs.copy()
initkwargs.update(getattr(viewset, methodname).kwargs) initkwargs.update(getattr(viewset, methodname).kwargs)
ret.append(Route( ret.append(Route(
key=route.key,
url=replace_methodname(route.url, methodname), url=replace_methodname(route.url, methodname),
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
name=replace_methodname(route.name, methodname), name=replace_methodname(route.name, methodname),