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): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 11 | """Recursively gets all data from nodes""" |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 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: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 15 | frappe.throw(_("Not Permitted"), frappe.PermissionError) |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 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)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 24 | nodes_to_expand.extend( |
| 25 | [{"id": d.get("id"), "name": d.get("name")} for d in data if d.get("expandable")] |
| 26 | ) |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 27 | |
| 28 | while nodes_to_expand: |
| 29 | parent = nodes_to_expand.pop(0) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | data = method(parent.get("id"), company) |
| 31 | result.append(dict(parent=parent.get("id"), parent_name=parent.get("name"), data=data)) |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 32 | for d in data: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 33 | if d.get("expandable"): |
| 34 | nodes_to_expand.append({"id": d.get("id"), "name": d.get("name")}) |
Rucha Mahabal | 6f799d17 | 2021-07-21 23:19:47 +0530 | [diff] [blame] | 35 | |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 36 | return result |