set userrealtion & organization to list of their childs - BaseVewSet

This commit is contained in:
2025-10-27 16:23:34 +03:30
parent 0af53bddb8
commit db97d0102a
3 changed files with 28 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -26,3 +26,12 @@
[2025-10-27 15:36:02,875] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-10-27 15:53:23,156] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\exceptions.py changed, reloading.
[2025-10-27 15:53:26,735] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-10-27 16:05:03,977] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\core\api.py changed, reloading.
[2025-10-27 16:05:06,153] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-10-27 16:05:14,176] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\services\service.py changed, reloading.
[2025-10-27 16:05:17,730] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-10-27 16:14:20,578] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\core\api.py changed, reloading.
[2025-10-27 16:14:23,767] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-10-27 16:15:30,229] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\core\api.py changed, reloading.
[2025-10-27 16:15:32,562] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-10-27 16:22:54,143] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\core\api.py changed, reloading.