2021-07-21 17:49:47 +00:00
|
|
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
|
|
|
# MIT License. See license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-07-21 17:49:47 +00:00
|
|
|
@frappe.whitelist()
|
2021-09-01 17:37:26 +00:00
|
|
|
def get_all_nodes(method, company):
|
2021-07-21 17:49:47 +00:00
|
|
|
"""Recursively gets all data from nodes"""
|
|
|
|
method = frappe.get_attr(method)
|
|
|
|
|
2021-07-25 14:58:01 +00:00
|
|
|
if method not in frappe.whitelisted:
|
2021-07-21 17:49:47 +00:00
|
|
|
frappe.throw(_("Not Permitted"), frappe.PermissionError)
|
|
|
|
|
2021-09-01 17:37:26 +00:00
|
|
|
root_nodes = method(company=company)
|
|
|
|
result = []
|
|
|
|
nodes_to_expand = []
|
2021-07-21 17:49:47 +00:00
|
|
|
|
2021-09-01 17:37:26 +00:00
|
|
|
for root in root_nodes:
|
|
|
|
data = method(root.id, company)
|
|
|
|
result.append(dict(parent=root.id, parent_name=root.name, data=data))
|
|
|
|
nodes_to_expand.extend(
|
|
|
|
[{"id": d.get("id"), "name": d.get("name")} for d in data if d.get("expandable")]
|
|
|
|
)
|
2021-07-21 17:49:47 +00:00
|
|
|
|
|
|
|
while nodes_to_expand:
|
|
|
|
parent = nodes_to_expand.pop(0)
|
|
|
|
data = method(parent.get("id"), company)
|
|
|
|
result.append(dict(parent=parent.get("id"), parent_name=parent.get("name"), data=data))
|
|
|
|
for d in data:
|
|
|
|
if d.get("expandable"):
|
|
|
|
nodes_to_expand.append({"id": d.get("id"), "name": d.get("name")})
|
|
|
|
|
|
|
|
return result
|