diff --git a/apps/applications/api/mixin.py b/apps/applications/api/mixin.py index 7833b46ba..91c7d7b3f 100644 --- a/apps/applications/api/mixin.py +++ b/apps/applications/api/mixin.py @@ -1,4 +1,5 @@ from common.exceptions import JMSException +from orgs.models import Organization from .. import models @@ -85,14 +86,46 @@ class SerializeApplicationToTreeNodeMixin: 'meta': {'type': 'k8s_app'} } - def _serialize(self, application): + def _serialize_application(self, application): method_name = f'_serialize_{application.category}' data = getattr(self, method_name)(application) data.update({ + 'pId': application.org.id, 'org_name': application.org_name }) return data def serialize_applications(self, applications): - data = [self._serialize(application) for application in applications] + data = [self._serialize_application(application) for application in applications] + return data + + @staticmethod + def _serialize_organization(org): + return { + 'id': org.id, + 'name': org.name, + 'title': org.name, + 'pId': '', + 'open': True, + 'isParent': True, + 'meta': { + 'type': 'node' + } + } + + def serialize_organizations(self, organizations): + data = [self._serialize_organization(org) for org in organizations] + return data + + @staticmethod + def filter_organizations(applications): + organizations_id = set(applications.values_list('org_id', flat=True)) + organizations = [Organization.get_instance(org_id) for org_id in organizations_id] + return organizations + + def serialize_applications_with_org(self, applications): + organizations = self.filter_organizations(applications) + data_organizations = self.serialize_organizations(organizations) + data_applications = self.serialize_applications(applications) + data = data_organizations + data_applications return data diff --git a/apps/orgs/mixins/models.py b/apps/orgs/mixins/models.py index af3a8a30f..f2f6ae49e 100644 --- a/apps/orgs/mixins/models.py +++ b/apps/orgs/mixins/models.py @@ -8,7 +8,7 @@ from django.core.exceptions import ValidationError from common.utils import get_logger from ..utils import ( set_current_org, get_current_org, current_org, - filter_org_queryset, get_org_name_by_id + filter_org_queryset, get_org_by_id, get_org_name_by_id ) from ..models import Organization @@ -70,9 +70,7 @@ class OrgModelMixin(models.Model): @property def org(self): - from orgs.models import Organization - org = Organization.get_instance(self.org_id) - return org + return get_org_by_id(self.org_id) @property def org_name(self): diff --git a/apps/orgs/utils.py b/apps/orgs/utils.py index 356fac9bd..c10a5dacc 100644 --- a/apps/orgs/utils.py +++ b/apps/orgs/utils.py @@ -90,10 +90,15 @@ def get_org_mapper(): return org_mapper -def get_org_name_by_id(org_id): +def get_org_by_id(org_id): org_id = str(org_id) org_mapper = get_org_mapper() org = org_mapper.get(org_id) + return org + + +def get_org_name_by_id(org_id): + org = get_org_by_id(org_id) if org: org_name = org.name else: diff --git a/apps/perms/api/application/user_permission/user_permission_applications.py b/apps/perms/api/application/user_permission/user_permission_applications.py index c74adb3d1..848e22304 100644 --- a/apps/perms/api/application/user_permission/user_permission_applications.py +++ b/apps/perms/api/application/user_permission/user_permission_applications.py @@ -48,7 +48,7 @@ class ApplicationsAsTreeMixin(SerializeApplicationToTreeNodeMixin): def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) - data = self.serialize_applications(queryset) + data = self.serialize_applications_with_org(queryset) return Response(data=data) diff --git a/apps/perms/api/asset/user_permission/user_permission_nodes_with_assets.py b/apps/perms/api/asset/user_permission/user_permission_nodes_with_assets.py index 4f9de071d..44d8f22b3 100644 --- a/apps/perms/api/asset/user_permission/user_permission_nodes_with_assets.py +++ b/apps/perms/api/asset/user_permission/user_permission_nodes_with_assets.py @@ -139,11 +139,13 @@ class MyGrantedNodesWithAssetsAsTreeApi(SerializeToTreeNodeMixin, ListAPIView): return Response(data=data) -class UserGrantedNodeChildrenWithAssetsAsTreeForAdminApi(ForAdminMixin, UserNodeGrantStatusDispatchMixin, - SerializeToTreeNodeMixin, ListAPIView): +class GrantedNodeChildrenWithAssetsAsTreeApiMixin(UserNodeGrantStatusDispatchMixin, + SerializeToTreeNodeMixin, + ListAPIView): """ 带资产的授权树 """ + user: None def get_data_on_node_direct_granted(self, key): nodes = Node.objects.filter(parent_key=key) @@ -203,5 +205,9 @@ class UserGrantedNodeChildrenWithAssetsAsTreeForAdminApi(ForAdminMixin, UserNode return Response(data=[*tree_nodes, *tree_assets]) -class MyGrantedNodeChildrenWithAssetsAsTreeApi(ForUserMixin, UserGrantedNodeChildrenWithAssetsAsTreeForAdminApi): +class UserGrantedNodeChildrenWithAssetsAsTreeApi(ForAdminMixin, GrantedNodeChildrenWithAssetsAsTreeApiMixin): + pass + + +class MyGrantedNodeChildrenWithAssetsAsTreeApi(ForUserMixin, GrantedNodeChildrenWithAssetsAsTreeApiMixin): pass diff --git a/apps/perms/urls/asset_permission.py b/apps/perms/urls/asset_permission.py index f7e10e0fc..7b251dea7 100644 --- a/apps/perms/urls/asset_permission.py +++ b/apps/perms/urls/asset_permission.py @@ -56,7 +56,7 @@ user_permission_urlpatterns = [ path('nodes-with-assets/tree/', api.MyGrantedNodesWithAssetsAsTreeApi.as_view(), name='my-nodes-with-assets-as-tree'), # 主要用于 luna 页面,带资产的节点树 - path('/nodes/children-with-assets/tree/', api.UserGrantedNodeChildrenWithAssetsAsTreeForAdminApi.as_view(), name='user-nodes-children-with-assets-as-tree'), + path('/nodes/children-with-assets/tree/', api.UserGrantedNodeChildrenWithAssetsAsTreeApi.as_view(), name='user-nodes-children-with-assets-as-tree'), path('nodes/children-with-assets/tree/', api.MyGrantedNodeChildrenWithAssetsAsTreeApi.as_view(), name='my-nodes-children-with-assets-as-tree'), # 查询授权树上某个节点的所有资产