annotate antispam/decorators.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
parents 4a49d4ac319f
children
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 functools import wraps
bgneal@690 6 import logging
bgneal@472 7
bgneal@472 8
bgneal@690 9 def log_auth_failures(auth_type):
bgneal@472 10
bgneal@472 11 def decorator(fn):
bgneal@690 12 logger = logging.getLogger('auth')
bgneal@472 13
bgneal@472 14 @wraps(fn)
bgneal@472 15 def wrapped(request, *args, **kwargs):
bgneal@472 16
bgneal@472 17 response = fn(request, *args, **kwargs)
bgneal@472 18
bgneal@472 19 if request.method == 'POST':
bgneal@505 20
bgneal@692 21 # Figure out if the view succeeded; success means a redirect is
bgneal@692 22 # about to occur.
bgneal@692 23 success = (response and response.has_header('location') and
bgneal@692 24 response.status_code == 302)
bgneal@505 25
bgneal@505 26 if not success:
bgneal@690 27 username = request.POST.get('username')
bgneal@690 28 username = username if username else '(None)'
bgneal@690 29 logger.error("%s failure from [%s] for %s",
bgneal@690 30 auth_type,
bgneal@690 31 request.META.get('REMOTE_ADDR', '?'),
bgneal@690 32 username)
bgneal@479 33
bgneal@472 34 return response
bgneal@472 35
bgneal@472 36 return wrapped
bgneal@472 37 return decorator