1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import logging
- from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed
- from django.dispatch import receiver
-
- try:
- from config import APP_NAME as ROOT_LOGGER_NAME
- except ImportError:
- ROOT_LOGGER_NAME = 'root'
- logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
-
-
- @receiver(user_logged_in)
- def user_logged_in_callback(sender, request, user, **kwargs):
- # to cover more complex cases:
- # http://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
- ip = request.META.get('REMOTE_ADDR')
-
- logger.info('Accepted password for {user} from {ip}'.format(
- user=user,
- ip=ip
- ))
-
-
- @receiver(user_logged_out)
- def user_logged_out_callback(sender, request, user, **kwargs):
- ip = request.META.get('REMOTE_ADDR')
-
- logger.debug('Disconnected from user {user} {ip}'.format(
- user=user,
- ip=ip
- ))
-
-
- @receiver(user_login_failed)
- def user_login_failed_callback(sender, request, credentials, **kwargs):
- ip = request.META.get('REMOTE_ADDR')
-
- logger.warning('Failed password for {user} from {ip}'.format(
- user=credentials.get('username'),
- ip=ip
- ))
|