Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import unicode_literals
- from django.contrib.auth.models import (AbstractBaseUser,
- BaseUserManager,
- PermissionsMixin,)
- from django.core.mail import send_mail
- from django.db import models
- from django.utils import timezone
- from django.utils.translation import ugettext_lazy as _
- # Create your models here.
- class UserManager(BaseUserManager):
- def _create_user(self, email, password,
- is_staff, is_superuser,
- **extra_fields):
- # Timestamp for the "date_joined" field
- now = timezone.now()
- # Ensure that an email has been provided
- if not email:
- raise ValueError('Email must be set')
- # This just makes sure email domain is lowercased
- email = self.normalize_email(email)
- # reads the is_active parameter if provided, or defaults to True. This could be set to default to False
- # if you prefer to implement email verification to activate users
- is_active = extra_fields.pop("is_active", True)
- # create the user in the DB using the passed parameters and/or default values
- user = self.model(email=email, is_staff=is_staff, is_active=is_active, is_superuser=is_superuser,
- date_joined=now, **extra_fields)
- # Sets the password using django's hashing/encryption
- user.set_password(password)
- # Save the user to the DB
- user.save(using=self._db)
- # returns the user instance
- return user
- def create_user(self, email, password=None, **extra_fields):
- is_staff = extra_fields.pop("is_staff", False)
- return self._create_user(email, password, is_staff, False,
- **extra_fields)
- def create_superuser(self, email, password, **extra_fields):
- # set is_staff and is_superuser to True for superuser
- return self._create_user(email, password, True, True,
- **extra_fields)
- class User(AbstractBaseUser, PermissionsMixin):
- email = models.EmailField(_('email address'), max_length=255, unique=True, db_index=True)
- is_staff = models.BooleanField(_('staff status'), default=False, help_text=_('Designates whether the user can log into this admin site.'))
- is_active = models.BooleanField(_('active'), default=True, help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
- date_joined = models.DateTimeField(_('date joined'),default=timezone.now)
- objects = UserManager()
- USERNAME_FIELD = 'email'
- REQUIRED_FIELDS = []
- class Meta:
- verbose_name = _('user')
- verbose_name_plural = _('users')
- def get_full_name(self):
- """Return the email."""
- return self.email
- def get_short_name(self):
- """Return the email."""
- return self.email
- def email_user(self, subject, message, from_email=None, **kwargs):
- """Send an email to this User."""
- send_mail(subject, message, from_email, [self.email], **kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement