From 785e4cc3e47e36a53427721f817e51a70831cefb Mon Sep 17 00:00:00 2001 From: "fangfang.dong" Date: Mon, 3 Jul 2023 16:33:58 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=8E=A5=E5=8F=A3sql=E4=BC=98=E5=8C=96?= =?UTF-8?q?=20/api/v1/perms/asset-permissions//assets/all/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/perms/api/asset_permission_relation.py | 3 +++ apps/perms/utils/user_perm.py | 9 +++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/perms/api/asset_permission_relation.py b/apps/perms/api/asset_permission_relation.py index f2bd3cf30..5a97192e0 100644 --- a/apps/perms/api/asset_permission_relation.py +++ b/apps/perms/api/asset_permission_relation.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- # +from functools import lru_cache + from rest_framework import generics from django.db.models import F from django.shortcuts import get_object_or_404 @@ -93,6 +95,7 @@ class AssetPermissionAllAssetListApi(generics.ListAPIView): filterset_fields = ("name", "address") search_fields = filterset_fields + @lru_cache(maxsize=2) def get_queryset(self): pk = self.kwargs.get("pk") assets = AssetPermissionPermAssetUtil(perm_ids=[pk]).get_all_assets() diff --git a/apps/perms/utils/user_perm.py b/apps/perms/utils/user_perm.py index d054e67ce..7c78c9149 100644 --- a/apps/perms/utils/user_perm.py +++ b/apps/perms/utils/user_perm.py @@ -24,12 +24,9 @@ class AssetPermissionPermAssetUtil: def get_perm_nodes_assets(self, flat=False): """ 获取所有授权节点下的资产 """ - node_ids = AssetPermission.nodes.through.objects \ - .filter(assetpermission_id__in=self.perm_ids) \ - .values_list('node_id', flat=True) \ - .distinct() - node_ids = list(node_ids) - nodes = PermNode.objects.filter(id__in=node_ids).only('id', 'key') + from assets.models import Node + nodes = Node.objects.prefetch_related('granted_by_permissions').filter( + granted_by_permissions__in=self.perm_ids).only('id', 'key') assets = PermNode.get_nodes_all_assets(*nodes) if flat: return assets.values_list('id', flat=True)