summaryrefslogtreecommitdiffstats
path: root/myuser/models.py
blob: 1236ba680abe6f08c82c57b5738fb3d1986f608d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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