2013-04-25 15:47:34 +04:00
|
|
|
"""
|
2013-05-28 18:09:23 +04:00
|
|
|
The most important decorator in this module is `@api_view`, which is used
|
2013-04-25 15:47:34 +04:00
|
|
|
for writing function-based views with REST framework.
|
|
|
|
|
|
|
|
There are also various decorators for setting the API policies on function
|
2013-07-16 02:35:13 +04:00
|
|
|
based views, as well as the `@detail_route` and `@list_route` decorators, which are
|
2013-04-25 15:47:34 +04:00
|
|
|
used to annotate methods on viewsets that should be included by routers.
|
|
|
|
"""
|
2015-06-18 16:38:29 +03:00
|
|
|
import types
|
2017-09-20 12:29:47 +03:00
|
|
|
import warnings
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
from django.forms.utils import pretty_name
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2019-03-03 11:20:45 +03:00
|
|
|
from rest_framework import RemovedInDRF310Warning
|
2012-09-26 16:52:29 +04:00
|
|
|
from rest_framework.views import APIView
|
2012-09-14 19:07:07 +04:00
|
|
|
|
|
|
|
|
2018-10-18 12:38:46 +03:00
|
|
|
def api_view(http_method_names=None):
|
2012-09-14 19:07:07 +04:00
|
|
|
"""
|
2012-09-26 16:52:29 +04:00
|
|
|
Decorator that converts a function-based view into an APIView subclass.
|
|
|
|
Takes a list of allowed methods for the view as an argument.
|
2012-09-14 19:07:07 +04:00
|
|
|
"""
|
2015-02-04 17:26:23 +03:00
|
|
|
http_method_names = ['GET'] if (http_method_names is None) else http_method_names
|
2012-09-14 19:07:07 +04:00
|
|
|
|
2012-09-26 16:52:29 +04:00
|
|
|
def decorator(func):
|
|
|
|
|
2012-10-29 18:41:33 +04:00
|
|
|
WrappedAPIView = type(
|
2019-04-30 18:53:44 +03:00
|
|
|
'WrappedAPIView',
|
2012-10-29 18:41:33 +04:00
|
|
|
(APIView,),
|
|
|
|
{'__doc__': func.__doc__}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Note, the above allows us to set the docstring.
|
2012-11-14 22:36:29 +04:00
|
|
|
# It is the equivalent of:
|
2012-10-29 18:41:33 +04:00
|
|
|
#
|
|
|
|
# class WrappedAPIView(APIView):
|
|
|
|
# pass
|
|
|
|
# WrappedAPIView.__doc__ = func.doc <--- Not possible to do this
|
2012-09-26 16:52:29 +04:00
|
|
|
|
2013-01-19 19:51:14 +04:00
|
|
|
# api_view applied without (method_names)
|
|
|
|
assert not(isinstance(http_method_names, types.FunctionType)), \
|
|
|
|
'@api_view missing list of allowed HTTP methods'
|
|
|
|
|
|
|
|
# api_view applied with eg. string instead of list of strings
|
|
|
|
assert isinstance(http_method_names, (list, tuple)), \
|
2013-05-28 18:09:23 +04:00
|
|
|
'@api_view expected a list of strings, received %s' % type(http_method_names).__name__
|
2013-01-19 19:51:14 +04:00
|
|
|
|
2017-11-06 12:03:01 +03:00
|
|
|
allowed_methods = set(http_method_names) | {'options'}
|
2012-10-09 15:01:17 +04:00
|
|
|
WrappedAPIView.http_method_names = [method.lower() for method in allowed_methods]
|
2012-09-26 16:52:29 +04:00
|
|
|
|
|
|
|
def handler(self, *args, **kwargs):
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
for method in http_method_names:
|
|
|
|
setattr(WrappedAPIView, method.lower(), handler)
|
|
|
|
|
2012-10-09 12:57:08 +04:00
|
|
|
WrappedAPIView.__name__ = func.__name__
|
2016-09-05 13:16:41 +03:00
|
|
|
WrappedAPIView.__module__ = func.__module__
|
2012-10-09 12:57:08 +04:00
|
|
|
|
2012-09-26 16:52:29 +04:00
|
|
|
WrappedAPIView.renderer_classes = getattr(func, 'renderer_classes',
|
|
|
|
APIView.renderer_classes)
|
|
|
|
|
|
|
|
WrappedAPIView.parser_classes = getattr(func, 'parser_classes',
|
|
|
|
APIView.parser_classes)
|
|
|
|
|
|
|
|
WrappedAPIView.authentication_classes = getattr(func, 'authentication_classes',
|
|
|
|
APIView.authentication_classes)
|
|
|
|
|
|
|
|
WrappedAPIView.throttle_classes = getattr(func, 'throttle_classes',
|
|
|
|
APIView.throttle_classes)
|
|
|
|
|
|
|
|
WrappedAPIView.permission_classes = getattr(func, 'permission_classes',
|
|
|
|
APIView.permission_classes)
|
|
|
|
|
2017-09-14 11:46:34 +03:00
|
|
|
WrappedAPIView.schema = getattr(func, 'schema',
|
|
|
|
APIView.schema)
|
|
|
|
|
2012-09-26 16:52:29 +04:00
|
|
|
return WrappedAPIView.as_view()
|
2018-10-18 12:38:46 +03:00
|
|
|
|
2012-09-03 18:57:43 +04:00
|
|
|
return decorator
|
2012-09-14 19:07:07 +04:00
|
|
|
|
|
|
|
|
2012-09-26 16:52:29 +04:00
|
|
|
def renderer_classes(renderer_classes):
|
|
|
|
def decorator(func):
|
2012-09-26 23:18:57 +04:00
|
|
|
func.renderer_classes = renderer_classes
|
2012-09-26 16:52:29 +04:00
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def parser_classes(parser_classes):
|
|
|
|
def decorator(func):
|
2012-09-26 23:18:57 +04:00
|
|
|
func.parser_classes = parser_classes
|
2012-09-26 16:52:29 +04:00
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def authentication_classes(authentication_classes):
|
|
|
|
def decorator(func):
|
2012-09-26 23:18:57 +04:00
|
|
|
func.authentication_classes = authentication_classes
|
2012-09-26 16:52:29 +04:00
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def throttle_classes(throttle_classes):
|
|
|
|
def decorator(func):
|
2012-09-26 23:18:57 +04:00
|
|
|
func.throttle_classes = throttle_classes
|
2012-09-26 16:52:29 +04:00
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def permission_classes(permission_classes):
|
|
|
|
def decorator(func):
|
2012-09-26 23:18:57 +04:00
|
|
|
func.permission_classes = permission_classes
|
2012-09-26 16:52:29 +04:00
|
|
|
return func
|
|
|
|
return decorator
|
2013-04-04 23:35:40 +04:00
|
|
|
|
|
|
|
|
2017-09-14 11:46:34 +03:00
|
|
|
def schema(view_inspector):
|
|
|
|
def decorator(func):
|
|
|
|
func.schema = view_inspector
|
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2018-10-02 17:22:21 +03:00
|
|
|
def action(methods=None, detail=None, url_path=None, url_name=None, **kwargs):
|
2013-04-04 23:35:40 +04:00
|
|
|
"""
|
2018-01-25 11:40:49 +03:00
|
|
|
Mark a ViewSet method as a routable action.
|
|
|
|
|
|
|
|
Set the `detail` boolean to determine if this action should apply to
|
|
|
|
instance/detail requests or collection/list requests.
|
2013-04-04 23:35:40 +04:00
|
|
|
"""
|
2015-02-04 17:26:23 +03:00
|
|
|
methods = ['get'] if (methods is None) else methods
|
2018-01-25 11:40:49 +03:00
|
|
|
methods = [method.lower() for method in methods]
|
|
|
|
|
|
|
|
assert detail is not None, (
|
|
|
|
"@action() missing required argument: 'detail'"
|
|
|
|
)
|
2015-02-04 17:13:30 +03:00
|
|
|
|
2018-10-02 17:22:21 +03:00
|
|
|
# name and suffix are mutually exclusive
|
|
|
|
if 'name' in kwargs and 'suffix' in kwargs:
|
|
|
|
raise TypeError("`name` and `suffix` are mutually exclusive arguments.")
|
|
|
|
|
2013-04-04 23:35:40 +04:00
|
|
|
def decorator(func):
|
2018-07-06 11:33:10 +03:00
|
|
|
func.mapping = MethodMapper(func, methods)
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
func.detail = detail
|
2018-04-04 21:50:42 +03:00
|
|
|
func.url_path = url_path if url_path else func.__name__
|
|
|
|
func.url_name = url_name if url_name else func.__name__.replace('_', '-')
|
2013-04-04 23:35:40 +04:00
|
|
|
func.kwargs = kwargs
|
2018-10-02 17:22:21 +03:00
|
|
|
|
|
|
|
# Set descriptive arguments for viewsets
|
|
|
|
if 'name' not in kwargs and 'suffix' not in kwargs:
|
|
|
|
func.kwargs['name'] = pretty_name(func.__name__)
|
|
|
|
func.kwargs['description'] = func.__doc__ or None
|
2018-07-06 11:33:10 +03:00
|
|
|
|
2013-04-04 23:35:40 +04:00
|
|
|
return func
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
class MethodMapper(dict):
|
|
|
|
"""
|
|
|
|
Enables mapping HTTP methods to different ViewSet methods for a single,
|
|
|
|
logical action.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
class MyViewSet(ViewSet):
|
|
|
|
|
|
|
|
@action(detail=False)
|
|
|
|
def example(self, request, **kwargs):
|
|
|
|
...
|
|
|
|
|
|
|
|
@example.mapping.post
|
|
|
|
def create_example(self, request, **kwargs):
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, action, methods):
|
|
|
|
self.action = action
|
|
|
|
for method in methods:
|
|
|
|
self[method] = self.action.__name__
|
|
|
|
|
|
|
|
def _map(self, method, func):
|
|
|
|
assert method not in self, (
|
|
|
|
"Method '%s' has already been mapped to '.%s'." % (method, self[method]))
|
|
|
|
assert func.__name__ != self.action.__name__, (
|
|
|
|
"Method mapping does not behave like the property decorator. You "
|
|
|
|
"cannot use the same method name for each mapping declaration.")
|
|
|
|
|
|
|
|
self[method] = func.__name__
|
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
def get(self, func):
|
|
|
|
return self._map('get', func)
|
|
|
|
|
|
|
|
def post(self, func):
|
|
|
|
return self._map('post', func)
|
|
|
|
|
|
|
|
def put(self, func):
|
|
|
|
return self._map('put', func)
|
|
|
|
|
|
|
|
def patch(self, func):
|
|
|
|
return self._map('patch', func)
|
|
|
|
|
|
|
|
def delete(self, func):
|
|
|
|
return self._map('delete', func)
|
|
|
|
|
|
|
|
def head(self, func):
|
|
|
|
return self._map('head', func)
|
|
|
|
|
|
|
|
def options(self, func):
|
|
|
|
return self._map('options', func)
|
|
|
|
|
|
|
|
def trace(self, func):
|
|
|
|
return self._map('trace', func)
|
|
|
|
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
def detail_route(methods=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Used to mark a method on a ViewSet that should be routed for detail requests.
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
2018-10-18 12:38:46 +03:00
|
|
|
"`detail_route` is deprecated and will be removed in 3.10 in favor of "
|
2018-01-25 11:40:49 +03:00
|
|
|
"`action`, which accepts a `detail` bool. Use `@action(detail=True)` instead.",
|
2019-03-03 11:20:45 +03:00
|
|
|
RemovedInDRF310Warning, stacklevel=2
|
2018-01-25 11:40:49 +03:00
|
|
|
)
|
2018-04-04 21:50:42 +03:00
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
func = action(methods, detail=True, **kwargs)(func)
|
|
|
|
if 'url_name' not in kwargs:
|
|
|
|
func.url_name = func.url_path.replace('_', '-')
|
|
|
|
return func
|
|
|
|
return decorator
|
2018-01-25 11:40:49 +03:00
|
|
|
|
|
|
|
|
2015-02-04 17:03:03 +03:00
|
|
|
def list_route(methods=None, **kwargs):
|
2013-04-04 23:35:40 +04:00
|
|
|
"""
|
2013-08-20 00:02:22 +04:00
|
|
|
Used to mark a method on a ViewSet that should be routed for list requests.
|
2013-04-04 23:35:40 +04:00
|
|
|
"""
|
2018-01-25 11:40:49 +03:00
|
|
|
warnings.warn(
|
2018-10-18 12:38:46 +03:00
|
|
|
"`list_route` is deprecated and will be removed in 3.10 in favor of "
|
2018-01-25 11:40:49 +03:00
|
|
|
"`action`, which accepts a `detail` bool. Use `@action(detail=False)` instead.",
|
2019-03-03 11:20:45 +03:00
|
|
|
RemovedInDRF310Warning, stacklevel=2
|
2018-01-25 11:40:49 +03:00
|
|
|
)
|
2018-04-04 21:50:42 +03:00
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
func = action(methods, detail=False, **kwargs)(func)
|
|
|
|
if 'url_name' not in kwargs:
|
|
|
|
func.url_name = func.url_path.replace('_', '-')
|
|
|
|
return func
|
|
|
|
return decorator
|