From a767c208b39ecce1f2e106bdec069646f61afacb Mon Sep 17 00:00:00 2001 From: Bai Date: Fri, 9 Dec 2022 13:13:02 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20get=5Fall=5Fnode?= =?UTF-8?q?=5Fkeys=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/asset/common.py | 18 ++++++++++-------- apps/assets/models/node.py | 4 +--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/assets/models/asset/common.py b/apps/assets/models/asset/common.py index c191dafe4..e7d3ae8db 100644 --- a/apps/assets/models/asset/common.py +++ b/apps/assets/models/asset/common.py @@ -63,26 +63,28 @@ class NodesRelationMixin: nodes = Node.objects.filter(id=Node.org_root().id) return nodes - def get_all_nodes(self, flat=False, only_keys=False): + def get_all_nodes(self, flat=False): from ..node import Node - node_keys = set() - for node in self.get_nodes(): - ancestor_keys = node.get_ancestor_keys(with_self=True) - node_keys.update(ancestor_keys) - if only_keys: - return node_keys + node_keys = self.get_all_node_keys() nodes = Node.objects.filter(key__in=node_keys).distinct() if not flat: return nodes node_ids = set(nodes.values_list('id', flat=True)) return node_ids + def get_all_node_keys(self): + node_keys = set() + for node in self.get_nodes(): + ancestor_keys = node.get_ancestor_keys(with_self=True) + node_keys.update(ancestor_keys) + return node_keys + @classmethod def get_all_nodes_for_assets(cls, assets): from ..node import Node node_keys = set() for asset in assets: - asset_node_keys = asset.get_all_nodes(only_keys=True) + asset_node_keys = asset.get_all_node_keys() node_keys.update(asset_node_keys) nodes = Node.objects.filter(key__in=node_keys) return nodes diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index 54bdecf61..960d858d5 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -178,9 +178,7 @@ class FamilyMixin: return parent_keys def get_ancestor_keys(self, with_self=False): - return self.get_node_ancestor_keys( - self.key, with_self=with_self - ) + return self.get_node_ancestor_keys(self.key, with_self=with_self) @property def ancestors(self):