view antispam/decorators.py @ 953:8647a669edb4

Fix excessive cache usage for forum date/times. Issue #84. Hitting the cache 30+ times while browsing the forums to adjust all the dates/times into the user's time zone. Just hit the user's profile and be done with it. It should be loaded.
author Brian Neal <bgneal@gmail.com>
date Tue, 19 May 2015 21:08:45 -0500
parents 4a49d4ac319f
children
line wrap: on
line source
"""
This module contains decorators for the antispam application.

"""
from functools import wraps
import logging


def log_auth_failures(auth_type):

    def decorator(fn):
        logger = logging.getLogger('auth')

        @wraps(fn)
        def wrapped(request, *args, **kwargs):

            response = fn(request, *args, **kwargs)

            if request.method == 'POST':

                # Figure out if the view succeeded; success means a redirect is
                # about to occur.
                success = (response and response.has_header('location') and
                        response.status_code == 302)

                if not success:
                    username = request.POST.get('username')
                    username = username if username else '(None)'
                    logger.error("%s failure from [%s] for %s",
                            auth_type,
                            request.META.get('REMOTE_ADDR', '?'),
                            username)

            return response

        return wrapped
    return decorator