Added has_lookup_field argument to the link and action decorators.

This commit is contained in:
Warnar Boekkooi 2013-11-21 17:47:40 +08:00
parent 3765865b4b
commit ea9e78caf5
3 changed files with 32 additions and 10 deletions

View File

@ -145,6 +145,13 @@ The `@action` decorator will route `POST` requests by default, but may also acce
The two new actions will then be available at the urls `^users/{pk}/set_password/$` and `^users/{pk}/unset_password/$`
The `@action` and `@link` decorators will add `{pk}` to the route by default, by using the `has_lookup_field` argument this behavior can be disabled.
*The `has_lookup_field` argument has no effect when using `ModelViewSet` or `ReadOnlyModelViewSet`*
@action(has_lookup_field=False)
def others(self, request):
...
---

View File

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

View File

@ -110,6 +110,14 @@ class SimpleRouter(BaseRouter):
name='{basename}-{methodnamehyphen}',
initkwargs={}
),
Route(
url=r'^{prefix}/{methodname}{trailing_slash}$',
mapping={
'{httpmethod}': '{methodname}',
},
name='{basename}-{methodnamehyphen}',
initkwargs={}
),
]
def __init__(self, trailing_slash=True):
@ -146,18 +154,23 @@ class SimpleRouter(BaseRouter):
for methodname in dir(viewset):
attr = getattr(viewset, methodname)
httpmethods = getattr(attr, 'bind_to_methods', None)
if httpmethods:
if methodname in known_actions:
raise ImproperlyConfigured('Cannot use @action or @link decorator on '
'method "%s" as it is an existing route' % methodname)
httpmethods = [method.lower() for method in httpmethods]
dynamic_routes.append((httpmethods, methodname))
has_lookup_field = getattr(attr, 'has_lookup_field', True)
if not httpmethods:
continue
if methodname in known_actions:
raise ImproperlyConfigured('Cannot use @action or @link decorator on '
'method "%s" as it is an existing route' % methodname)
httpmethods = [method.lower() for method in httpmethods]
dynamic_routes.append((httpmethods, methodname, has_lookup_field))
ret = []
for route in self.routes:
if route.mapping == {'{httpmethod}': '{methodname}'}:
# Dynamic routes (@link or @action decorator)
for httpmethods, methodname in dynamic_routes:
is_lookup_route = '{lookup}' in route.url
for httpmethods, methodname, has_lookup_field in dynamic_routes:
if has_lookup_field != is_lookup_route:
continue
initkwargs = route.initkwargs.copy()
initkwargs.update(getattr(viewset, methodname).kwargs)
ret.append(Route(