Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/api-guide/views.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ The available decorators are:
* `@parser_classes(...)`
* `@authentication_classes(...)`
* `@throttle_classes(...)`
* `@throttle_scope(...)`
* `@permission_classes(...)`
* `@content_negotiation_class(...)`
* `@metadata_class(...)`
Expand Down
11 changes: 11 additions & 0 deletions rest_framework/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def handler(self, *args, **kwargs):
WrappedAPIView.throttle_classes = getattr(func, 'throttle_classes',
APIView.throttle_classes)

WrappedAPIView.throttle_scope = getattr(func, 'throttle_scope',
None)

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

Expand Down Expand Up @@ -136,6 +139,14 @@ def decorator(func):
return decorator


def throttle_scope(throttle_scope):
def decorator(func):
_check_decorator_order(func, 'throttle_scope')
func.throttle_scope = throttle_scope
return func
return decorator


def permission_classes(permission_classes):
def decorator(func):
_check_decorator_order(func, 'permission_classes')
Expand Down
29 changes: 27 additions & 2 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rest_framework.decorators import (
action, api_view, authentication_classes, content_negotiation_class,
metadata_class, parser_classes, permission_classes, renderer_classes,
schema, throttle_classes, versioning_class
schema, throttle_classes, throttle_scope, versioning_class
)
from rest_framework.negotiation import BaseContentNegotiation
from rest_framework.parsers import JSONParser
Expand All @@ -17,7 +17,7 @@
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.throttling import ScopedRateThrottle, UserRateThrottle
from rest_framework.versioning import QueryParameterVersioning
from rest_framework.views import APIView

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

def test_throttle_scope(self):
scope = "x"

class OncePerDayScopedThrottle(ScopedRateThrottle):
THROTTLE_RATES = {scope: "1/day"}

@api_view(['GET'])
@throttle_classes([OncePerDayScopedThrottle])
@throttle_scope(scope)
Comment on lines +163 to +164
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is @throttle_classes required every time @throttle_scope is used? I don't think so if the scopes are defined in the project settings... Can we add a test to cover that please?

Also make sure we're aligned with CBV in terms of misconfigurations (e.g specifying an unknown scope)

def view_1(request):
return Response({})

@api_view(['GET'])
@throttle_classes([OncePerDayScopedThrottle])
@throttle_scope(scope)
def view_2(request):
return Response({})

request = self.factory.get('/')
response = view_1(request)
assert response.status_code == status.HTTP_200_OK

response = view_2(request)
assert response.status_code == status.HTTP_429_TOO_MANY_REQUESTS

def test_versioning_class(self):
@api_view(["GET"])
@versioning_class(QueryParameterVersioning)
Expand Down
Loading