From 95921aa8b44e5166b8d7c26b6a3d11b84c4393be Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Tue, 4 Nov 2025 11:59:20 +0330 Subject: [PATCH] fix - show org type by binary childs --- apps/authentication/api/v1/api.py | 2 +- apps/authentication/services/service.py | 14 +++++++++++++- apps/core/api.py | 7 +++++-- logs/django_requests.log | 24 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/apps/authentication/api/v1/api.py b/apps/authentication/api/v1/api.py index 3e39d37..410db6c 100644 --- a/apps/authentication/api/v1/api.py +++ b/apps/authentication/api/v1/api.py @@ -207,7 +207,7 @@ class ProvinceViewSet(SoftDeleteMixin, ModelViewSet): serializer_class = ProvinceSerializer -class OrganizationTypeViewSet(SoftDeleteMixin, ModelViewSet): +class OrganizationTypeViewSet(BaseViewSet, SoftDeleteMixin, ModelViewSet): """ Crud operations for Organization Type model """ # queryset = OrganizationType.objects.all() serializer_class = OrganizationTypeSerializer diff --git a/apps/authentication/services/service.py b/apps/authentication/services/service.py index 6e0a013..78e6893 100644 --- a/apps/authentication/services/service.py +++ b/apps/authentication/services/service.py @@ -1,6 +1,6 @@ import typing -from apps.authentication.models import Organization +from apps.authentication.models import Organization, OrganizationType def get_users_of_organization(org: Organization) -> typing.Any: @@ -28,3 +28,15 @@ def get_all_org_child(org: Organization = None) -> typing.Any: descendants.append(child) descendants.extend(get_all_org_child(child)) return descendants + + +def get_all_org_type_child(org_type: OrganizationType = None) -> typing.Any: + """ + get all child of an organization + """ + descendants = [] + children = org_type.children.all() + for child in children: + descendants.append(child) + descendants.extend(get_all_org_type_child(child)) + return descendants diff --git a/apps/core/api.py b/apps/core/api.py index 889de96..f780ebb 100644 --- a/apps/core/api.py +++ b/apps/core/api.py @@ -1,7 +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.authentication.services.service import get_all_org_child, get_all_org_type_child from apps.authentication.services.visibility_services import apply_visibility_filter from apps.authorization.services.role_child import get_all_role_child from apps.core.models import MobileTest, SystemConfig @@ -50,9 +50,12 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet): elif model_name == 'organization': queryset = queryset.filter(id__in=[org.id for org in child_orgs]) + elif model_name == 'organizationtype': # noqa + + queryset = queryset.filter(id__in=[org_type.id for org_type in get_all_org_type_child(org.type)]) + elif model_name == 'role': queryset = queryset.filter(id__in=[role.id for role in get_all_role_child(user_relation.role)]) - return queryset diff --git a/logs/django_requests.log b/logs/django_requests.log index 6fbea78..9b98921 100644 --- a/logs/django_requests.log +++ b/logs/django_requests.log @@ -1228,3 +1228,27 @@ TypeError: 'NoneType' object is not iterable [2025-11-04 11:33:45,466] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\api\v1\serializers\serializer.py changed, reloading. [2025-11-04 11:33:49,074] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader [2025-11-04 11:33:54,631] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/user-relations/?role= HTTP/1.1" 200 17248 +[2025-11-04 11:51:04,464] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\services\service.py changed, reloading. +[2025-11-04 11:51:06,283] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-11-04 11:51:14,527] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\services\service.py changed, reloading. +[2025-11-04 11:51:16,895] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-11-04 11:52:11,897] WARNING django.request | IP: 127.0.0.1 | Path: /auth/api/v1/organization-type/ | Unauthorized: /auth/api/v1/organization-type/ +[2025-11-04 11:52:11,898] WARNING django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/?org_type_field=CO HTTP/1.1" 401 191 +[2025-11-04 11:52:14,408] WARNING django.request | IP: 127.0.0.1 | Path: /auth/api/v1/organization-type/ | Unauthorized: /auth/api/v1/organization-type/ +[2025-11-04 11:52:14,409] WARNING django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/?org_type_field=CO HTTP/1.1" 401 191 +[2025-11-04 11:52:36,884] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/?org_type_field=CO HTTP/1.1" 200 577 +[2025-11-04 11:53:45,008] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 736 +[2025-11-04 11:55:07,543] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 736 +[2025-11-04 11:55:11,219] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 736 +[2025-11-04 11:55:26,356] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 736 +[2025-11-04 11:57:33,191] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\core\api.py changed, reloading. +[2025-11-04 11:57:37,132] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-11-04 11:57:42,968] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 736 +[2025-11-04 11:57:45,929] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 736 +[2025-11-04 11:58:08,395] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\core\api.py changed, reloading. +[2025-11-04 11:58:11,818] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-11-04 11:58:15,499] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 736 +[2025-11-04 11:58:16,994] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 736 +[2025-11-04 11:58:36,417] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\api\v1\api.py changed, reloading. +[2025-11-04 11:58:38,421] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-11-04 11:58:43,169] INFO django.server | IP: - | Path: - | "GET /auth/api/v1/organization-type/ HTTP/1.1" 200 428