Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. from __future__ import unicode_literals
  2.  
  3. from django.contrib.auth.models import (AbstractBaseUser,
  4. BaseUserManager,
  5. PermissionsMixin,)
  6. from django.core.mail import send_mail
  7. from django.db import models
  8. from django.utils import timezone
  9. from django.utils.translation import ugettext_lazy as _
  10.  
  11. # Create your models here.
  12.  
  13.  
  14. class UserManager(BaseUserManager):
  15.  
  16. def _create_user(self, email, password,
  17. is_staff, is_superuser,
  18. **extra_fields):
  19.  
  20. # Timestamp for the "date_joined" field
  21. now = timezone.now()
  22. # Ensure that an email has been provided
  23. if not email:
  24. raise ValueError('Email must be set')
  25.  
  26. # This just makes sure email domain is lowercased
  27. email = self.normalize_email(email)
  28.  
  29. # reads the is_active parameter if provided, or defaults to True. This could be set to default to False
  30. # if you prefer to implement email verification to activate users
  31. is_active = extra_fields.pop("is_active", True)
  32.  
  33. # create the user in the DB using the passed parameters and/or default values
  34. user = self.model(email=email, is_staff=is_staff, is_active=is_active, is_superuser=is_superuser,
  35. date_joined=now, **extra_fields)
  36.  
  37. # Sets the password using django's hashing/encryption
  38. user.set_password(password)
  39.  
  40. # Save the user to the DB
  41. user.save(using=self._db)
  42.  
  43. # returns the user instance
  44. return user
  45.  
  46. def create_user(self, email, password=None, **extra_fields):
  47. is_staff = extra_fields.pop("is_staff", False)
  48. return self._create_user(email, password, is_staff, False,
  49. **extra_fields)
  50.  
  51. def create_superuser(self, email, password, **extra_fields):
  52. # set is_staff and is_superuser to True for superuser
  53. return self._create_user(email, password, True, True,
  54. **extra_fields)
  55.  
  56.  
  57. class User(AbstractBaseUser, PermissionsMixin):
  58. email = models.EmailField(_('email address'), max_length=255, unique=True, db_index=True)
  59. is_staff = models.BooleanField(_('staff status'), default=False, help_text=_('Designates whether the user can log into this admin site.'))
  60. is_active = models.BooleanField(_('active'), default=True, help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
  61. date_joined = models.DateTimeField(_('date joined'),default=timezone.now)
  62.  
  63. objects = UserManager()
  64.  
  65. USERNAME_FIELD = 'email'
  66. REQUIRED_FIELDS = []
  67.  
  68. class Meta:
  69. verbose_name = _('user')
  70. verbose_name_plural = _('users')
  71.  
  72. def get_full_name(self):
  73. """Return the email."""
  74. return self.email
  75.  
  76. def get_short_name(self):
  77. """Return the email."""
  78. return self.email
  79.  
  80. def email_user(self, subject, message, from_email=None, **kwargs):
  81. """Send an email to this User."""
  82. send_mail(subject, message, from_email, [self.email], **kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement