123 lines
4.5 KiB
Python
123 lines
4.5 KiB
Python
from django.db import models
|
|
from django.contrib import auth
|
|
from django.core.mail import send_mail
|
|
from django.contrib.auth.base_user import BaseUserManager, AbstractBaseUser
|
|
from django.contrib.auth.models import PermissionsMixin
|
|
from django.utils import timezone
|
|
from django.utils.text import slugify
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from .validators.username import usernameValidator
|
|
|
|
class PuckoBase_Manager(BaseUserManager):
|
|
use_in_migrations = True
|
|
|
|
def _create_user(self, username, email, password, **extra_fields):
|
|
if not username:
|
|
raise ValueError('Du måste ha ett användarnamn!')
|
|
email = self.normalize_email(email)
|
|
username = self.model.normalize_username(username)
|
|
user = self.model(username=username, email=email, **extra_fields)
|
|
user.set_password(password)
|
|
user.save(using=self._db)
|
|
|
|
def create_user(self, username, email=None, password=None, **extra_fields):
|
|
extra_fields.setdefault('is_staff', False)
|
|
extra_fields.setdefault('is_superuser', False)
|
|
return self._create_user(username, email, password, **extra_fields)
|
|
|
|
def create_superuser(self, username, email=None, password=None, **extra_fields):
|
|
extra_fields.setdefault('is_staff', True)
|
|
extra_fields.setdefault('is_superuser', 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(username, email, password, **extra_fields)
|
|
|
|
def with_perm(self, perm, is_active=True, include_superusers=True, backend=None, obj=None):
|
|
if backend is None:
|
|
backends = auth._get_backends(return_tuples=True)
|
|
if len(backends) == 1:
|
|
backend, tmp = backends[0]
|
|
else:
|
|
raise ValueError(
|
|
(f'You have multiple authentication backends configured and '
|
|
f'therefor must provide the `backend` argument.')
|
|
)
|
|
elif not isinstance(backend, str):
|
|
raise TypeError(
|
|
f'backend must be a dotted import path string (got {backend}).'
|
|
)
|
|
else:
|
|
backend = auth.load_backend(backend)
|
|
if hasattr(backend, 'with_perm'):
|
|
return backend.with_perm(
|
|
perm,
|
|
is_active=is_active,
|
|
include_superusers=include_superusers,
|
|
obj=obj,
|
|
)
|
|
return self.none()
|
|
|
|
class PuckoBase_User(AbstractBaseUser, PermissionsMixin):
|
|
username = models.CharField(_('username'), max_length=150, unique=True, validators=[usernameValidator])
|
|
email = models.EmailField(_('email address'), blank=True, null=True)
|
|
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
|
|
is_active = models.BooleanField(_('active'), default=True)
|
|
is_staff = models.BooleanField(_('staff status'), default=False)
|
|
slug = models.SlugField(max_length=70, unique=True, blank=True, null=True)
|
|
|
|
objects = PuckoBase_Manager()
|
|
EMAIL_FIELD = 'email'
|
|
USERNAME_FIELD = 'username'
|
|
REQUIRED_FIELDS = []
|
|
|
|
class Meta:
|
|
verbose_name = _('pucko user')
|
|
verbose_name_plural = _('pucko users')
|
|
abstract = True
|
|
|
|
def clean(self):
|
|
super().clean()
|
|
if self.email:
|
|
self.email = self.__class__.objects.normalize_email(self.email)
|
|
|
|
def save(self, *args, **kwargs):
|
|
self.slug = slugify(self.username.lower())
|
|
super(PuckoBase_User, self).save(*args, **kwargs)
|
|
|
|
@classmethod
|
|
def this(cls, username):
|
|
return cls.objects.get(username=username)
|
|
|
|
@classmethod
|
|
def create(cls, username, password, superuser=False):
|
|
if superuser:
|
|
cls.objects.create_superuser(
|
|
username, password=password,
|
|
)
|
|
else:
|
|
cls.objects.create_user(
|
|
username, password=password,
|
|
)
|
|
return cls.this(username)
|
|
|
|
@classmethod
|
|
def get_all(cls):
|
|
return cls.objects.all()
|
|
|
|
def get_username(self):
|
|
return f'{self.username}'
|
|
|
|
def email_user(self, subject, message, from_email=None, **kwargs):
|
|
if not self.email:
|
|
return False
|
|
send_mail(subject, message, from_email, [self.email], **kwargs)
|
|
return True
|
|
|
|
def __str__(self):
|
|
return self.username
|