Skip to content

Commit

Permalink
Use actual url_path/url_name in lieu of methodname
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Nov 18, 2017
1 parent eafe0cc commit a8dff35
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions rest_framework/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ def escape_curly_brackets(url_path):
return url_path


def replace_methodname(format_string, methodname):
"""
Partially format a format_string, swapping out any
'{methodname}' or '{methodnamehyphen}' components.
"""
methodnamehyphen = methodname.replace('_', '-')
ret = format_string
ret = ret.replace('{methodname}', methodname)
ret = ret.replace('{methodnamehyphen}', methodnamehyphen)
return ret


def flatten(list_of_lists):
"""
Takes an iterable of iterables, returns a single iterable containing all items
Expand Down Expand Up @@ -130,8 +118,8 @@ class SimpleRouter(BaseRouter):
# Generated using @list_route decorator
# on methods of the viewset.
DynamicRoute(
url=r'^{prefix}/{methodname}{trailing_slash}$',
name='{basename}-{methodnamehyphen}',
url=r'^{prefix}/{url_path}{trailing_slash}$',
name='{basename}-{url_name}',
detail=False,
initkwargs={}
),
Expand All @@ -151,8 +139,8 @@ class SimpleRouter(BaseRouter):
# Dynamically generated detail routes.
# Generated using @detail_route decorator on methods of the viewset.
DynamicRoute(
url=r'^{prefix}/{lookup}/{methodname}{trailing_slash}$',
name='{basename}-{methodnamehyphen}',
url=r'^{prefix}/{lookup}/{url_path}{trailing_slash}$',
name='{basename}-{url_name}',
detail=True,
initkwargs={}
),
Expand Down Expand Up @@ -219,10 +207,10 @@ def _get_dynamic_route(self, route, action):
url_path = escape_curly_brackets(action.url_path)

return Route(
url=replace_methodname(route.url, url_path),
url=route.url.replace('{url_path}', url_path),
mapping={http_method: action.__name__
for http_method in action.bind_to_methods},
name=replace_methodname(route.name, action.url_name),
name=route.name.replace('{url_name}', action.url_name),
detail=route.detail,
initkwargs=initkwargs,
)
Expand Down

0 comments on commit a8dff35

Please sign in to comment.