diff gpp/antispam/decorators.py @ 472:7c3816d76c6c

Implement rate limiting on registration and login for #224.
author Brian Neal <bgneal@gmail.com>
date Thu, 25 Aug 2011 02:23:55 +0000
parents
children 5e826e232932
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/antispam/decorators.py	Thu Aug 25 02:23:55 2011 +0000
@@ -0,0 +1,34 @@
+"""
+This module contains decorators for the antispam application.
+
+"""
+from datetime import timedelta
+from functools import wraps
+
+from django.shortcuts import render
+
+from antispam.rate_limit import rate_check
+
+
+def rate_limit(count=10, interval=timedelta(minutes=1),
+        lockout=timedelta(hours=8)):
+
+    def decorator(fn):
+
+        @wraps(fn)
+        def wrapped(request, *args, **kwargs):
+
+            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)
+
+            return response
+
+        return wrapped
+    return decorator