mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +03:00
76 lines
2.2 KiB
Python
76 lines
2.2 KiB
Python
from rest_framework.views import APIView
|
|
|
|
|
|
def api_view(http_method_names):
|
|
|
|
"""
|
|
Decorator that converts a function-based view into an APIView subclass.
|
|
Takes a list of allowed methods for the view as an argument.
|
|
"""
|
|
|
|
def decorator(func):
|
|
|
|
class WrappedAPIView(APIView):
|
|
pass
|
|
|
|
WrappedAPIView.http_method_names = [method.lower() for method in http_method_names]
|
|
|
|
def handler(self, *args, **kwargs):
|
|
return func(*args, **kwargs)
|
|
|
|
for method in http_method_names:
|
|
setattr(WrappedAPIView, method.lower(), handler)
|
|
|
|
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)
|
|
|
|
return WrappedAPIView.as_view()
|
|
return decorator
|
|
|
|
|
|
def renderer_classes(renderer_classes):
|
|
def decorator(func):
|
|
func.renderer_classes = renderer_classes
|
|
return func
|
|
return decorator
|
|
|
|
|
|
def parser_classes(parser_classes):
|
|
def decorator(func):
|
|
func.parser_classes = parser_classes
|
|
return func
|
|
return decorator
|
|
|
|
|
|
def authentication_classes(authentication_classes):
|
|
def decorator(func):
|
|
func.authentication_classes = authentication_classes
|
|
return func
|
|
return decorator
|
|
|
|
|
|
def throttle_classes(throttle_classes):
|
|
def decorator(func):
|
|
func.throttle_classes = throttle_classes
|
|
return func
|
|
return decorator
|
|
|
|
|
|
def permission_classes(permission_classes):
|
|
def decorator(func):
|
|
func.permission_classes = permission_classes
|
|
return func
|
|
return decorator
|