diff 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
line wrap: on
line diff
--- a/gpp/antispam/decorators.py	Thu Aug 25 02:23:55 2011 +0000
+++ b/gpp/antispam/decorators.py	Sat Aug 27 04:23:30 2011 +0000
@@ -7,7 +7,7 @@
 
 from django.shortcuts import render
 
-from antispam.rate_limit import rate_check
+from antispam.rate_limit import RateLimiter, RateLimiterUnavailable
 
 
 def rate_limit(count=10, interval=timedelta(minutes=1),
@@ -18,15 +18,23 @@
         @wraps(fn)
         def wrapped(request, *args, **kwargs):
 
+            ip = request.META.get('REMOTE_ADDR')
+            try:
+                rate_limiter = RateLimiter(ip, count, interval, lockout)
+            except RateLimiterUnavailable:
+                # just call the function and return the result
+                return fn(request, *args, **kwargs)
+
+            if rate_limiter.is_blocked():
+                return render(request, 'antispam/blocked.html', status=403)
+
             response = fn(request, *args, **kwargs)
 
             if request.method == 'POST':
                 success = (response and response.has_header('location') and
                         response.status_code == 302)
-                if not success:
-                    ip = request.META.get('REMOTE_ADDR')
-                    if rate_check(ip, count, interval, lockout):
-                        return render(request, 'antispam/blocked.html', status=403)
+                if not success and rate_limiter.incr():
+                    return render(request, 'antispam/blocked.html', status=403)
 
             return response