Mercurial > public > sg101
annotate 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 |
rev | line source |
---|---|
bgneal@472 | 1 """ |
bgneal@472 | 2 This module contains decorators for the antispam application. |
bgneal@472 | 3 |
bgneal@472 | 4 """ |
bgneal@472 | 5 from datetime import timedelta |
bgneal@472 | 6 from functools import wraps |
bgneal@472 | 7 |
bgneal@472 | 8 from django.shortcuts import render |
bgneal@472 | 9 |
bgneal@472 | 10 from antispam.rate_limit import rate_check |
bgneal@472 | 11 |
bgneal@472 | 12 |
bgneal@472 | 13 def rate_limit(count=10, interval=timedelta(minutes=1), |
bgneal@472 | 14 lockout=timedelta(hours=8)): |
bgneal@472 | 15 |
bgneal@472 | 16 def decorator(fn): |
bgneal@472 | 17 |
bgneal@472 | 18 @wraps(fn) |
bgneal@472 | 19 def wrapped(request, *args, **kwargs): |
bgneal@472 | 20 |
bgneal@472 | 21 response = fn(request, *args, **kwargs) |
bgneal@472 | 22 |
bgneal@472 | 23 if request.method == 'POST': |
bgneal@472 | 24 success = (response and response.has_header('location') and |
bgneal@472 | 25 response.status_code == 302) |
bgneal@472 | 26 if not success: |
bgneal@472 | 27 ip = request.META.get('REMOTE_ADDR') |
bgneal@472 | 28 if rate_check(ip, count, interval, lockout): |
bgneal@472 | 29 return render(request, 'antispam/blocked.html', status=403) |
bgneal@472 | 30 |
bgneal@472 | 31 return response |
bgneal@472 | 32 |
bgneal@472 | 33 return wrapped |
bgneal@472 | 34 return decorator |