From df957c8625c79e36c33f314c943a2c593f3a2701 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 14 Jun 2013 14:18:40 +0100 Subject: Fix and tests for ScopedRateThrottle. Closes #935 --- rest_framework/throttling.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'rest_framework/throttling.py') diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 9d89d1cb..ec9c80ff 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -40,9 +40,9 @@ class SimpleRateThrottle(BaseThrottle): """ timer = time.time - settings = api_settings cache_format = 'throtte_%(scope)s_%(ident)s' scope = None + THROTTLE_RATES = api_settings.DEFAULT_THROTTLE_RATES def __init__(self): if not getattr(self, 'rate', None): @@ -68,7 +68,7 @@ class SimpleRateThrottle(BaseThrottle): raise ImproperlyConfigured(msg) try: - return self.settings.DEFAULT_THROTTLE_RATES[self.scope] + return self.THROTTLE_RATES[self.scope] except KeyError: msg = "No default throttle rate set for '%s' scope" % self.scope raise ImproperlyConfigured(msg) @@ -187,6 +187,19 @@ class ScopedRateThrottle(SimpleRateThrottle): """ scope_attr = 'throttle_scope' + def __init__(self): + pass + + def allow_request(self, request, view): + self.scope = getattr(view, self.scope_attr, None) + + if not self.scope: + return True + + self.rate = self.get_rate() + self.num_requests, self.duration = self.parse_rate(self.rate) + return super(ScopedRateThrottle, self).allow_request(request, view) + def get_cache_key(self, request, view): """ If `view.throttle_scope` is not set, don't apply this throttle. -- cgit v1.2.3