Coverage for apps/users/models.py: 68%
44 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-08 12:47 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-08 12:47 -0600
1from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
2from django.core.validators import ValidationError
3from django.db import models
4from django.utils import timezone
5from polymorphic.models import PolymorphicModel
7from apps.accounting.models import Accounting
8from base.models import TimeStampedModel
10from .managers import UserManager
13class User(AbstractBaseUser, PermissionsMixin, PolymorphicModel):
14 """
15 User Model
16 """
18 email = models.EmailField(max_length=255)
19 first_name = models.CharField(max_length=32, null=True, blank=True)
20 last_name = models.CharField(max_length=32, null=True, blank=True)
22 is_staff = models.BooleanField(default=False)
23 is_active = models.BooleanField(default=True)
24 date_joined = models.DateTimeField(default=timezone.now, editable=False)
26 objects = UserManager()
28 USERNAME_FIELD = "email"
29 REQUIRED_FIELDS = []
32class SuperUser(User, TimeStampedModel):
33 """
34 Child model SuperUser
35 """
37 def validate_unique(self, exclude):
38 user = SuperUser.objects.exclude(pk=self.pk).filter(email=self.email)
39 if user.exists():
40 raise ValidationError("Cannot have super user with same email")
43class Employee(User, TimeStampedModel):
44 """
45 Child model Employee
46 """
48 parent_organization = models.ForeignKey(
49 "organizations.Organization", on_delete=models.PROTECT, related_name="admins"
50 )
51 registered_organizations = models.ManyToManyField("organizations.Organization", blank=True)
53 def validate_unique(self, exclude):
54 user = Employee.objects.exclude(pk=self.pk).filter(
55 email=self.email, registered_organizations__in=[self.parent_organization]
56 )
57 if user.exists():
58 raise ValidationError("Cannot have user with same email on the same organization tree")
61class Fan(User, TimeStampedModel):
62 """
63 Child model Fan
64 """
66 parent_organization = models.ForeignKey("organizations.Organization", on_delete=models.PROTECT, related_name="fans")
67 registered_organizations = models.ManyToManyField("organizations.Organization", blank=True)
69 def validate_unique(self, exclude):
70 user = Fan.objects.exclude(pk=self.pk).filter(
71 email=self.email, registered_organizations__in=[self.parent_organization]
72 )
73 if user.exists():
74 raise ValidationError("Cannot have user with same email on the same organization tree")
76 def organization_balance(self, organization):
77 try:
78 return self.accounting.get(organization=organization).balance
79 except Accounting.DoesNotExist:
80 return 0
82 class Meta:
83 ordering = ["parent_organization"]