feat: new operator - descendants of (inclusive)

Co-Authored-By: Faris Ansari <netchamp.faris@gmail.com>
This commit is contained in:
Ankush Menat 2023-05-29 15:36:38 +05:30
parent e5878b0c68
commit 3df13ca392
4 changed files with 23 additions and 11 deletions

View file

@ -23,6 +23,7 @@ NestedSetHierarchy = (
"descendants of",
"not ancestors of",
"not descendants of",
"descendants of (inclusive)",
)

View file

@ -730,18 +730,30 @@ class DatabaseQuery:
lft, rgt = frappe.db.get_value(ref_doctype, f.value, ["lft", "rgt"])
# Get descendants elements of a DocType with a tree structure
if f.operator.lower() in ("descendants of", "not descendants of"):
result = frappe.get_all(
ref_doctype, filters={"lft": [">", lft], "rgt": ["<", rgt]}, order_by="`lft` ASC"
if f.operator.lower() in (
"descendants of",
"not descendants of",
"descendants of (inclusive)",
):
nodes = frappe.get_all(
ref_doctype,
filters={"lft": [">", lft], "rgt": ["<", rgt]},
order_by="`lft` ASC",
pluck="name",
)
if f.operator.lower() == "descendants of (inclusive)":
nodes += [f.value]
else:
# Get ancestor elements of a DocType with a tree structure
result = frappe.get_all(
ref_doctype, filters={"lft": ["<", lft], "rgt": [">", rgt]}, order_by="`lft` DESC"
nodes = frappe.get_all(
ref_doctype,
filters={"lft": ["<", lft], "rgt": [">", rgt]},
order_by="`lft` DESC",
pluck="name",
)
fallback = "''"
value = [frappe.db.escape((cstr(v.name) or "").strip(), percent=False) for v in result]
value = [frappe.db.escape((cstr(v)).strip(), percent=False) for v in nodes]
if len(value):
value = f"({', '.join(value)})"
else:

View file

@ -30,6 +30,7 @@ frappe.ui.Filter = class {
this.nested_set_conditions = [
["descendants of", __("Descendants Of")],
["descendants of (inclusive)", __("Descendants Of (inclusive)")],
["not descendants of", __("Not Descendants Of")],
["ancestors of", __("Ancestors Of")],
["not ancestors of", __("Not Ancestors Of")],
@ -524,6 +525,7 @@ frappe.ui.filter_utils = {
"=",
"!=",
"descendants of",
"descendants of (inclusive)",
"ancestors of",
"not descendants of",
"not ancestors of",

View file

@ -1766,6 +1766,7 @@ def get_filter(doctype: str, f: dict | list | tuple, filters_config=None) -> "fr
"fieldtype":
}
"""
from frappe.database.utils import NestedSetHierarchy
from frappe.model import child_table_fields, default_fields, optional_fields
if isinstance(f, dict):
@ -1805,14 +1806,10 @@ def get_filter(doctype: str, f: dict | list | tuple, filters_config=None) -> "fr
"not in",
"is",
"between",
"descendants of",
"ancestors of",
"not descendants of",
"not ancestors of",
"timespan",
"previous",
"next",
)
) + NestedSetHierarchy
if filters_config:
additional_operators = []