Skip to content

Setting the throttle scope of a function based view #1496

Closed

Description

@sdobz

As far as I can see in the documentation there is no way of setting the throttle scope of a function based view, only class based views.
The naive method didn't work:

@api_view(('GET', ))
def view(request):
    return Response()
view.throttle_scope = 'scope'

After poking around in the code and a few failed attempts I came up with a method that works, but it is nasty:

class ManualScopeRateThrottle(ScopedRateThrottle):
    scope = None

    def allow_request(self, request, view):
        self.rate = self.get_rate()
        self.num_requests, self.duration = self.parse_rate(self.rate)

        # We can now proceed as normal.
        # Note we are calling the super of ScopedRateThrottle, not the super of this class
        # This is because we want to bypass ScopedRateThrottle.allow_request
        return super(ScopedRateThrottle, self).allow_request(request, view)


def set_throttle_scope(scope):
    # This is a nasty piece of work.
    # In effect it allows the throttle scope of a function view to be set:
    # @api_view(('GET', ))
    # @set_throttle_scope('scope')
    # def view(request):
    #     ...
    # It does this by creating a new class with scope already defined, then setting the functions throttle class to that
    def decorator(func):
        CustomScopeRateThrottle = type(
            'CustomScopeRateThrottle',
            (ManualScopeRateThrottle,),
            {'scope': scope}
        )
        func.throttle_classes = (CustomScopeRateThrottle, )
        return func
    return decorator

While this code shouldn't be included, certainly the feature is worth looking at.

Activity

  1. lovelydinosaur commented on Apr 19, 2014

    @lovelydinosaur
    Member

    Indeed. I'm okay with the fact that there are a few things that the plain FBVs don't support over the fuller featured CBVs.

  2. alimp5 commented on May 17, 2021

    @alimp5

    Greetings !
    @tomchristie
    @sdobz

    It's a shiit solution for DRF :/
    Please introduce a way for adding scopes in FBVs.
    we are in 2021...
    just builtin AnonRateThrottle and UserRateThrottle throttles works fine in FBVs. what about custom scope !!!!????

  3. sdobz commented on May 17, 2021

    @sdobz
    Author

    who dis @ing me 7 years after posting an issue

    go read the source and derp the code around, you'll figure something out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions