diff options
Diffstat (limited to 'rest_framework/throttling.py')
| -rw-r--r-- | rest_framework/throttling.py | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 0d33f0e2..566c277d 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -8,14 +8,7 @@ class BaseThrottle(object): """ Rate throttling of requests. """ - - def __init__(self, view=None): - """ - All throttles hold a reference to the instantiating view. - """ - self.view = view - - def allow_request(self, request): + def allow_request(self, request, view): """ Return `True` if the request should be allowed, `False` otherwise. """ @@ -48,13 +41,12 @@ class SimpleRateThottle(BaseThrottle): cache_format = 'throtte_%(scope)s_%(ident)s' scope = None - def __init__(self, view): - super(SimpleRateThottle, self).__init__(view) + def __init__(self): if not getattr(self, 'rate', None): self.rate = self.get_rate() self.num_requests, self.duration = self.parse_rate(self.rate) - def get_cache_key(self, request): + def get_cache_key(self, request, view): """ Should return a unique cache-key which can be used for throttling. Must be overridden. @@ -90,7 +82,7 @@ class SimpleRateThottle(BaseThrottle): duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]] return (num_requests, duration) - def allow_request(self, request): + def allow_request(self, request, view): """ Implement the check to see if the request should be throttled. @@ -100,7 +92,7 @@ class SimpleRateThottle(BaseThrottle): if self.rate is None: return True - self.key = self.get_cache_key(request) + self.key = self.get_cache_key(request, view) self.history = cache.get(self.key, []) self.now = self.timer() @@ -149,7 +141,7 @@ class AnonRateThrottle(SimpleRateThottle): """ scope = 'anon' - def get_cache_key(self, request): + def get_cache_key(self, request, view): if request.user.is_authenticated(): return None # Only throttle unauthenticated requests. @@ -171,7 +163,7 @@ class UserRateThrottle(SimpleRateThottle): """ scope = 'user' - def get_cache_key(self, request): + def get_cache_key(self, request, view): if request.user.is_authenticated(): ident = request.user.id else: @@ -190,25 +182,20 @@ class ScopedRateThrottle(SimpleRateThottle): throttled. The unique cache key will be generated by concatenating the user id of the request, and the scope of the view being accessed. """ - scope_attr = 'throttle_scope' - def __init__(self, view): - """ - Scope is determined from the view being accessed. - """ - self.scope = getattr(self.view, self.scope_attr, None) - super(ScopedRateThrottle, self).__init__(view) - - def get_cache_key(self, request): + def get_cache_key(self, request, view): """ If `view.throttle_scope` is not set, don't apply this throttle. Otherwise generate the unique cache key by concatenating the user id with the '.throttle_scope` property of the view. """ - if not self.scope: - return None # Only throttle views if `.throttle_scope` is set. + scope = getattr(view, self.scope_attr, None) + + if not scope: + # Only throttle views if `.throttle_scope` is set on the view. + return None if request.user.is_authenticated(): ident = request.user.id @@ -216,6 +203,6 @@ class ScopedRateThrottle(SimpleRateThottle): ident = request.META.get('REMOTE_ADDR', None) return self.cache_format % { - 'scope': self.scope, + 'scope': scope, 'ident': ident } |
