How do I add hooks to the Django Admin, such that I can execute a function when the user logs in or out?
Django does sadly not send any signals on that events.... But you could make your own custom AuthorizationBackend that enables you to do so:
from django.dispatch import Signal
post_login = Signal(providing_args=['user'])
from django.contrib.auth.backends import ModelBackend
from 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):
post_login.send(sender=None, user=user)
return user
except User.DoesNotExist:
return None
def login_handler(sender, **kwargs):
print "logging in..."
post_login.connect(login_handler)
To enable it you have to put AUTHENTICATION_BACKENDS = (myapp.mymodule.AuthSignalBackend',)
in your settings.py!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With