comparison gpp/antispam/decorators.py @ 479:32cec6cd8808

Refactor RateLimiter so that if Redis is not running, everything still runs normally (minus the rate limiting protection). My assumption that creating a Redis connection would throw an exception if Redis wasn't running was wrong. The exceptions actually occur when you issue a command. This is for #224.
author Brian Neal <bgneal@gmail.com>
date Sun, 25 Sep 2011 00:49:05 +0000
parents 5e826e232932
children a5d11471d031
comparison
equal deleted inserted replaced
478:d280b27fed17 479:32cec6cd8808
19 def wrapped(request, *args, **kwargs): 19 def wrapped(request, *args, **kwargs):
20 20
21 ip = request.META.get('REMOTE_ADDR') 21 ip = request.META.get('REMOTE_ADDR')
22 try: 22 try:
23 rate_limiter = RateLimiter(ip, count, interval, lockout) 23 rate_limiter = RateLimiter(ip, count, interval, lockout)
24 if rate_limiter.is_blocked():
25 return render(request, 'antispam/blocked.html', status=403)
26
24 except RateLimiterUnavailable: 27 except RateLimiterUnavailable:
25 # just call the function and return the result 28 # just call the function and return the result
26 return fn(request, *args, **kwargs) 29 return fn(request, *args, **kwargs)
27
28 if rate_limiter.is_blocked():
29 return render(request, 'antispam/blocked.html', status=403)
30 30
31 response = fn(request, *args, **kwargs) 31 response = fn(request, *args, **kwargs)
32 32
33 if request.method == 'POST': 33 if request.method == 'POST':
34 success = (response and response.has_header('location') and 34 success = (response and response.has_header('location') and
35 response.status_code == 302) 35 response.status_code == 302)
36 if not success and rate_limiter.incr(): 36 try:
37 return render(request, 'antispam/blocked.html', status=403) 37 if not success and rate_limiter.incr():
38 return render(request, 'antispam/blocked.html', status=403)
39
40 except RateLimiterUnavailable:
41 pass
38 42
39 return response 43 return response
40 44
41 return wrapped 45 return wrapped
42 return decorator 46 return decorator