Coverage for apps/users/mixins.py: 47%
39 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-19 14:43 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-19 14:43 -0600
1from rest_framework.permissions import AllowAny
3from apps.organizations.models import Organization
4from apps.users.models import Employee, SuperUser
5from apps.users.permissions import EmployeePanel, FanPanel, SuperUserPanel
8class BaseMixin:
9 model_fan = "fan"
10 model_organization = "organization"
12 @property
13 def organization(self):
14 """
15 Retrieve user organization
16 """
17 if self.request:
18 try:
19 return Organization.objects.get(domain=self.request.headers.get("Origin", None))
20 except Organization.DoesNotExist:
21 return None
22 else:
23 return None
25 @property
26 def model(self):
27 """
28 Retrieve model used in serializer
29 """
30 return self.serializer_class.Meta.model
32 def get_queryset(self):
33 """
34 Get Only Organization Related objects
35 """
36 if self.queryset:
37 return self.queryset
38 return self.model.objects.filter(**{self.model_organization: self.organization})
41class EmployeeMixin(BaseMixin):
42 permission_classes = [SuperUserPanel | EmployeePanel]
44 def get_queryset(self):
45 """
46 Get Only Organization Related objects
47 """
48 if self.queryset:
49 return self.queryset
50 if isinstance(self.request.user, Employee):
51 return self.model.objects.filter(
52 **{f"{self.model_organization}__in": self.request.user.registered_organizations.all()}
53 )
54 elif isinstance(self.request.user, SuperUser):
55 return self.model.objects.all()
58class FanMixin(BaseMixin):
59 permission_classes = [FanPanel]
61 def get_queryset(self):
62 """
63 Get Only Fan Related objects
64 """
65 if self.queryset:
66 return self.queryset
67 else:
68 return self.model.objects.filter(**{f"{self.model_fan}": self.request.user})
71class PublicMixin(BaseMixin):
72 permission_classes = [AllowAny]