Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 1 | # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # MIT License. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import frappe |
| 6 | from frappe import _ |
| 7 | |
| 8 | @frappe.whitelist() |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame^] | 9 | def get_all_nodes(method, company): |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 10 | '''Recursively gets all data from nodes''' |
| 11 | method = frappe.get_attr(method) |
| 12 | |
Rucha Mahabal | 78f50a9 | 2021-07-25 20:28:01 +0530 | [diff] [blame] | 13 | if method not in frappe.whitelisted: |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 14 | frappe.throw(_('Not Permitted'), frappe.PermissionError) |
| 15 | |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame^] | 16 | root_nodes = method(company=company) |
| 17 | result = [] |
| 18 | nodes_to_expand = [] |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 19 | |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame^] | 20 | 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 Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 24 | |
| 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 Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 33 | return result |