python - Django 1.3 post login/logout signals in relation with authentication -


first of both methods below return true. i'd expect second 1 return false using django standard admin authentication procedure or wrong?

def post_login(sender, **kwargs):     """     django 1.3 post login signal handler     """     # stuff     user = kwargs['user']     print user.is_authenticated()  user_logged_in.connect(post_login)   def post_logout(sender, **kwargs):     """     django 1.3 post logout signal handler     """     # stuff     user = kwargs['user']     print user.is_authenticated()  user_logged_out.connect(post_logout) 

anyway i'm trying understand why django doesn't have hook on authentication failure also.. can use own backend users login , out of account, hook onto admin procedure cover in 1 portion of code.. found topics no real awnser how fix this.

i came with:

import settings  django.dispatch import signal  failed_login = signal(providing_args=['user'])  django.contrib.auth.backends import modelbackend django.contrib.auth.models import user  class authsignalbackend(modelbackend):     def authenticate(self, username=none, password=none):         try:             user = user.objects.get(username=username)             if user.check_password(password):                 return user             else:                 failed_login.send(sender=none, user=user)         except user.doesnotexist:             return none   def login_handler(sender, **kwargs):     if settings.debug:         print "failed login detected...!" failed_login.connect(login_handler) 

that works great, there's no request in modelbackend, while post_login , logout signals have request.. unfortunate because great ip logging

any advise welcome, i'm pretty sure people should have come across 1 before..

  1. if user instance of user model, user.is_authenticated() always return true. models instance can't know what's going on on request level. method views.

  2. if want deal failed login attempts, take @ django-axes. can use it, or @ code , reimplement ideas like.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -