diff --git a/apps/authentication/services/service.py b/apps/authentication/services/service.py index 87a92f3..6e0a013 100644 --- a/apps/authentication/services/service.py +++ b/apps/authentication/services/service.py @@ -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 diff --git a/apps/core/api.py b/apps/core/api.py index 8808827..31d407e 100644 --- a/apps/core/api.py +++ b/apps/core/api.py @@ -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 diff --git a/logs/django_requests.log b/logs/django_requests.log index b2b94de..8e719fb 100644 --- a/logs/django_requests.log +++ b/logs/django_requests.log @@ -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.