seitime-frappe/frappe/desk/treeview.py
Aarol D'Souza 68727cbd61
fix: add type hints to whitelisted methods 4 (#37204)
* fix(utils): add type hints to whitelisted methods

* fix(desktop): add type hints to whitelisted methods

* fix(listview): add type hints to whitelisted methods

* fix(access_log): add type hints to whitelisted methods

* fix(setup_wizard): add type hints to whitelisted methods

* fix(notification_settings): add type hints to whitelisted methods

* fix(calendar): add type hints to whitelisted methods

* fix(notifications): add type hints to whitelisted methods

* fix(query_report): add type hints to whitelisted methods

* fix(load): add type hints to whitelisted methods

* fix(save): add type hints to whitelisted methods

* fix(user): add type hints to whitelisted methods

* fix: correct Document import

* fix(list_view_settings): add type hints to whitelisted methods

* fix(reportview): add type hints to whitelisted methods

* fix(treeview): add type hints to whitelisted methods

* fix(linked_with): add type hints to whitelisted methods

* fix(bulk_update): add type hints to whitelisted methods

* fix(assign_to): add type hints to whitelisted methods

* fix(workspace): add type hints to whitelisted methods

* fix(kanban_board): add type hints to whitelisted methods

* fix(event): add type hints to whitelisted methods

* fix(email): add type hints to whitelisted methods

* fix(exporter): add type hints to whitelisted methods

* fix(permission_manager): add type hints to whitelisted methods

* fix(dashboard_chart): add type hints to whitelisted methods

* fix(number_card): add type hints to whitelisted methods

* fix(tag): add type hints to whitelisted methods

* fix: add hook to force type hints on all whitelisted endpoints

* fix: target_doc can be dict/json string

* fix: doc can be dict/json string

* fix(tests): add type hints to whitelisted methods in test

* fix: tree method is optional

* test: Fix test api types

* chore: drop dead code

* fix: document can be int

* fix: Number card input can be document

As utils in some other API calls

* fix: Always use session user

The only usage of this API that makes sense.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Ankush Menat <ankush@frappe.io>
2026-02-20 14:53:08 +05:30

89 lines
2.4 KiB
Python

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import frappe
from frappe import _
from frappe.query_builder import Field, functions
@frappe.whitelist()
def get_all_nodes(doctype: str, label: str, parent: str, tree_method: str | None, **filters):
"""Recursively gets all data from tree nodes"""
filters.pop("cmd", None)
filters.pop("data", None)
tree_method = frappe.get_attr(tree_method)
frappe.is_whitelisted(tree_method)
data = tree_method(doctype, parent, **filters)
out = [dict(parent=label, data=data)]
filters.pop("is_root", None)
to_check = [d.get("value") for d in data if d.get("expandable")]
while to_check:
parent = to_check.pop()
data = tree_method(doctype, parent, is_root=False, **filters)
out.append(dict(parent=parent, data=data))
for d in data:
if d.get("expandable"):
to_check.append(d.get("value"))
return out
@frappe.whitelist()
def get_children(doctype: str, parent: str = "", include_disabled: str | int | bool = False, **filters):
if isinstance(include_disabled, str):
include_disabled = frappe.sbool(include_disabled)
return _get_children(doctype, parent, include_disabled=include_disabled)
def _get_children(doctype, parent="", ignore_permissions=False, include_disabled=False):
meta = frappe.get_meta(doctype)
parent_field = meta.get("nsm_parent_field") or "parent_" + frappe.scrub(doctype)
qb = (
frappe.qb.from_(doctype)
.select(
Field("name").as_("value"),
Field(meta.get("title_field") or "name").as_("title"),
Field("is_group").as_("expandable"),
)
.where(functions.IfNull(Field(parent_field), "").eq(parent))
.where(Field("docstatus") < 2)
)
if frappe.db.has_column(doctype, "disabled") and not include_disabled:
# used 0 instead of `false` since type of check in postgres is smallint
qb = qb.where(Field("disabled").eq(0))
# Order by name and execute
return qb.orderby("name").run(as_dict=True)
@frappe.whitelist()
def add_node():
args = make_tree_args(**frappe.form_dict)
doc = frappe.get_doc(args)
doc.save()
def make_tree_args(**kwarg):
kwarg.pop("cmd", None)
doctype = kwarg["doctype"]
parent_field = "parent_" + frappe.scrub(doctype)
if kwarg["is_root"] == "false":
kwarg["is_root"] = False
if kwarg["is_root"] == "true":
kwarg["is_root"] = True
parent = kwarg.get("parent") or kwarg.get(parent_field)
if doctype != parent:
kwarg.update({parent_field: parent})
return frappe._dict(kwarg)