This commit is contained in:
Aleksey Kladov 2013-08-26 03:48:51 -07:00
commit 777d354e46
5 changed files with 36 additions and 9 deletions

View File

@ -53,6 +53,14 @@ The following URL pattern would additionally be generated:
* URL pattern: `^users/{pk}/set_password/$` Name: `'user-set-password'` * URL pattern: `^users/{pk}/set_password/$` Name: `'user-set-password'`
You can customize endpoint name by supplying `endpoint` argument like this:
@action(endpoint='fancy-name')
def do_stuff(self, request, pk=None):
...
* URL pattern: `^users/{pk}/fancy-name/$` Name: `'user-do-stuff'`
# API Guide # API Guide
## SimpleRouter ## SimpleRouter

View File

@ -142,6 +142,12 @@ The `@action` decorator will route `POST` requests by default, but may also acce
@action(methods=['POST', 'DELETE']) @action(methods=['POST', 'DELETE'])
def unset_password(self, request, pk=None): def unset_password(self, request, pk=None):
... ...
By default endpoint match method name, but this can be overridden with `endpoint` argument.
@action(endpoint='fancy-name')
def do_stuff(self, request, pk=None):
...
--- ---
# API Reference # API Reference

View File

@ -107,23 +107,25 @@ def permission_classes(permission_classes):
return decorator return decorator
def link(**kwargs): def link(endpoint=None, **kwargs):
""" """
Used to mark a method on a ViewSet that should be routed for GET requests. Used to mark a method on a ViewSet that should be routed for GET requests.
""" """
def decorator(func): def decorator(func):
func.bind_to_methods = ['get'] func.bind_to_methods = ['get']
func.kwargs = kwargs func.kwargs = kwargs
func.endpoint = endpoint or func.__name__
return func return func
return decorator return decorator
def action(methods=['post'], **kwargs): def action(methods=['post'], endpoint=None, **kwargs):
""" """
Used to mark a method on a ViewSet that should be routed for POST requests. Used to mark a method on a ViewSet that should be routed for POST requests.
""" """
def decorator(func): def decorator(func):
func.bind_to_methods = methods func.bind_to_methods = methods
func.kwargs = kwargs func.kwargs = kwargs
func.endpoint = endpoint or func.__name__
return func return func
return decorator return decorator

View File

@ -147,22 +147,24 @@ class SimpleRouter(BaseRouter):
attr = getattr(viewset, methodname) attr = getattr(viewset, methodname)
httpmethods = getattr(attr, 'bind_to_methods', None) httpmethods = getattr(attr, 'bind_to_methods', None)
if httpmethods: if httpmethods:
if methodname in known_actions: endpoint = getattr(attr, 'endpoint')
if endpoint in known_actions:
raise ImproperlyConfigured('Cannot use @action or @link decorator on ' raise ImproperlyConfigured('Cannot use @action or @link decorator on '
'method "%s" as it is an existing route' % methodname) 'method "%s" as %s is an existing route'
% (methodname, endpoint))
httpmethods = [method.lower() for method in httpmethods] httpmethods = [method.lower() for method in httpmethods]
dynamic_routes.append((httpmethods, methodname)) dynamic_routes.append((httpmethods, methodname, endpoint))
ret = [] ret = []
for route in self.routes: for route in self.routes:
if route.mapping == {'{httpmethod}': '{methodname}'}: if route.mapping == {'{httpmethod}': '{methodname}'}:
# Dynamic routes (@link or @action decorator) # Dynamic routes (@link or @action decorator)
for httpmethods, methodname in dynamic_routes: for httpmethods, methodname, endpoint in dynamic_routes:
initkwargs = route.initkwargs.copy() initkwargs = route.initkwargs.copy()
initkwargs.update(getattr(viewset, methodname).kwargs) initkwargs.update(getattr(viewset, methodname).kwargs)
ret.append(Route( ret.append(Route(
url=replace_methodname(route.url, methodname), url=replace_methodname(route.url, endpoint),
mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), mapping=dict((httpmethod, endpoint) for httpmethod in httpmethods),
name=replace_methodname(route.name, methodname), name=replace_methodname(route.name, methodname),
initkwargs=initkwargs, initkwargs=initkwargs,
)) ))

View File

@ -30,6 +30,10 @@ class BasicViewSet(viewsets.ViewSet):
def action3(self, request, *args, **kwargs): def action3(self, request, *args, **kwargs):
return Response({'method': 'action2'}) return Response({'method': 'action2'})
@action(endpoint='action-name')
def action4(self, request, *args, **kwargs):
return Response({'method': 'action4'})
@link() @link()
def link1(self, request, *args, **kwargs): def link1(self, request, *args, **kwargs):
return Response({'method': 'link1'}) return Response({'method': 'link1'})
@ -38,6 +42,10 @@ class BasicViewSet(viewsets.ViewSet):
def link2(self, request, *args, **kwargs): def link2(self, request, *args, **kwargs):
return Response({'method': 'link2'}) return Response({'method': 'link2'})
@link(endpoint='link-name')
def link3(self, request, *args, **kwargs):
return Response({'method': 'link3'})
class TestSimpleRouter(TestCase): class TestSimpleRouter(TestCase):
def setUp(self): def setUp(self):
@ -47,7 +55,8 @@ class TestSimpleRouter(TestCase):
routes = self.router.get_routes(BasicViewSet) routes = self.router.get_routes(BasicViewSet)
decorator_routes = routes[2:] decorator_routes = routes[2:]
# Make sure all these endpoints exist and none have been clobbered # Make sure all these endpoints exist and none have been clobbered
for i, endpoint in enumerate(['action1', 'action2', 'action3', 'link1', 'link2']): for i, endpoint in enumerate(['action1', 'action2', 'action3', 'action-name',
'link1', 'link2', 'link-name']):
route = decorator_routes[i] route = decorator_routes[i]
# check url listing # check url listing
self.assertEqual(route.url, self.assertEqual(route.url,