comparison gpp/antispam/decorators.py @ 473:5e826e232932

Fixing #224; make sure we block IP's that have tripped the rate limiter or have been manually blocked.
author Brian Neal <bgneal@gmail.com>
date Sat, 27 Aug 2011 04:23:30 +0000
parents 7c3816d76c6c
children 32cec6cd8808
comparison
equal deleted inserted replaced
472:7c3816d76c6c 473:5e826e232932
5 from datetime import timedelta 5 from datetime import timedelta
6 from functools import wraps 6 from functools import wraps
7 7
8 from django.shortcuts import render 8 from django.shortcuts import render
9 9
10 from antispam.rate_limit import rate_check 10 from antispam.rate_limit import RateLimiter, RateLimiterUnavailable
11 11
12 12
13 def rate_limit(count=10, interval=timedelta(minutes=1), 13 def rate_limit(count=10, interval=timedelta(minutes=1),
14 lockout=timedelta(hours=8)): 14 lockout=timedelta(hours=8)):
15 15
16 def decorator(fn): 16 def decorator(fn):
17 17
18 @wraps(fn) 18 @wraps(fn)
19 def wrapped(request, *args, **kwargs): 19 def wrapped(request, *args, **kwargs):
20 20
21 ip = request.META.get('REMOTE_ADDR')
22 try:
23 rate_limiter = RateLimiter(ip, count, interval, lockout)
24 except RateLimiterUnavailable:
25 # just call the function and return the result
26 return fn(request, *args, **kwargs)
27
28 if rate_limiter.is_blocked():
29 return render(request, 'antispam/blocked.html', status=403)
30
21 response = fn(request, *args, **kwargs) 31 response = fn(request, *args, **kwargs)
22 32
23 if request.method == 'POST': 33 if request.method == 'POST':
24 success = (response and response.has_header('location') and 34 success = (response and response.has_header('location') and
25 response.status_code == 302) 35 response.status_code == 302)
26 if not success: 36 if not success and rate_limiter.incr():
27 ip = request.META.get('REMOTE_ADDR') 37 return render(request, 'antispam/blocked.html', status=403)
28 if rate_check(ip, count, interval, lockout):
29 return render(request, 'antispam/blocked.html', status=403)
30 38
31 return response 39 return response
32 40
33 return wrapped 41 return wrapped
34 return decorator 42 return decorator