Mercurial > public > sg101
view gpp/antispam/decorators.py @ 478:d280b27fed17
Updating to MarkitUp 1.1.12. Merging vendor branch changes into trunk.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 11 Sep 2011 19:50:59 +0000 |
parents | 5e826e232932 |
children | 32cec6cd8808 |
line wrap: on
line source
""" 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 RateLimiter, RateLimiterUnavailable def rate_limit(count=10, interval=timedelta(minutes=1), lockout=timedelta(hours=8)): def decorator(fn): @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 and rate_limiter.incr(): return render(request, 'antispam/blocked.html', status=403) return response return wrapped return decorator