Django Library Users
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

signals.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import logging
  2. from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed
  3. from django.dispatch import receiver
  4. try:
  5. from config import APP_NAME as ROOT_LOGGER_NAME
  6. except ImportError:
  7. ROOT_LOGGER_NAME = 'root'
  8. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  9. @receiver(user_logged_in)
  10. def user_logged_in_callback(sender, request, user, **kwargs):
  11. # to cover more complex cases:
  12. # http://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
  13. ip = request.META.get('REMOTE_ADDR')
  14. logger.info('Accepted password for {user} from {ip}'.format(
  15. user=user,
  16. ip=ip
  17. ))
  18. @receiver(user_logged_out)
  19. def user_logged_out_callback(sender, request, user, **kwargs):
  20. ip = request.META.get('REMOTE_ADDR')
  21. logger.debug('Disconnected from user {user} {ip}'.format(
  22. user=user,
  23. ip=ip
  24. ))
  25. @receiver(user_login_failed)
  26. def user_login_failed_callback(sender, request, credentials, **kwargs):
  27. ip = request.META.get('REMOTE_ADDR')
  28. logger.warning('Failed password for {user} from {ip}'.format(
  29. user=credentials.get('username'),
  30. ip=ip
  31. ))