You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Setting the throttle scope of a function based view #1496
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:
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.
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 !!!!????
Activity
lovelydinosaur commentedon Apr 19, 2014
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.
alimp5 commentedon May 17, 2021
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
AnonRateThrottleandUserRateThrottlethrottles works fine in FBVs. what about custom scope !!!!????sdobz commentedon May 17, 2021
who dis @ing me 7 years after posting an issue
go read the source and derp the code around, you'll figure something out