annotate gpp/antispam/decorators.py @ 477:737a0c261a77

Add a link to John Blair's 50 years of surf music article on the banner.
author Brian Neal <bgneal@gmail.com>
date Fri, 09 Sep 2011 22:16:40 +0000
parents 5e826e232932
children 32cec6cd8808
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@473 10 from antispam.rate_limit import RateLimiter, RateLimiterUnavailable
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@473 21 ip = request.META.get('REMOTE_ADDR')
bgneal@473 22 try:
bgneal@473 23 rate_limiter = RateLimiter(ip, count, interval, lockout)
bgneal@473 24 except RateLimiterUnavailable:
bgneal@473 25 # just call the function and return the result
bgneal@473 26 return fn(request, *args, **kwargs)
bgneal@473 27
bgneal@473 28 if rate_limiter.is_blocked():
bgneal@473 29 return render(request, 'antispam/blocked.html', status=403)
bgneal@473 30
bgneal@472 31 response = fn(request, *args, **kwargs)
bgneal@472 32
bgneal@472 33 if request.method == 'POST':
bgneal@472 34 success = (response and response.has_header('location') and
bgneal@472 35 response.status_code == 302)
bgneal@473 36 if not success and rate_limiter.incr():
bgneal@473 37 return render(request, 'antispam/blocked.html', status=403)
bgneal@472 38
bgneal@472 39 return response
bgneal@472 40
bgneal@472 41 return wrapped
bgneal@472 42 return decorator