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 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe import _ |
| 7 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 9 | @frappe.whitelist() |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame] | 10 | def get_all_nodes(method, company): |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 11 | '''Recursively gets all data from nodes''' |
| 12 | method = frappe.get_attr(method) |
| 13 | |
Rucha Mahabal | 78f50a9 | 2021-07-25 20:28:01 +0530 | [diff] [blame] | 14 | if method not in frappe.whitelisted: |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 15 | frappe.throw(_('Not Permitted'), frappe.PermissionError) |
| 16 | |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame] | 17 | root_nodes = method(company=company) |
| 18 | result = [] |
| 19 | nodes_to_expand = [] |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 20 | |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame] | 21 | for root in root_nodes: |
| 22 | data = method(root.id, company) |
| 23 | result.append(dict(parent=root.id, parent_name=root.name, data=data)) |
| 24 | 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] | 25 | |
| 26 | while nodes_to_expand: |
| 27 | parent = nodes_to_expand.pop(0) |
| 28 | data = method(parent.get('id'), company) |
| 29 | result.append(dict(parent=parent.get('id'), parent_name=parent.get('name'), data=data)) |
| 30 | for d in data: |
| 31 | if d.get('expandable'): |
| 32 | nodes_to_expand.append({'id': d.get('id'), 'name': d.get('name')}) |
| 33 | |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 34 | return result |