Skip to content

Commit 5229848

Browse files
committed
Clean up
1 parent df957c8 commit 5229848

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

rest_framework/throttling.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,24 @@ class ScopedRateThrottle(SimpleRateThrottle):
188188
scope_attr = 'throttle_scope'
189189

190190
def __init__(self):
191+
# Override the usual SimpleRateThrottle, because we can't determine
192+
# the rate until called by the view.
191193
pass
192194

193195
def allow_request(self, request, view):
196+
# We can only determine the scope once we're called by the view.
194197
self.scope = getattr(view, self.scope_attr, None)
195198

199+
# If a view does not have a `throttle_scope` always allow the request
196200
if not self.scope:
197201
return True
198202

203+
# Determine the allowed request rate as we normally would during
204+
# the `__init__` call.
199205
self.rate = self.get_rate()
200206
self.num_requests, self.duration = self.parse_rate(self.rate)
207+
208+
# We can now proceed as normal.
201209
return super(ScopedRateThrottle, self).allow_request(request, view)
202210

203211
def get_cache_key(self, request, view):
@@ -207,18 +215,12 @@ def get_cache_key(self, request, view):
207215
Otherwise generate the unique cache key by concatenating the user id
208216
with the '.throttle_scope` property of the view.
209217
"""
210-
scope = getattr(view, self.scope_attr, None)
211-
212-
if not scope:
213-
# Only throttle views if `.throttle_scope` is set on the view.
214-
return None
215-
216218
if request.user.is_authenticated():
217219
ident = request.user.id
218220
else:
219221
ident = request.META.get('REMOTE_ADDR', None)
220222

221223
return self.cache_format % {
222-
'scope': scope,
224+
'scope': self.scope,
223225
'ident': ident
224226
}

0 commit comments

Comments
 (0)