summaryrefslogtreecommitdiffstats
path: root/users/models/user.py
blob: 08a703eaa595999afe5774918ecf906a9c7f0182 (plain)
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
from typing import List

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models


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)
    last_seen = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = "email"
    EMAIL_FIELD = "email"
    REQUIRED_FIELDS: List[str] = []

    objects = UserManager()

    @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

    def has_module_perms(self, module):
        return self.admin

    def has_perm(self, perm):
        return self.admin