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): 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