usermodel.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from django.core.exceptions import ValidationError
  2. from django.contrib.auth.models import (AbstractBaseUser, PermissionsMixin,
  3. UserManager as BaseUserManager)
  4. from django.db import models
  5. from django.utils import timezone
  6. from django.utils.translation import ugettext_lazy as _
  7. from misago.core.utils import slugify
  8. from misago.users.utils import hash_email
  9. from misago.users.validators import (validate_email, validate_password,
  10. validate_username)
  11. class UserManager(BaseUserManager):
  12. def create_user(self, username, email, password=None, **extra_fields):
  13. if not email:
  14. raise ValueError(_("User must have an email address."))
  15. if not password:
  16. raise ValueError(_("User must have a password."))
  17. validate_username(username)
  18. validate_email(email)
  19. validate_password(password)
  20. now = timezone.now()
  21. user = self.model(is_staff=False, is_superuser=False, last_login=now,
  22. joined_on=now, **extra_fields)
  23. user.set_username(username)
  24. user.set_email(email)
  25. user.set_password(password)
  26. user.save(using=self._db)
  27. return user
  28. def create_superuser(self, username, email, password):
  29. user = self.create_user(username, email, password=password)
  30. user.is_staff = True
  31. user.is_superuser = True
  32. user.save(update_fields=['is_staff', 'is_superuser'], using=self._db)
  33. return user
  34. def get_by_username(self, username):
  35. return self.get(username_slug=slugify(username))
  36. def get_by_email(self, email):
  37. return self.get(email_hash=hash_email(email))
  38. def get_by_username_or_email(self, login):
  39. queryset = models.Q(username_slug=slugify(login))
  40. queryset = queryset | models.Q(email_hash=hash_email(login))
  41. return self.get(queryset)
  42. class User(AbstractBaseUser, PermissionsMixin):
  43. """
  44. Note that "username" field is purely for shows.
  45. When searching users by their names, always use lowercased string
  46. and username_slug field instead that is normalized around DB engines
  47. differences in case handling.
  48. """
  49. username = models.CharField(max_length=30)
  50. username_slug = models.CharField(max_length=30, unique=True)
  51. """
  52. Misago stores user email in two fields:
  53. "email" holds normalized email address
  54. "email_hash" is lowercase hash of email address used to identify account
  55. as well as enforcing on database level that no more than one user can be
  56. using one email address
  57. """
  58. email = models.EmailField(max_length=255, db_index=True)
  59. email_hash = models.CharField(max_length=32, unique=True)
  60. joined_on = models.DateTimeField(_('joined on'), default=timezone.now)
  61. is_staff = models.BooleanField(
  62. _('staff status'), default=False, db_index=True,
  63. help_text=_('Designates whether the user can log into admin sites.'))
  64. is_active = True
  65. USERNAME_FIELD = 'username_slug'
  66. REQUIRED_FIELDS = ['email']
  67. objects = UserManager()
  68. class Meta:
  69. app_label = 'users'
  70. def get_username(self):
  71. """
  72. Dirty hack: return real username instead of normalized slug
  73. """
  74. return self.username
  75. def get_full_name(self):
  76. return self.username
  77. def get_short_name(self):
  78. return self.username
  79. def set_username(self, new_username):
  80. self.username = new_username
  81. self.username_slug = slugify(new_username)
  82. def set_email(self, new_email):
  83. self.email = UserManager.normalize_email(new_email)
  84. self.email_hash = hash_email(new_email)