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