throttles.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:django_rest_demo 作者: SeanAppleby 项目源码 文件源码
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()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号