2023-05-04 04:42:37 +00:00
|
|
|
from functools import cached_property
|
|
|
|
|
2022-12-16 23:38:52 +00:00
|
|
|
import urlman
|
2022-11-05 20:17:27 +00:00
|
|
|
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
|
|
|
|
from django.db import models
|
|
|
|
|
2023-05-04 04:42:37 +00:00
|
|
|
from core.models import Config
|
|
|
|
|
2022-11-05 20:17:27 +00:00
|
|
|
|
|
|
|
class UserManager(BaseUserManager):
|
|
|
|
"""
|
|
|
|
Custom user manager that understands emails
|
|
|
|
"""
|
|
|
|
|
|
|
|
def create_user(self, email, password=None):
|
|
|
|
user = self.create(email=email)
|
|
|
|
if password:
|
|
|
|
user.set_password(password)
|
|
|
|
user.save()
|
|
|
|
return user
|
|
|
|
|
|
|
|
def create_superuser(self, email, password=None):
|
|
|
|
user = self.create(email=email, admin=True)
|
|
|
|
if password:
|
|
|
|
user.set_password(password)
|
|
|
|
user.save()
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
class User(AbstractBaseUser):
|
|
|
|
"""
|
|
|
|
Custom user model that only needs an email
|
|
|
|
"""
|
|
|
|
|
|
|
|
email = models.EmailField(unique=True)
|
|
|
|
|
|
|
|
admin = models.BooleanField(default=False)
|
|
|
|
moderator = models.BooleanField(default=False)
|
|
|
|
banned = models.BooleanField(default=False)
|
|
|
|
deleted = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|
2022-11-17 04:42:25 +00:00
|
|
|
last_seen = models.DateTimeField(auto_now_add=True)
|
2022-11-05 20:17:27 +00:00
|
|
|
|
|
|
|
USERNAME_FIELD = "email"
|
|
|
|
EMAIL_FIELD = "email"
|
2022-12-05 17:38:37 +00:00
|
|
|
REQUIRED_FIELDS: list[str] = []
|
2022-11-05 20:17:27 +00:00
|
|
|
|
|
|
|
objects = UserManager()
|
|
|
|
|
2022-12-16 23:38:52 +00:00
|
|
|
class urls(urlman.Urls):
|
|
|
|
admin = "/admin/users/"
|
|
|
|
admin_edit = "{admin}{self.pk}/"
|
|
|
|
|
2022-11-05 20:17:27 +00:00
|
|
|
@property
|
|
|
|
def is_active(self):
|
|
|
|
return not (self.deleted or self.banned)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_superuser(self):
|
|
|
|
return self.admin
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_staff(self):
|
|
|
|
return self.admin
|
2022-11-05 23:51:54 +00:00
|
|
|
|
|
|
|
def has_module_perms(self, module):
|
|
|
|
return self.admin
|
|
|
|
|
|
|
|
def has_perm(self, perm):
|
|
|
|
return self.admin
|
2023-05-04 04:42:37 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def config_user(self) -> Config.UserOptions:
|
|
|
|
return Config.load_user(self)
|