aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/throttling.py
diff options
context:
space:
mode:
authorTom Christie2013-06-14 14:18:40 +0100
committerTom Christie2013-06-14 14:18:40 +0100
commitdf957c8625c79e36c33f314c943a2c593f3a2701 (patch)
treef4bbf53718323ba92a0cb9afdb833d788afcccf6 /rest_framework/throttling.py
parent6cc4fe56373ba844a02173ea2efdf288d8fbac03 (diff)
downloaddjango-rest-framework-df957c8625c79e36c33f314c943a2c593f3a2701.tar.bz2
Fix and tests for ScopedRateThrottle. Closes #935
Diffstat (limited to 'rest_framework/throttling.py')
-rw-r--r--rest_framework/throttling.py17
1 files changed, 15 insertions, 2 deletions
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.