Coverage for apps/users/managers.py: 41%
20 statements
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-19 09:45 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2024-04-19 09:45 -0600
1from django.contrib.auth.models import BaseUserManager
4class UserManager(BaseUserManager):
5 """
6 Custom User Manager class for User model
7 """
9 use_in_migrations = True
11 def _create_user(self, email, password, **extra_fields):
12 """
13 Create and save a user with the given email, and password.
14 """
15 email = self.normalize_email(email)
16 user = self.model(email=email, **extra_fields)
17 user.set_password(password)
18 user.save(using=self._db)
19 return user
21 def create_user(self, email=None, password=None, **extra_fields):
22 """
23 Custom create user method
24 """
25 extra_fields["is_staff"] = False
26 extra_fields["is_superuser"] = False
27 return self._create_user(email, password, **extra_fields)
29 def create_superuser(self, email, password, **extra_fields):
30 """
31 Custom create superuser method
32 """
33 extra_fields["is_staff"] = True
34 extra_fields["is_superuser"] = True
35 return self._create_user(email, password, **extra_fields)
37 def create_user_without_password(self, email):
38 """
39 Define flow for creating user without a defined password,
40 for example when an employee registers a new user
41 """
42 user, _ = self.get_or_create(email=email)
43 return user