set userrealtion & organization to list of their childs - BaseVewSet
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import typing
|
||||
|
||||
from apps.authentication.models import Organization
|
||||
|
||||
|
||||
@@ -15,3 +16,15 @@ def get_users_of_organization(org: Organization) -> typing.Any:
|
||||
} for rel in user_relations]
|
||||
|
||||
return users_list
|
||||
|
||||
|
||||
def get_all_org_child(org: Organization = None) -> typing.Any:
|
||||
"""
|
||||
get all child of an organization
|
||||
"""
|
||||
descendants = []
|
||||
children = org.parents.all()
|
||||
for child in children:
|
||||
descendants.append(child)
|
||||
descendants.extend(get_all_org_child(child))
|
||||
return descendants
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from rest_framework import viewsets
|
||||
|
||||
from apps.authentication.mixins.region_filter import RegionFilterMixin, get_organization_by_user
|
||||
from apps.authentication.services.service import get_all_org_child
|
||||
from apps.core.models import MobileTest, SystemConfig
|
||||
from apps.core.serializers import MobileTestSerializer, SystemConfigSerializer
|
||||
|
||||
@@ -20,17 +21,19 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet):
|
||||
|
||||
if self.request.method.lower() == 'get' and not self.kwargs.get('pk'):
|
||||
queryset = self.filter_by_region(queryset, org=True)
|
||||
print(queryset)
|
||||
|
||||
if user_relation.exists():
|
||||
if not user_relation.first().role.type.key == 'ADM':
|
||||
# get all child orgs
|
||||
child_orgs = get_all_org_child(org)
|
||||
|
||||
model_name = queryset.model.__name__.lower()
|
||||
|
||||
if model_name == 'userrelations': # noqa
|
||||
queryset = (queryset.exclude(id=user_relation.first().id)).exclude(role__type__key='ADM')
|
||||
queryset = (queryset.filter(organization__in=child_orgs))
|
||||
|
||||
elif model_name == 'organization':
|
||||
queryset = queryset.exclude(id=user_relation.first().organization.id)
|
||||
queryset = queryset.filter(id__in=child_orgs)
|
||||
|
||||
return queryset
|
||||
|
||||
|
||||
Reference in New Issue
Block a user