Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. class CustomUserManager(BaseUserManager):
  2.  
  3. def _create_user(self, email, password,
  4. is_staff, is_superuser, **extra_fields):
  5. """
  6. Creates and saves a User with the given email and password.
  7. """
  8. now = timezone.now()
  9. if not email:
  10. raise ValueError('The given email must be set')
  11. email = self.normalize_email(email)
  12. user = self.model(email=email,
  13. is_staff=is_staff, is_active=True,
  14. is_superuser=is_superuser, last_login=now,
  15. date_joined=now, **extra_fields)
  16. user.set_password(password)
  17. user.save(using=self._db)
  18. return user
  19.  
  20. def create_user(self, email, password=None, **extra_fields):
  21. return self._create_user(email, password, False, False,
  22. **extra_fields)
  23.  
  24. def create_superuser(self, email, password, **extra_fields):
  25. return self._create_user(email, password, True, True,
  26. **extra_fields)
  27.  
  28.  
  29. class CustomUser(AbstractBaseUser, PermissionsMixin):
  30.  
  31. username = models.CharField(
  32. _('username'),
  33. max_length=30,
  34. unique=True,
  35. help_text=_('Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'),
  36. validators=[
  37. validators.RegexValidator(
  38. r'^[\w.@+-]+$',
  39. _('Enter a valid username. This value may contain only '
  40. 'letters, numbers ' 'and @/./+/-/_ characters.')
  41. ),
  42. ],
  43. error_messages={
  44. 'unique': _("A user with that username already exists."),
  45. },
  46. )
  47. first_name = models.CharField(_('first name'), max_length=30, blank=True)
  48. last_name = models.CharField(_('last name'), max_length=30, blank=True)
  49. email = models.EmailField(_('email address'), blank=True)
  50. is_staff = models.BooleanField(_('staff status'), default=False,
  51. help_text=_('Designates whether the user can log into this admin site.'))
  52. is_active = models.BooleanField(_('active'), default=True,
  53. help_text=_('Designates whether this user should be treated as '
  54. 'active. Unselect this instead of deleting accounts.'))
  55. date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
  56.  
  57. objects = CustomUserManager()
  58.  
  59. USERNAME_FIELD = 'username'
  60. REQUIRED_FIELDS = []
  61.  
  62. class Meta:
  63. verbose_name = _('user')
  64. verbose_name_plural = _('users')
  65.  
  66. def get_absolute_url(self):
  67. return "/users/%s/" % urlquote(self.email)
  68.  
  69. def get_full_name(self):
  70. """
  71. Returns the first_name plus the last_name, with a space in between.
  72. """
  73. full_name = '%s %s' % (self.first_name, self.last_name)
  74. return full_name.strip()
  75.  
  76. def get_short_name(self):
  77. """
  78. Returns the short name for the user.
  79. """
  80. return self.first_name
  81.  
  82. def email_user(self, subject, message, from_email=None):
  83. """
  84. Sends an email to this User.
  85. """
  86. send_mail(subject, message, from_email, [self.email])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement