signals.py 880 B

12345678910111213141516171819202122232425
  1. from django.dispatch import receiver
  2. from django.db.models.signals import pre_save, post_save
  3. from django.template.defaultfilters import slugify
  4. from django.utils.crypto import get_random_string
  5. from .models import Profile
  6. from adhesions.models import User
  7. @receiver(pre_save, sender=User, dispatch_uid='set_unusable_password')
  8. def set_unusable_password(sender, instance, **kwargs):
  9. if not instance.password:
  10. instance.set_password(get_random_string(length=32))
  11. @receiver(pre_save, sender=User, dispatch_uid='set_default_username')
  12. def set_default_username(sender, instance, **kwargs):
  13. if not instance.username:
  14. instance.username = slugify(instance.get_full_name())
  15. @receiver(post_save, sender=User, dispatch_uid='create_profile')
  16. def create_profile(sender, instance, created, **kwargs):
  17. if created:
  18. Profile.objects.create(user=instance)