Coverage for organizations/mixins.py: 93%
22 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-05-02 14:13 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-05-02 14:13 -0600
1from users.permissions import AdminPanel
4class OrganizationAdminMixin:
5 permission_classes = [AdminPanel]
6 model_organization = "organization"
8 @property
9 def organization(self):
10 """
11 Retrieve user organization
12 """
13 if self.request: 13 ↛ 16line 13 didn't jump to line 16, because the condition on line 13 was never false
14 return self.request.user.employee.organization
15 else:
16 return None
18 @property
19 def model(self):
20 """
21 Retrieve model used in serializer
22 """
23 return self.serializer_class.Meta.model
25 def perform_create(self, serializer):
26 """
27 When saving serializer add user organization to data
28 if model needs it
29 """
30 if "organization" in [f.name for f in self.model._meta.get_fields()]:
31 serializer.save(organization=self.organization)
32 else:
33 serializer.save()
35 def get_queryset(self):
36 """
37 Get Only Organization Related objects
38 """
39 # if self.queryset is not None:
40 # return self.queryset
41 return self.model.objects.filter(**{self.model_organization: self.organization})
43 def get_serializer_context(self):
44 """
45 Add Organization to serializers context
46 """
47 context = super(OrganizationAdminMixin, self).get_serializer_context()
48 context.update(
49 {
50 "organization": self.organization,
51 }
52 )
53 return context