khaganat-web/neluser/models.py

88 lines
2.9 KiB
Python
Raw Permalink Normal View History

2018-01-27 22:12:09 +00:00
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
2018-01-27 20:06:40 +00:00
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:
2019-07-24 17:07:41 +00:00
raise ValueError("The given email must be set")
2018-01-27 20:06:40 +00:00
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):
2019-07-24 17:07:41 +00:00
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
2018-01-27 20:06:40 +00:00
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
2019-07-24 17:07:41 +00:00
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
extra_fields.setdefault("is_active", True)
2018-01-27 20:06:40 +00:00
2019-07-24 17:07:41 +00:00
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.")
2018-01-27 20:06:40 +00:00
return self._create_user(email, password, **extra_fields)
class NelUser(AbstractBaseUser, PermissionsMixin):
2019-07-24 17:07:41 +00:00
email = models.EmailField(_("email"), unique=True, null=True)
2018-01-27 20:06:40 +00:00
is_staff = models.BooleanField(
2019-07-24 17:07:41 +00:00
_("staff status"),
2018-01-27 20:06:40 +00:00
default=False,
2019-07-24 17:07:41 +00:00
help_text=_("Designates whether the user can log into this admin site."),
2018-01-27 20:06:40 +00:00
)
is_active = models.BooleanField(
2019-07-24 17:07:41 +00:00
_("active"),
default=False,
2018-01-27 20:06:40 +00:00
help_text=_(
2019-07-24 17:07:41 +00:00
"Designates whether this user should be treated as active. "
"Unselect this instead of deleting accounts."
2018-01-27 20:06:40 +00:00
),
)
2019-07-24 17:07:41 +00:00
date_joined = models.DateTimeField(_("date joined"), default=timezone.now)
2018-05-27 16:45:52 +00:00
nsfw_allowed = models.BooleanField(
2019-07-24 17:07:41 +00:00
_("NSFW flag"),
2018-05-27 16:45:52 +00:00
default=False,
help_text=_(
2019-07-24 17:07:41 +00:00
"Indicate whether or not adult or sensitive content should " "be displayed."
),
2018-05-27 16:45:52 +00:00
)
2018-01-27 20:06:40 +00:00
objects = NelUserManager()
2019-07-24 17:07:41 +00:00
EMAIL_FIELD = "email"
USERNAME_FIELD = "email"
2018-01-27 20:06:40 +00:00
REQUIRED_FIELDS = []
class Meta:
2019-07-24 17:07:41 +00:00
verbose_name = _("user")
verbose_name_plural = _("users")
2018-01-27 20:06:40 +00:00
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)