82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
|
|
from django.contrib.auth.models import BaseUserManager
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.core.mail import send_mail
|
|
from django.utils import timezone
|
|
from django.db import models
|
|
|
|
|
|
class NelUserManager(BaseUserManager):
|
|
use_in_migrations = True
|
|
|
|
def _create_user(self, email, password, **extra_fields):
|
|
if not email:
|
|
raise ValueError('The given email must be set')
|
|
email = self.normalize_email(email)
|
|
user = self.model(email=email, **extra_fields)
|
|
user.set_password(password)
|
|
user.save()
|
|
return user
|
|
|
|
def create_user(self, email, password=None, **extra_fields):
|
|
extra_fields.setdefault('is_staff', False)
|
|
extra_fields.setdefault('is_superuser', False)
|
|
return self._create_user(email, password, **extra_fields)
|
|
|
|
def create_superuser(self, email, password, **extra_fields):
|
|
extra_fields.setdefault('is_staff', True)
|
|
extra_fields.setdefault('is_superuser', True)
|
|
extra_fields.setdefault('is_active', True)
|
|
|
|
if extra_fields.get('is_staff') is not True:
|
|
raise ValueError('Superuser must have is_staff=True.')
|
|
if extra_fields.get('is_superuser') is not True:
|
|
raise ValueError('Superuser must have is_superuser=True.')
|
|
|
|
return self._create_user(email, password, **extra_fields)
|
|
|
|
|
|
class NelUser(AbstractBaseUser, PermissionsMixin):
|
|
email = models.EmailField(unique=True, null=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 = NelUserManager()
|
|
|
|
EMAIL_FIELD = 'email'
|
|
USERNAME_FIELD = 'email'
|
|
REQUIRED_FIELDS = []
|
|
|
|
class Meta:
|
|
verbose_name = _('user')
|
|
verbose_name_plural = _('users')
|
|
|
|
def __str__(self):
|
|
return self.email.strip()
|
|
|
|
def clean(self):
|
|
super().clean()
|
|
self.email = self.__class__.objects.normalize_email(self.email)
|
|
|
|
def get_full_name(self):
|
|
return self.email.strip()
|
|
|
|
def get_short_name(self):
|
|
return self.email.strip()
|
|
|
|
def email_user(self, subject, message, from_email=None, **kwargs):
|
|
send_mail(subject, message, from_email, [self.email], **kwargs)
|