Coverage for backend/django/authentication/user/models.py: 62%
39 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-12-18 04:00 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-12-18 04:00 +0000
1from django.db import models
2from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin, AnonymousUser
5# Create your models here.
7class UserManager(BaseUserManager):
8 def create_user(self, username, email=None, password=None, **kwargs):
9 """Create and return a `User` with a phone number, username and password."""
10 if username is None: 10 ↛ 11line 10 didn't jump to line 11 because the condition on line 10 was never true
11 raise TypeError('Users must have a username.')
13 user = self.model(username=username, email=self.normalize_email(email))
14 user.set_password(password)
15 user.save(using=self._db)
17 return user
19 def create_superuser(self, username, password):
20 """
21 Create and return a `User` with superuser (admin) permissions.
22 """
23 if password is None:
24 raise TypeError('Superusers must have a password.')
25 if username is None:
26 raise TypeError('Superusers must have an username.')
28 user = self.create_user(username, password)
29 user.is_superuser = True
30 user.is_staff = True
31 user.save(using=self._db)
33 return user
36class User(AbstractBaseUser, PermissionsMixin):
37 username = models.CharField(db_index=True, max_length=255, unique=True)
38 email = models.EmailField(db_index=True, max_length=255, null=True)
39 is_active = models.BooleanField(default=True)
40 is_staff = models.BooleanField(default=False)
41 is_tester = models.BooleanField(default=False)
42 first_name = models.CharField(max_length=50, default="")
43 last_name = models.CharField(max_length=50, default="")
45 USERNAME_FIELD = 'username'
47 objects = UserManager()
49 def __str__(self):
50 return f"{self.id}"
52class BotUser(AnonymousUser):
53 @property
54 def is_authenticated(self):
55 return True
57 @property
58 def is_anonymous(self):
59 return True