role childs service add to BaseViewSet

This commit is contained in:
2025-10-28 09:39:22 +03:30
parent 1204fad1a0
commit 714e9abc1c
4 changed files with 34 additions and 2 deletions

View File

@@ -25,7 +25,7 @@ from apps.core.mixins.search_mixin import DynamicSearchMixin
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin 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 """ """ Crud Operations For User Roles """
queryset = Role.objects.all() queryset = Role.objects.all()
@@ -33,6 +33,14 @@ class RoleViewSet(SoftDeleteMixin, viewsets.ModelViewSet):
filter_backends = [filters.SearchFilter] filter_backends = [filters.SearchFilter]
search_fields = ['role_name', 'type__name'] 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): class PageViewSet(SoftDeleteMixin, viewsets.ModelViewSet):
""" add website pages to system to set permission on it """ """ add website pages to system to set permission on it """

View File

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

View File

@@ -2,6 +2,7 @@ from rest_framework import viewsets
from apps.authentication.mixins.region_filter import RegionFilterMixin, get_organization_by_user 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
from apps.authorization.services.role_child import get_all_role_child
from apps.core.models import MobileTest, SystemConfig from apps.core.models import MobileTest, SystemConfig
from apps.core.serializers import MobileTestSerializer, SystemConfigSerializer from apps.core.serializers import MobileTestSerializer, SystemConfigSerializer
@@ -23,7 +24,8 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet):
queryset = self.filter_by_region(queryset, org=True) queryset = self.filter_by_region(queryset, org=True)
if user_relation.exists(): 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 # get all child orgs
child_orgs = get_all_org_child(org) child_orgs = get_all_org_child(org)
@@ -35,6 +37,9 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet):
elif model_name == 'organization': elif model_name == 'organization':
queryset = queryset.filter(id__in=[org.id for org in child_orgs]) 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 return queryset

View File

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