perf: 优化 get_all_node_keys 方法

pull/9212/head
Bai 2 years ago committed by Jiangjie.Bai
parent 1679efe2c9
commit a767c208b3

@ -63,26 +63,28 @@ class NodesRelationMixin:
nodes = Node.objects.filter(id=Node.org_root().id) nodes = Node.objects.filter(id=Node.org_root().id)
return nodes return nodes
def get_all_nodes(self, flat=False, only_keys=False): def get_all_nodes(self, flat=False):
from ..node import Node from ..node import Node
node_keys = set() node_keys = self.get_all_node_keys()
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
nodes = Node.objects.filter(key__in=node_keys).distinct() nodes = Node.objects.filter(key__in=node_keys).distinct()
if not flat: if not flat:
return nodes return nodes
node_ids = set(nodes.values_list('id', flat=True)) node_ids = set(nodes.values_list('id', flat=True))
return node_ids 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 @classmethod
def get_all_nodes_for_assets(cls, assets): def get_all_nodes_for_assets(cls, assets):
from ..node import Node from ..node import Node
node_keys = set() node_keys = set()
for asset in assets: 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) node_keys.update(asset_node_keys)
nodes = Node.objects.filter(key__in=node_keys) nodes = Node.objects.filter(key__in=node_keys)
return nodes return nodes

@ -178,9 +178,7 @@ class FamilyMixin:
return parent_keys return parent_keys
def get_ancestor_keys(self, with_self=False): def get_ancestor_keys(self, with_self=False):
return self.get_node_ancestor_keys( return self.get_node_ancestor_keys(self.key, with_self=with_self)
self.key, with_self=with_self
)
@property @property
def ancestors(self): def ancestors(self):

Loading…
Cancel
Save