Skip to content

Commit

Permalink
Remove duplicated code in routers.SimpleRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
k4nar committed Feb 25, 2015
1 parent 90f1c04 commit 940cf2e
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions rest_framework/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,34 +165,30 @@ def get_routes(self, viewset):
else:
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 = []
for route in self.routes:
if isinstance(route, DynamicDetailRoute):
# Dynamic detail routes (@detail_route decorator)
for httpmethods, methodname in 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,
))
ret += _get_dynamic_routes(route, detail_routes)
elif isinstance(route, DynamicListRoute):
# Dynamic list routes (@list_route decorator)
for httpmethods, methodname in 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,
))
ret += _get_dynamic_routes(route, list_routes)
else:
# Standard route
ret.append(route)
Expand Down

0 comments on commit 940cf2e

Please sign in to comment.