Debian Codegraph @

tests/auth_tests/models/custom_user.py

raw
1 from django.contrib.auth.models import (
2 AbstractBaseUser,
3 AbstractUser,
4 BaseUserManager,
5 Group,
6 Permission,
7 PermissionsMixin,
8 UserManager,
9 )
10 from django.db import models
11
12
13 # The custom user uses email as the unique identifier, and requires
14 # that every user provide a date of birth. This lets us test
15 # changes in username datatype, and non-text required fields.
16 class CustomUserManager(BaseUserManager):
17 def create_user(self, email, date_of_birth, password=None, **fields):
18 """
19 Creates and saves a User with the given email and password.
20 """
21 if not email:
22 raise ValueError("Users must have an email address")
23
24 user = self.model(
25 email=self.normalize_email(email), date_of_birth=date_of_birth, **fields
26 )
27
28 user.set_password(password)
29 user.save(using=self._db)
30 return user
31
32 async def acreate_user(self, email, date_of_birth, password=None, **fields):
33 """See create_user()"""
34 if not email:
35 raise ValueError("Users must have an email address")
36
37 user = self.model(
38 email=self.normalize_email(email), date_of_birth=date_of_birth, **fields
39 )
40
41 user.set_password(password)
42 await user.asave(using=self._db)
43 return user
44
45 def create_superuser(self, email, password, date_of_birth, **fields):
46 u = self.create_user(
47 email, password=password, date_of_birth=date_of_birth, **fields
48 )
49 u.is_admin = True
50 u.save(using=self._db)
51 return u
52
53
54 class CustomUser(AbstractBaseUser):
55 email = models.EmailField(verbose_name="email address", max_length=255, unique=True)
56 is_active = models.BooleanField(default=True)
57 is_admin = models.BooleanField(default=False)
58 date_of_birth = models.DateField()
59 first_name = models.CharField(max_length=50)
60
61 custom_objects = CustomUserManager()
62
63 USERNAME_FIELD = "email"
64 REQUIRED_FIELDS = ["date_of_birth", "first_name"]
65
66 def __str__(self):
67 return self.email
68
69 # Maybe required?
70 def get_group_permissions(self, obj=None):
71 return set()
72
73 def get_all_permissions(self, obj=None):
74 return set()
75
76 def has_perm(self, perm, obj=None):
77 return True
78
79 def has_perms(self, perm_list, obj=None):
80 return True
81
82 def has_module_perms(self, app_label):
83 return True
84
85 # Admin required fields
86 @property
87 def is_staff(self):
88 return self.is_admin
89
90
91 class RemoveGroupsAndPermissions:
92 """
93 A context manager to temporarily remove the groups and user_permissions M2M
94 fields from the AbstractUser class, so they don't clash with the
95 related_name sets.
96 """
97
98 def __enter__(self):
99 self._old_au_local_m2m = AbstractUser._meta.local_many_to_many
100 self._old_pm_local_m2m = PermissionsMixin._meta.local_many_to_many
101 groups = models.ManyToManyField(Group, blank=True)
102 groups.contribute_to_class(PermissionsMixin, "groups")
103 user_permissions = models.ManyToManyField(Permission, blank=True)
104 user_permissions.contribute_to_class(PermissionsMixin, "user_permissions")
105 PermissionsMixin._meta.local_many_to_many = [groups, user_permissions]
106 AbstractUser._meta.local_many_to_many = [groups, user_permissions]
107
108 def __exit__(self, exc_type, exc_value, traceback):
109 AbstractUser._meta.local_many_to_many = self._old_au_local_m2m
110 PermissionsMixin._meta.local_many_to_many = self._old_pm_local_m2m
111
112
113 class CustomUserWithoutIsActiveField(AbstractBaseUser):
114 username = models.CharField(max_length=150, unique=True)
115 email = models.EmailField(unique=True)
116
117 objects = UserManager()
118
119 USERNAME_FIELD = "username"
120
121
122 class CustomUserCompositePrimaryKey(AbstractBaseUser):
123 pk = models.CompositePrimaryKey("email", "date_of_birth")
124 email = models.EmailField(verbose_name="email address", max_length=255, unique=True)
125 is_active = models.BooleanField(default=True)
126 is_admin = models.BooleanField(default=False)
127 date_of_birth = models.DateField()
128
129 custom_objects = CustomUserManager()
130
131 USERNAME_FIELD = "email"
132 REQUIRED_FIELDS = ["date_of_birth"]
133
134
135 # The extension user is a simple extension of the built-in user class,
136 # adding a required date_of_birth field. This allows us to check for
137 # any hard references to the name "User" in forms/handlers etc.
138 with RemoveGroupsAndPermissions():
139
140 class ExtensionUser(AbstractUser):
141 date_of_birth = models.DateField()
142
143 custom_objects = UserManager()
144
145 REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ["date_of_birth"]