aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework
diff options
context:
space:
mode:
authorTom Christie2015-02-25 13:04:49 +0000
committerTom Christie2015-02-25 13:04:49 +0000
commit5688d8b73648c67fbe5ea003916dae04a7c5bba0 (patch)
tree7c515fc9d3b122f3a7d2ea1ef84abe68581ff789 /rest_framework
parent90f1c04c6b59cc0cadbb5e36251b130d96307943 (diff)
parent940cf2e2e004f913d3cc260fa2b490d33a163b51 (diff)
downloaddjango-rest-framework-5688d8b73648c67fbe5ea003916dae04a7c5bba0.tar.bz2
Merge pull request #2601 from k4nar/refactor_simplerouter
Remove duplicated code in routers.SimpleRouter
Diffstat (limited to 'rest_framework')
-rw-r--r--rest_framework/routers.py40
1 files changed, 18 insertions, 22 deletions
diff --git a/rest_framework/routers.py b/rest_framework/routers.py
index 081654b8..b1e39ff7 100644
--- a/rest_framework/routers.py
+++ b/rest_framework/routers.py
@@ -165,34 +165,30 @@ class SimpleRouter(BaseRouter):
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)