Remove duplicated code in routers.SimpleRouter

This commit is contained in:
Yannick PEROUX 2015-02-25 13:29:07 +01:00
parent 90f1c04c6b
commit 940cf2e2e0

View File

@ -165,34 +165,30 @@ class SimpleRouter(BaseRouter):
else: else:
list_routes.append((httpmethods, methodname)) list_routes.append((httpmethods, methodname))
def _get_dynamic_routes(route, dynamic_routes):
ret = []
for httpmethods, methodname in dynamic_routes:
method_kwargs = getattr(viewset, methodname).kwargs
initkwargs = route.initkwargs.copy()
initkwargs.update(method_kwargs)
url_path = initkwargs.pop("url_path", None) or methodname
ret.append(Route(
url=replace_methodname(route.url, url_path),
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
name=replace_methodname(route.name, url_path),
initkwargs=initkwargs,
))
return ret
ret = [] ret = []
for route in self.routes: for route in self.routes:
if isinstance(route, DynamicDetailRoute): if isinstance(route, DynamicDetailRoute):
# Dynamic detail routes (@detail_route decorator) # Dynamic detail routes (@detail_route decorator)
for httpmethods, methodname in detail_routes: ret += _get_dynamic_routes(route, detail_routes)
method_kwargs = getattr(viewset, methodname).kwargs
initkwargs = route.initkwargs.copy()
initkwargs.update(method_kwargs)
url_path = initkwargs.pop("url_path", None) or methodname
ret.append(Route(
url=replace_methodname(route.url, url_path),
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
name=replace_methodname(route.name, url_path),
initkwargs=initkwargs,
))
elif isinstance(route, DynamicListRoute): elif isinstance(route, DynamicListRoute):
# Dynamic list routes (@list_route decorator) # Dynamic list routes (@list_route decorator)
for httpmethods, methodname in list_routes: ret += _get_dynamic_routes(route, list_routes)
method_kwargs = getattr(viewset, methodname).kwargs
initkwargs = route.initkwargs.copy()
initkwargs.update(method_kwargs)
url_path = initkwargs.pop("url_path", None) or methodname
ret.append(Route(
url=replace_methodname(route.url, url_path),
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
name=replace_methodname(route.name, url_path),
initkwargs=initkwargs,
))
else: else:
# Standard route # Standard route
ret.append(route) ret.append(route)