Django Library Users
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

signals.py 1.1KB

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