bgneal@472: """ bgneal@472: This module contains decorators for the antispam application. bgneal@472: bgneal@472: """ bgneal@472: from datetime import timedelta bgneal@472: from functools import wraps bgneal@472: bgneal@472: from django.shortcuts import render bgneal@472: bgneal@472: from antispam.rate_limit import rate_check bgneal@472: bgneal@472: bgneal@472: def rate_limit(count=10, interval=timedelta(minutes=1), bgneal@472: lockout=timedelta(hours=8)): bgneal@472: bgneal@472: def decorator(fn): bgneal@472: bgneal@472: @wraps(fn) bgneal@472: def wrapped(request, *args, **kwargs): bgneal@472: bgneal@472: response = fn(request, *args, **kwargs) bgneal@472: bgneal@472: if request.method == 'POST': bgneal@472: success = (response and response.has_header('location') and bgneal@472: response.status_code == 302) bgneal@472: if not success: bgneal@472: ip = request.META.get('REMOTE_ADDR') bgneal@472: if rate_check(ip, count, interval, lockout): bgneal@472: return render(request, 'antispam/blocked.html', status=403) bgneal@472: bgneal@472: return response bgneal@472: bgneal@472: return wrapped bgneal@472: return decorator