25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 713 B

1234567891011121314151617181920212223242526272829
  1. import hashlib
  2. from django.contrib.auth.models import User as BaseUser
  3. from django.db.models.signals import post_save
  4. from django.dispatch import receiver
  5. def create_token_if_necessary(user: BaseUser):
  6. from rest_framework.authtoken.models import Token
  7. token = Token.objects.filter(user=user).first()
  8. if token is not None:
  9. return token
  10. else:
  11. return Token.objects.create(user=user)
  12. class User(BaseUser):
  13. @property
  14. def gravatar(self):
  15. return hashlib.md5(self.email.encode('utf-8')).hexdigest()
  16. class Meta:
  17. proxy = True
  18. @receiver(post_save, sender=User)
  19. def create_profile(sender, instance: User, **kwargs):
  20. create_token_if_necessary(instance)