def setUp(self): self.window = 30 self.limit_count = 10 self.make_key = lambda x, y: "test-cachekey-%s" % y.date() self.limiter = ratelimit.RateLimit( self.make_key, lambda x: None, self.window, self.limit_count, ) cache.clear()
from inboxen.utils import ratelimit, ip logger = logging.getLogger(__name__) def make_key(request, dt): key = "{}{}-{}".format( settings.REGISTER_LIMIT_CACHE_PREFIX, ip.strip_ip(request.META["REMOTE_ADDR"]), dt.strftime("%Y%m%d%H%M"), ) key = urllib.parse.quote(key.encode("utf-8")) return key def full_callback(request): logger.warning("Registration rate-limit reached: IP %s", request.META["REMOTE_ADDR"]) register_ratelimit = ratelimit.RateLimit( make_key, full_callback, settings.REGISTER_LIMIT_WINDOW, settings.REGISTER_LIMIT_COUNT, ) register_counter_full = register_ratelimit.counter_full register_counter_increase = register_ratelimit.counter_increase
settings.REGISTER_LIMIT_CACHE_PREFIX, ip.strip_ip(request.META["REMOTE_ADDR"]), dt.strftime("%Y%m%d%H%M"), ) key = urllib.parse.quote(key.encode("utf-8")) return key def full_register_callback(request): logger.warning("Registration rate-limit reached: IP %s", request.META["REMOTE_ADDR"]) register_ratelimit = ratelimit.RateLimit( make_register_key, full_register_callback, settings.REGISTER_LIMIT_WINDOW, settings.REGISTER_LIMIT_COUNT, ) def make_login_key(request, dt): key = "{}{}-{}".format( settings.LOGIN_LIMIT_CACHE_PREFIX, ip.strip_ip(request.META["REMOTE_ADDR"]), dt.strftime("%Y%m%d%H%M"), ) key = urllib.parse.quote(key.encode("utf-8")) return key