bgneal@472: """ bgneal@472: This module contains decorators for the antispam application. bgneal@472: bgneal@472: """ bgneal@679: import json bgneal@472: from functools import wraps bgneal@690: import logging bgneal@472: bgneal@472: bgneal@690: def log_auth_failures(auth_type): bgneal@472: bgneal@472: def decorator(fn): bgneal@690: logger = logging.getLogger('auth') 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@505: bgneal@505: # Figure out if the view succeeded; if it is a non-ajax view, bgneal@505: # then success means a redirect is about to occur. If it is bgneal@505: # an ajax view, we have to decode the json response. bgneal@505: success = False bgneal@505: if not request.is_ajax(): bgneal@505: success = (response and response.has_header('location') and bgneal@505: response.status_code == 302) bgneal@505: elif response: bgneal@679: json_resp = json.loads(response.content) bgneal@505: success = json_resp['success'] bgneal@505: bgneal@505: if not success: bgneal@690: username = request.POST.get('username') bgneal@690: username = username if username else '(None)' bgneal@690: logger.error("%s failure from [%s] for %s", bgneal@690: auth_type, bgneal@690: request.META.get('REMOTE_ADDR', '?'), bgneal@690: username) bgneal@479: bgneal@472: return response bgneal@472: bgneal@472: return wrapped bgneal@472: return decorator