from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin
from django.db import models
from django.db.models import BooleanField, CharField, EmailField
class Common(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class TeamMgr(BaseUserManager):
def create_user(self, email, password=None):
if not email:
raise ValueError("Users must have an email address")
user = self.model(
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None):
user = self.create_user(
email,
password=password,
)
user.is_admin = True
user.save(using=self._db)
return user
class Team(Common):
name = models.CharField(max_length=256)
def __str__(self):
return self.name
class TeamUser(AbstractBaseUser, PermissionsMixin):
"""
The `TeamUser` class is a custom user model that extends the `AbstractBaseUser` and `PermissionsMixin` classes from Django's built-in authentication system. It provides a user model with the following fields:
- `email`: The user's email address, which is used as the unique identifier for the user.
- `first_name`: The user's first name (optional).
- `last_name`: The user's last name (optional).
- `dapsy`: A boolean field indicating whether the user is a DAPSY user.
- `is_active`: A boolean field indicating whether the user is active.
- `is_admin`: A boolean field indicating whether the user is an admin.
- `team`: A foreign key relationship to the `Team` model, indicating the team the user belongs to.
- `lead_for`: A many-to-many relationship to the `engagements.Organisation` model, indicating the organizations the user is a lead inspector for.
- `designation`: The user's designation (optional).
The `TeamUser` class also overrides the default user manager with the `TeamMgr` class, which provides custom methods for creating regular and superuser accounts.
The `__str__` method returns the user's email address, and the `fullname` method returns the user's full name (first and last name).
The `has_perm` and `has_module_perms` methods are placeholders that always return `True`, indicating that the user has all permissions. The `is_staff` property is also a placeholder that returns the value of `is_admin`.
"""
email = EmailField(verbose_name="email address", max_length=255, unique=True)
first_name = CharField(max_length=20, null=True, blank=True)
last_name = CharField(max_length=20, null=True, blank=True)
dapsy = BooleanField(default=False)
is_active = BooleanField(default=True)
is_admin = BooleanField(default=False)
team = models.ForeignKey(Team, null=True, on_delete=models.CASCADE, related_name="team_members")
lead_for = models.ManyToManyField("engagements.Organisation", blank=True, related_name="lead_inspector")
designation = models.CharField(max_length=3, null=True, blank=True)
USERNAME_FIELD = "email"
# EMAIL_FIELD = "email"
objects = TeamMgr()
def __str__(self):
return self.email
def fullname(self):
return f"{self.first_name} {self.last_name}"
def has_perm(self, perm, obj=None):
"Does this user have a specific permission? Set True for now."
return True
def has_module_perms(self, app_label):
"Does user have permissions to view the app `app_label`? Yes for now."
return True
@property
def is_staff(self):
return self.is_admin