perf: UserPermedAssetTreeAPI about get_special_nodes_if_needed

This commit is contained in:
Bai
2025-12-28 20:38:13 +08:00
parent c4d65d3a48
commit 47e7fa2cc0
3 changed files with 62 additions and 55 deletions

View File

@@ -121,6 +121,29 @@ class AbstractAssetTreeAPI(SerializeToTreeNodeMixin, generics.ListAPIView):
asset_type = self.get_query_value(self.query_asset_type_key)
with_asset_amount = True
expand_node_key = self.get_query_value(self.query_expand_node_key)
search_node = self.get_query_value(self.query_search_node_key)
search_asset = self.get_query_value(self.query_search_asset_key)
if self.render_tree_type.is_asset_tree:
if not search_asset:
# 兼容 search 为搜索资产
search = self.get_query_value(self.query_search_key) or ''
sep = self.query_search_key_value_sep
if sep not in search:
search_asset = search
data = self._list(
expand_node_key=expand_node_key,
search_node=search_node, search_asset=search_asset,
asset_category=asset_category, asset_type=asset_type,
with_asset_amount=with_asset_amount
)
return Response(data=data)
@timeit
def _list(self, expand_node_key=None, search_node=None, search_asset=None,
asset_category=None, asset_type=None, with_asset_amount=True):
if self.render_tree_type.is_node_tree:
data = self.render_node_tree(
asset_category=asset_category, asset_type=asset_type,
@@ -128,6 +151,8 @@ class AbstractAssetTreeAPI(SerializeToTreeNodeMixin, generics.ListAPIView):
)
elif self.render_tree_type.is_asset_tree:
data = self.render_asset_tree(
expand_node_key=expand_node_key,
search_node=search_node, search_asset=search_asset,
asset_category=asset_category, asset_type=asset_type,
with_asset_amount=with_asset_amount
)
@@ -135,7 +160,7 @@ class AbstractAssetTreeAPI(SerializeToTreeNodeMixin, generics.ListAPIView):
raise APIException(
f'Invalid tree type: {self.render_tree_type}'
)
return Response(data=data)
return data
@timeit
def render_node_tree(self, asset_category=None, asset_type=None, with_asset_amount=True):
@@ -149,35 +174,14 @@ class AbstractAssetTreeAPI(SerializeToTreeNodeMixin, generics.ListAPIView):
with_asset_amount=with_asset_amount
)
return data
@timeit
def render_asset_tree(self, asset_category=None, asset_type=None, with_asset_amount=True):
# 渲染资产树 #
expand_node_key = self.get_query_value(self.query_expand_node_key)
search_node = self.get_query_value(self.query_search_node_key)
search_asset = self.get_query_value(self.query_search_asset_key)
data = self._render_asset_tree(
expand_node_key=expand_node_key,
search_node=search_node, search_asset=search_asset,
asset_category=asset_category, asset_type=asset_type,
with_asset_amount=with_asset_amount
)
return data
@timeit
def _render_asset_tree(self, expand_node_key=None, search_node=None, search_asset=None,
def render_asset_tree(self, expand_node_key=None, search_node=None, search_asset=None,
asset_category=None, asset_type=None, with_asset_amount=True):
# 渲染资产树内部方法,支持子类重载 #
# 此方法包含渲染资产树的所有参数
# 资产树支持初始化、搜索节点、搜索资产和展开节点等动作
if not search_asset:
# 兼容 search 为搜索资产
search = self.get_query_value(self.query_search_key) or ''
sep = self.query_search_key_value_sep
if sep not in search:
search_asset = search
if expand_node_key:
data = self.expand_asset_tree_node(
node_key=expand_node_key,

View File

@@ -19,6 +19,7 @@ __all__ = [
class UserPermedAssetTreeAPI(SelfOrPKUserMixin, AbstractAssetTreeAPI):
search_special_node_asset_limit_max = 50
def get_tree_user(self):
return self.user
@@ -32,31 +33,23 @@ class UserPermedAssetTreeAPI(SelfOrPKUserMixin, AbstractAssetTreeAPI):
tree = UserPermAssetTree(user=self.user, **kwargs)
return tree
def _render_asset_tree(self, **kwargs):
# 重写父类方法,返回特殊节点
# 特殊节点如:收藏夹、未分组节点
data = super()._render_asset_tree(**kwargs)
expand_node_key = kwargs.pop('expand_node_key', None)
if expand_node_key:
# 展开其他节点时,不返回特殊节点
# 特殊节点不允许异步展开
return data
special_nodes = self.get_special_nodes(**kwargs)
data = special_nodes + data
return data
def render_node_tree(self, **kwargs):
# 重写父类方法,返回特殊节点
data = super().render_node_tree(**kwargs)
special_nodes = self.get_special_nodes(**kwargs)
def _list(self, **kwargs):
# 重写父类方法,返回用户授权的组织资产树节点和资产
data = super()._list(**kwargs)
special_nodes = self.get_special_nodes_if_needed(**kwargs)
data = special_nodes + data
return data
@timeit
def get_special_nodes(self, search_asset=None, search_node=None,
asset_category=None, asset_type=None, with_asset_amount=True):
def get_special_nodes_if_needed(self, expand_node_key=None, search_asset=None, search_node=None,
asset_category=None, asset_type=None, with_asset_amount=True):
# 获取特殊节点数据
# 特殊节点如:收藏夹、未分组节点
if expand_node_key:
# 展开其他节点时,不返回特殊节点
# 特殊节点不允许异步展开
return []
# 默认不包含资产
with_assets = False
@@ -71,12 +64,12 @@ class UserPermedAssetTreeAPI(SelfOrPKUserMixin, AbstractAssetTreeAPI):
f_node, f_assets = self.get_favorite_node(
search_asset=search_asset, search_node=search_node,
asset_category=asset_category, asset_type=asset_type,
with_assets=with_assets
with_assets=with_assets,
)
u_node, u_assets = self.get_ungrouped_node_if_need(
search_asset=search_asset, search_node=search_node,
asset_category=asset_category, asset_type=asset_type,
with_assets=with_assets
with_assets=with_assets,
)
nodes = [n for n in [f_node, u_node] if n]
@@ -93,7 +86,7 @@ class UserPermedAssetTreeAPI(SelfOrPKUserMixin, AbstractAssetTreeAPI):
serialized_assets = self.serialize_assets(assets)
else:
serialized_assets = []
data = serialized_nodes + serialized_assets
return data
@@ -105,7 +98,7 @@ class UserPermedAssetTreeAPI(SelfOrPKUserMixin, AbstractAssetTreeAPI):
user=self.tree_user,
search_asset=search_asset,
asset_category=asset_category,
asset_type=asset_type
asset_type=asset_type,
)
assets_amount = assets.count()
node = UserPermAssetTreeNode.favorite(
@@ -115,14 +108,19 @@ class UserPermedAssetTreeAPI(SelfOrPKUserMixin, AbstractAssetTreeAPI):
if search_node and not node.match(search_node):
return None, []
if assets_amount == 0:
# 没有资产时不返回收藏夹节点
return None, []
if not with_assets:
return node, []
if assets_amount == 0:
return node, []
assets = assets.values(*AssetTreeNodeAsset.model_values)
if search_asset:
assets = assets[:self.search_special_node_asset_limit_max]
assets_attrs = list(assets.values(*AssetTreeNodeAsset.model_values))
assets_attrs = list(assets)
assets = node.init_assets(assets_attrs)
return node, assets
@@ -143,7 +141,7 @@ class UserPermedAssetTreeAPI(SelfOrPKUserMixin, AbstractAssetTreeAPI):
assets = util.get_ungrouped_assets(
search_asset=search_asset,
asset_category=asset_category,
asset_type=asset_type
asset_type=asset_type,
)
assets_amount = assets.count()
node = UserPermAssetTreeNode.ungrouped(
@@ -154,11 +152,16 @@ class UserPermedAssetTreeAPI(SelfOrPKUserMixin, AbstractAssetTreeAPI):
return None, []
if assets_amount == 0:
return node, []
# 没有资产时不返回未分组节点
return None, []
if not with_assets:
return node, []
assets_attrs = list(assets.values(*AssetTreeNodeAsset.model_values))
assets = assets.values(*AssetTreeNodeAsset.model_values)
if search_asset:
assets = assets[:self.search_special_node_asset_limit_max]
assets_attrs = list(assets)
assets = node.init_assets(assets_attrs)
return node, assets

View File

@@ -163,7 +163,7 @@ class UserPermedAssetUtil(object):
asset_ids=asset_ids,
search_asset=search_asset,
asset_category=asset_category,
asset_type=asset_type
asset_type=asset_type,
)
return assets
@@ -173,7 +173,7 @@ class UserPermedAssetUtil(object):
asset_ids=asset_ids,
search_asset=search_asset,
asset_category=asset_category,
asset_type=asset_type
asset_type=asset_type,
)
return assets
@@ -184,7 +184,7 @@ class UserPermedAssetUtil(object):
search_asset=search_asset,
asset_category=asset_category,
asset_type=asset_type
)
)
assets = Asset.objects.filter(q).valid()
return assets