blob: 0e7f81fb8ee8ef9798ae9f75f7321cf665bb15fb [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
Chillar Anand915b3432021-09-02 16:44:59 +05305
Rucha Mahabal6f799d172021-07-21 23:19:47 +05306import frappe
7from frappe import _
8
Chillar Anand915b3432021-09-02 16:44:59 +05309
Rucha Mahabal6f799d172021-07-21 23:19:47 +053010@frappe.whitelist()
Rucha Mahabalf828d852021-09-01 23:07:26 +053011def get_all_nodes(method, company):
Rucha Mahabal6f799d172021-07-21 23:19:47 +053012 '''Recursively gets all data from nodes'''
13 method = frappe.get_attr(method)
14
Rucha Mahabal78f50a92021-07-25 20:28:01 +053015 if method not in frappe.whitelisted:
Rucha Mahabal6f799d172021-07-21 23:19:47 +053016 frappe.throw(_('Not Permitted'), frappe.PermissionError)
17
Rucha Mahabalf828d852021-09-01 23:07:26 +053018 root_nodes = method(company=company)
19 result = []
20 nodes_to_expand = []
Rucha Mahabal6f799d172021-07-21 23:19:47 +053021
Rucha Mahabalf828d852021-09-01 23:07:26 +053022 for root in root_nodes:
23 data = method(root.id, company)
24 result.append(dict(parent=root.id, parent_name=root.name, data=data))
25 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 +053026
27 while nodes_to_expand:
28 parent = nodes_to_expand.pop(0)
29 data = method(parent.get('id'), company)
30 result.append(dict(parent=parent.get('id'), parent_name=parent.get('name'), data=data))
31 for d in data:
32 if d.get('expandable'):
33 nodes_to_expand.append({'id': d.get('id'), 'name': d.get('name')})
34
Ankush Menat4551d7d2021-08-19 13:41:10 +053035 return result