diff options
| author | Yannick PEROUX | 2015-02-25 13:29:07 +0100 | 
|---|---|---|
| committer | Yannick PEROUX | 2015-02-25 13:29:07 +0100 | 
| commit | 940cf2e2e004f913d3cc260fa2b490d33a163b51 (patch) | |
| tree | 7c515fc9d3b122f3a7d2ea1ef84abe68581ff789 /rest_framework | |
| parent | 90f1c04c6b59cc0cadbb5e36251b130d96307943 (diff) | |
| download | django-rest-framework-940cf2e2e004f913d3cc260fa2b490d33a163b51.tar.bz2 | |
Remove duplicated code in routers.SimpleRouter
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/routers.py | 40 | 
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) | 
