expalert/custom_auth/models.py
2023-12-04 16:09:40 +01:00

50 lines
No EOL
1.3 KiB
Python

from django.contrib.auth.models import AbstractBaseUser, UserManager as DjangoUserManager
from django.db import models
import uuid
class UserManager(DjangoUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError('Users must have an email address')
user = self.model(email=email, **extra_fields)
if password:
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_superuser', True)
return self.create_user(email, password, **extra_fields)
class User(AbstractBaseUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
email = models.EmailField(unique=True)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
objects = UserManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return self.is_superuser
@property
def is_staff(self):
return True
@property
def username(self):
return self.email