aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Burgel2013-05-24 18:28:47 -0400
committerAlex Burgel2013-05-24 18:28:47 -0400
commit112b52f57e8f5a4a54e2b0c40f442ee63daf9709 (patch)
treea16037fd97cb8bd46710ebb6097046a94e717f64
parent02b29267acd3e3afabe6497a6ed04b10888d8731 (diff)
downloaddjango-rest-framework-112b52f57e8f5a4a54e2b0c40f442ee63daf9709.tar.bz2
Allow action decorator to handle multiple http methods
-rw-r--r--rest_framework/decorators.py6
-rw-r--r--rest_framework/routers.py10
-rw-r--r--rest_framework/tests/routers.py19
3 files changed, 20 insertions, 15 deletions
diff --git a/rest_framework/decorators.py b/rest_framework/decorators.py
index 81e585e1..8f79c817 100644
--- a/rest_framework/decorators.py
+++ b/rest_framework/decorators.py
@@ -112,18 +112,18 @@ def link(**kwargs):
Used to mark a method on a ViewSet that should be routed for GET requests.
"""
def decorator(func):
- func.bind_to_method = 'get'
+ func.bind_to_methods = ['get']
func.kwargs = kwargs
return func
return decorator
-def action(**kwargs):
+def action(methods=['post'], **kwargs):
"""
Used to mark a method on a ViewSet that should be routed for POST requests.
"""
def decorator(func):
- func.bind_to_method = 'post'
+ func.bind_to_methods = methods
func.kwargs = kwargs
return func
return decorator
diff --git a/rest_framework/routers.py b/rest_framework/routers.py
index dba104c3..6c5fd004 100644
--- a/rest_framework/routers.py
+++ b/rest_framework/routers.py
@@ -131,20 +131,20 @@ class SimpleRouter(BaseRouter):
dynamic_routes = []
for methodname in dir(viewset):
attr = getattr(viewset, methodname)
- httpmethod = getattr(attr, 'bind_to_method', None)
- if httpmethod:
- dynamic_routes.append((httpmethod, methodname))
+ httpmethods = getattr(attr, 'bind_to_methods', None)
+ if httpmethods:
+ dynamic_routes.append((httpmethods, methodname))
ret = []
for route in self.routes:
if route.mapping == {'{httpmethod}': '{methodname}'}:
# Dynamic routes (@link or @action decorator)
- for httpmethod, methodname in dynamic_routes:
+ for httpmethods, methodname in dynamic_routes:
initkwargs = route.initkwargs.copy()
initkwargs.update(getattr(viewset, methodname).kwargs)
ret.append(Route(
url=replace_methodname(route.url, methodname),
- mapping={httpmethod: methodname},
+ mapping=dict((httpmethod, methodname) for httpmethod in httpmethods),
name=replace_methodname(route.name, methodname),
initkwargs=initkwargs,
))
diff --git a/rest_framework/tests/routers.py b/rest_framework/tests/routers.py
index 4e4765cb..9f9ced0f 100644
--- a/rest_framework/tests/routers.py
+++ b/rest_framework/tests/routers.py
@@ -23,6 +23,10 @@ class BasicViewSet(viewsets.ViewSet):
def action2(self, request, *args, **kwargs):
return Response({'method': 'action2'})
+ @action(methods=['post', 'delete'])
+ def action3(self, request, *args, **kwargs):
+ return Response({'method': 'action2'})
+
@link()
def link1(self, request, *args, **kwargs):
return Response({'method': 'link1'})
@@ -40,16 +44,17 @@ class TestSimpleRouter(TestCase):
routes = self.router.get_routes(BasicViewSet)
decorator_routes = routes[2:]
# Make sure all these endpoints exist and none have been clobbered
- for i, endpoint in enumerate(['action1', 'action2', 'link1', 'link2']):
+ for i, endpoint in enumerate(['action1', 'action2', 'action3', 'link1', 'link2']):
route = decorator_routes[i]
# check url listing
self.assertEqual(route.url,
'^{{prefix}}/{{lookup}}/{0}/$'.format(endpoint))
# check method to function mapping
- if endpoint.startswith('action'):
- method_map = 'post'
+ if endpoint == 'action3':
+ methods_map = ['post', 'delete']
+ elif endpoint.startswith('action'):
+ methods_map = ['post']
else:
- method_map = 'get'
- self.assertEqual(route.mapping[method_map], endpoint)
-
-
+ methods_map = ['get']
+ for method in methods_map:
+ self.assertEqual(route.mapping[method], endpoint)