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 |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 6 | import frappe |
| 7 | from frappe import _ |
| 8 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 10 | @frappe.whitelist() |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame] | 11 | def get_all_nodes(method, company): |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 12 | '''Recursively gets all data from nodes''' |
| 13 | method = frappe.get_attr(method) |
| 14 | |
Rucha Mahabal | 78f50a9 | 2021-07-25 20:28:01 +0530 | [diff] [blame] | 15 | if method not in frappe.whitelisted: |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 16 | frappe.throw(_('Not Permitted'), frappe.PermissionError) |
| 17 | |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame] | 18 | root_nodes = method(company=company) |
| 19 | result = [] |
| 20 | nodes_to_expand = [] |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 21 | |
Rucha Mahabal | f828d85 | 2021-09-01 23:07:26 +0530 | [diff] [blame] | 22 | 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 Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 26 | |
| 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 Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 35 | return result |