brotherton-erpnext/erpnext/utilities/hierarchy_chart.py

37 lines
1015 B
Python
Raw Normal View History

# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
import frappe
from frappe import _
@frappe.whitelist()
def get_all_nodes(method, company):
2022-03-28 13:22:46 +00:00
"""Recursively gets all data from nodes"""
method = frappe.get_attr(method)
if method not in frappe.whitelisted:
2022-03-28 13:22:46 +00:00
frappe.throw(_("Not Permitted"), frappe.PermissionError)
root_nodes = method(company=company)
result = []
nodes_to_expand = []
for root in root_nodes:
data = method(root.id, company)
result.append(dict(parent=root.id, parent_name=root.name, data=data))
2022-03-28 13:22:46 +00:00
nodes_to_expand.extend(
[{"id": d.get("id"), "name": d.get("name")} for d in data if d.get("expandable")]
)
while nodes_to_expand:
parent = nodes_to_expand.pop(0)
2022-03-28 13:22:46 +00:00
data = method(parent.get("id"), company)
result.append(dict(parent=parent.get("id"), parent_name=parent.get("name"), data=data))
for d in data:
2022-03-28 13:22:46 +00:00
if d.get("expandable"):
nodes_to_expand.append({"id": d.get("id"), "name": d.get("name")})
return result