mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 11:30:12 +03:00
Merge ea9e78caf5
into 43c4e3aab8
This commit is contained in:
commit
6b3e21fa92
|
@ -142,9 +142,16 @@ The `@action` decorator will route `POST` requests by default, but may also acce
|
|||
@action(methods=['POST', 'DELETE'])
|
||||
def unset_password(self, request, pk=None):
|
||||
...
|
||||
|
||||
|
||||
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):
|
||||
...
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue
Block a user