From 714e9abc1c67acee617d86db95e023cc029f8c59 Mon Sep 17 00:00:00 2001 From: Mojtaba-z Date: Tue, 28 Oct 2025 09:39:22 +0330 Subject: [PATCH] role childs service add to BaseViewSet --- apps/authorization/api/v1/api.py | 10 +++++++++- apps/authorization/services/role_child.py | 15 +++++++++++++++ apps/core/api.py | 7 ++++++- logs/django_requests.log | 4 ++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 apps/authorization/services/role_child.py diff --git a/apps/authorization/api/v1/api.py b/apps/authorization/api/v1/api.py index 8620083..103c6f0 100644 --- a/apps/authorization/api/v1/api.py +++ b/apps/authorization/api/v1/api.py @@ -25,7 +25,7 @@ from apps.core.mixins.search_mixin import DynamicSearchMixin from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin -class RoleViewSet(SoftDeleteMixin, viewsets.ModelViewSet): +class RoleViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewSet): """ Crud Operations For User Roles """ queryset = Role.objects.all() @@ -33,6 +33,14 @@ class RoleViewSet(SoftDeleteMixin, viewsets.ModelViewSet): filter_backends = [filters.SearchFilter] search_fields = ['role_name', 'type__name'] + def list(self, request, *args, **kwargs): + """ all roles """ + + role = self.paginate_queryset(self.get_queryset().order_by('-modify_date')) + if role is not None: # noqa + serializer = self.get_serializer(role, many=True) + return self.get_paginated_response(serializer.data) + class PageViewSet(SoftDeleteMixin, viewsets.ModelViewSet): """ add website pages to system to set permission on it """ diff --git a/apps/authorization/services/role_child.py b/apps/authorization/services/role_child.py new file mode 100644 index 0000000..960a092 --- /dev/null +++ b/apps/authorization/services/role_child.py @@ -0,0 +1,15 @@ +import typing + +from apps.authorization.models import Role + + +def get_all_role_child(role: Role = None) -> typing.Any: + """ + get all child of an role + """ + descendants = [] + children = role.child.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 605d15a..501cff3 100644 --- a/apps/core/api.py +++ b/apps/core/api.py @@ -2,6 +2,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.authorization.services.role_child import get_all_role_child from apps.core.models import MobileTest, SystemConfig from apps.core.serializers import MobileTestSerializer, SystemConfigSerializer @@ -23,7 +24,8 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet): queryset = self.filter_by_region(queryset, org=True) if user_relation.exists(): - if not user_relation.first().role.type.key == 'ADM': + user_relation = user_relation.first() + if not user_relation.role.type.key == 'ADM': # get all child orgs child_orgs = get_all_org_child(org) @@ -35,6 +37,9 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet): elif model_name == 'organization': queryset = queryset.filter(id__in=[org.id for org in child_orgs]) + 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 00ddd4a..927a45e 100644 --- a/logs/django_requests.log +++ b/logs/django_requests.log @@ -280,3 +280,7 @@ django.core.exceptions.FieldError: Unsupported lookup 'name' for ForeignKey or j [2025-10-28 09:08:01,206] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader [2025-10-28 09:15:25,146] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authorization\api\v1\serializers.py changed, reloading. [2025-10-28 09:15:27,109] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-10-28 09:32:41,931] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authorization\api\v1\api.py changed, reloading. +[2025-10-28 09:32:44,094] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader +[2025-10-28 09:38:33,191] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\core\api.py changed, reloading. +[2025-10-28 09:38:36,690] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader