perf: use search_asset or search_node key

This commit is contained in:
Bai
2025-12-27 22:22:47 +08:00
parent bc9c9c30cd
commit 084635b098

View File

@@ -32,19 +32,21 @@ class UserPermNodeChildrenAsTreeApi(SelfOrPKUserMixin, SerializeToTreeNodeMixin,
node_key = request.query_params.get('key')
asset_category = request.query_params.get('category')
asset_type = request.query_params.get('type')
search_node = None
search_asset = None
# test
if search:
search_list = search.split()
for s in search_list:
node = s.split('node:')
if len(node) == 2:
search = node[1]
search_node = node[1]
with_assets = False
break
asset = s.split('asset:')
if len(asset) == 2:
search = asset[1]
search_asset = asset[1]
continue
asset_category = s.split('category:')
if len(asset_category) == 2:
@@ -56,33 +58,31 @@ class UserPermNodeChildrenAsTreeApi(SelfOrPKUserMixin, SerializeToTreeNodeMixin,
continue
if node_key:
# 一定是 with_assets
with_assets = True
search = None
asset_category = None
asset_type = None
if with_assets:
if node_key:
return self.expand_tree_node_with_assets(
node_key, asset_category, asset_type
)
elif search:
elif search_asset:
# search assets
return self.search_user_perm_tree_with_assets(
search, asset_category, asset_type
search_asset, asset_category, asset_type
)
else:
return self.init_user_perm_tree_with_assets(
asset_category, asset_type
)
else:
if search:
if search_node:
# search nodes
return self.search_user_perm_tree(search)
return self.search_user_perm_tree(search_node, asset_category, asset_type)
else:
return self.init_user_perm_tree()
return self.init_user_perm_tree(asset_category, asset_type)
def init_user_perm_tree(self):
def init_user_perm_tree(self, asset_category=None, asset_type=None):
''' 初始化用户权限树 - 不包含资产
前端搜索
@@ -104,7 +104,10 @@ class UserPermNodeChildrenAsTreeApi(SelfOrPKUserMixin, SerializeToTreeNodeMixin,
nodes = []
for org in orgs:
tree = UserPermTree(user=self.user, org=org)
tree = UserPermTree(
user=self.user, asset_category=asset_category,
asset_type=asset_type, org=org
)
_nodes = tree.get_nodes()
nodes.extend(_nodes)
nodes = self.serialize_nodes(
@@ -114,7 +117,7 @@ class UserPermNodeChildrenAsTreeApi(SelfOrPKUserMixin, SerializeToTreeNodeMixin,
data = self.add_favorites_and_ungrouped_node(data, with_assets=False)
return Response(data=data)
def search_user_perm_tree(self, search):
def search_user_perm_tree(self, search, asset_category=None, asset_type=None):
''' 搜索用户授权树 - 不包含资产
全局组织: 返回所有匹配节点以及祖先节点,不返回匹配节点的子孙节点,不返回资产,展开所有祖先节点,不展开匹配节点
实体组织: 同上
@@ -127,7 +130,10 @@ class UserPermNodeChildrenAsTreeApi(SelfOrPKUserMixin, SerializeToTreeNodeMixin,
search_nodes = []
nodes_ancestors = []
for org in orgs:
tree = UserPermTree(user=self.user, org=org)
tree = UserPermTree(
user=self.user, asset_category=asset_category,
asset_type=asset_type, org=org
)
_search_nodes = tree.search_nodes(search, only_top_level=True)
# tree.remove_nodes_descendants(_search_nodes)
_nodes_ancestors = tree.get_nodes_ancestors(_search_nodes)