blob: 4bf4353cdfc1b553dea8bf3d24cd5bb552fb05f7 [file] [log] [blame]
Rucha Mahabal6f799d172021-07-21 23:19:47 +05301# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
2# MIT License. See license.txt
3
Chillar Anand915b3432021-09-02 16:44:59 +05304
Rucha Mahabal6f799d172021-07-21 23:19:47 +05305import frappe
6from frappe import _
7
Chillar Anand915b3432021-09-02 16:44:59 +05308
Rucha Mahabal6f799d172021-07-21 23:19:47 +05309@frappe.whitelist()
Rucha Mahabalf828d852021-09-01 23:07:26 +053010def get_all_nodes(method, company):
Ankush Menat494bd9e2022-03-28 18:52:46 +053011 """Recursively gets all data from nodes"""
Rucha Mahabal6f799d172021-07-21 23:19:47 +053012 method = frappe.get_attr(method)
13
Rucha Mahabal78f50a92021-07-25 20:28:01 +053014 if method not in frappe.whitelisted:
Ankush Menat494bd9e2022-03-28 18:52:46 +053015 frappe.throw(_("Not Permitted"), frappe.PermissionError)
Rucha Mahabal6f799d172021-07-21 23:19:47 +053016
Rucha Mahabalf828d852021-09-01 23:07:26 +053017 root_nodes = method(company=company)
18 result = []
19 nodes_to_expand = []
Rucha Mahabal6f799d172021-07-21 23:19:47 +053020
Rucha Mahabalf828d852021-09-01 23:07:26 +053021 for root in root_nodes:
22 data = method(root.id, company)
23 result.append(dict(parent=root.id, parent_name=root.name, data=data))
Ankush Menat494bd9e2022-03-28 18:52:46 +053024 nodes_to_expand.extend(
25 [{"id": d.get("id"), "name": d.get("name")} for d in data if d.get("expandable")]
26 )
Rucha Mahabal6f799d172021-07-21 23:19:47 +053027
28 while nodes_to_expand:
29 parent = nodes_to_expand.pop(0)
Ankush Menat494bd9e2022-03-28 18:52:46 +053030 data = method(parent.get("id"), company)
31 result.append(dict(parent=parent.get("id"), parent_name=parent.get("name"), data=data))
Rucha Mahabal6f799d172021-07-21 23:19:47 +053032 for d in data:
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 if d.get("expandable"):
34 nodes_to_expand.append({"id": d.get("id"), "name": d.get("name")})
Rucha Mahabal6f799d172021-07-21 23:19:47 +053035
Ankush Menat4551d7d2021-08-19 13:41:10 +053036 return result