perf: 接口sql优化 /api/v1/perms/asset-permissions/<uuid:pk>/assets/all/

pull/10905/head
fangfang.dong 1 year ago committed by Bryan
parent dd846d4183
commit 785e4cc3e4

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

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

Loading…
Cancel
Save