def allow_request(self, request, view):
self.key = self.get_cache_key(request, view)
if self.key is None:
return True
self.history = self.cache.get(self.key, [])
self.now = self.timer()
while self.history and self.history[-1][0] <= self.now - float(self.post_rate_time):
if self.history[-1][1] == 'post':
self.history.pop()
if len(self.history) >= self.post_rate_num:
return self.throttle_failure()
if self.history and self.history[0][1] == 'duplicate':
time_remaining = self.history[0][0] - (self.now - float(self.duplicate_lockout))
if time_remaining > 0:
raise Throttled(detail="You posted a duplicate comment recently.", wait=time_remaining)
else:
self.history.pop(0)
if self.is_recent_duplicate(request):
self.history.insert(0, [self.now, 'duplicate'])
self.cache.set(self.key, self.history, float(self.duplicate_lockout))
raise Throttled(detail=("that's a duplicate"), wait=(self.duplicate_lockout))
return self.throttle_success()
评论列表
文章目录