Skip to content

Add missing decorators: @versioning_class(), @content_negotiation_class(), @metadata_class() #9719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions rest_framework/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def handler(self, *args, **kwargs):
WrappedAPIView.permission_classes = getattr(func, 'permission_classes',
APIView.permission_classes)

WrappedAPIView.content_negotiation_class = getattr(func, 'content_negotiation_class',
APIView.content_negotiation_class)

WrappedAPIView.metadata_class = getattr(func, 'metadata_class',
APIView.metadata_class)

WrappedAPIView.versioning_class = getattr(func, "versioning_class",
APIView.versioning_class)

WrappedAPIView.schema = getattr(func, 'schema',
APIView.schema)

Expand Down Expand Up @@ -113,6 +122,27 @@ def decorator(func):
return decorator


def content_negotiation_class(content_negotiation_class):
def decorator(func):
func.content_negotiation_class = content_negotiation_class
return func
return decorator


def metadata_class(metadata_class):
def decorator(func):
func.metadata_class = metadata_class
return func
return decorator


def versioning_class(versioning_class):
def decorator(func):
func.versioning_class = versioning_class
return func
return decorator


def schema(view_inspector):
def decorator(func):
func.schema = view_inspector
Expand Down
44 changes: 42 additions & 2 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
from rest_framework import status
from rest_framework.authentication import BasicAuthentication
from rest_framework.decorators import (
action, api_view, authentication_classes, parser_classes,
permission_classes, renderer_classes, schema, throttle_classes
action, api_view, authentication_classes, content_negotiation_class,
metadata_class, parser_classes, permission_classes, renderer_classes,
schema, throttle_classes, versioning_class
)
from rest_framework.negotiation import BaseContentNegotiation
from rest_framework.parsers import JSONParser
from rest_framework.permissions import IsAuthenticated
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.schemas import AutoSchema
from rest_framework.test import APIRequestFactory
from rest_framework.throttling import UserRateThrottle
from rest_framework.versioning import QueryParameterVersioning
from rest_framework.views import APIView


Expand Down Expand Up @@ -150,6 +153,43 @@ def view(request):
response = view(request)
assert response.status_code == status.HTTP_429_TOO_MANY_REQUESTS

def test_versioning_class(self):
@api_view(["GET"])
@versioning_class(QueryParameterVersioning)
def view(request):
return Response({"version": request.version})

request = self.factory.get("/?version=1.2.3")
response = view(request)
assert response.data == {"version": "1.2.3"}

def test_metadata_class(self):
# From TestMetadata.test_none_metadata()
@api_view()
@metadata_class(None)
def view(request):
return Response({})

request = self.factory.options('/')
response = view(request)
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
assert response.data == {'detail': 'Method "OPTIONS" not allowed.'}

def test_content_negotiation(self):
class CustomContentNegotiation(BaseContentNegotiation):
def select_renderer(self, request, renderers, format_suffix):
assert request.META['HTTP_ACCEPT'] == 'custom/type'
return (renderers[0], renderers[0].media_type)

@api_view(["GET"])
@content_negotiation_class(CustomContentNegotiation)
def view(request):
return Response({})

request = self.factory.get('/', HTTP_ACCEPT='custom/type')
response = view(request)
assert response.status_code == status.HTTP_200_OK

def test_schema(self):
"""
Checks CustomSchema class is set on view
Expand Down
Loading