mirror of https://github.com/jumpserver/jumpserver
perf: 优化 get_all_node_keys 方法
parent
1679efe2c9
commit
a767c208b3
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue