blob: cc506e5cfda5c43b38819bce44a5fb89a51efad1 [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
4from __future__ import unicode_literals
5import frappe
6from frappe import _
7
8@frappe.whitelist()
Rucha Mahabalf828d852021-09-01 23:07:26 +05309def get_all_nodes(method, company):
Rucha Mahabal6f799d172021-07-21 23:19:47 +053010 '''Recursively gets all data from nodes'''
11 method = frappe.get_attr(method)
12
Rucha Mahabal78f50a92021-07-25 20:28:01 +053013 if method not in frappe.whitelisted:
Rucha Mahabal6f799d172021-07-21 23:19:47 +053014 frappe.throw(_('Not Permitted'), frappe.PermissionError)
15
Rucha Mahabalf828d852021-09-01 23:07:26 +053016 root_nodes = method(company=company)
17 result = []
18 nodes_to_expand = []
Rucha Mahabal6f799d172021-07-21 23:19:47 +053019
Rucha Mahabalf828d852021-09-01 23:07:26 +053020 for root in root_nodes:
21 data = method(root.id, company)
22 result.append(dict(parent=root.id, parent_name=root.name, data=data))
23 nodes_to_expand.extend([{'id': d.get('id'), 'name': d.get('name')} for d in data if d.get('expandable')])
Rucha Mahabal6f799d172021-07-21 23:19:47 +053024
25 while nodes_to_expand:
26 parent = nodes_to_expand.pop(0)
27 data = method(parent.get('id'), company)
28 result.append(dict(parent=parent.get('id'), parent_name=parent.get('name'), data=data))
29 for d in data:
30 if d.get('expandable'):
31 nodes_to_expand.append({'id': d.get('id'), 'name': d.get('name')})
32
Ankush Menat4551d7d2021-08-19 13:41:10 +053033 return result