Kaynağa Gözat

feat: Convenience methods for NestedSet {get_parent, get_children} (#13579)

* feat: new properties `NestedSet.{parent, children}`

* fix: typo

* refactor: remove unused kwargs

* feat: method instead of property

* feat: use generator

* docs: fix docstring to match modified method

* refactor: add type hints

* refactor: type hints as string for compatibility with python <3.7

https://stackoverflow.com/questions/33533148/how-do-i-type-hint-a-method-with-the-type-of-the-enclosing-class

* refactor: import Iterator for type annotation

* refactor: Apply suggestions from code review

Co-authored-by: gavin <gavin18d@gmail.com>

Co-authored-by: gavin <gavin18d@gmail.com>
version-14
Raffael Meyer 3 yıl önce
committed by GitHub
ebeveyn
işleme
7c0078d8e4
Veri tabanında bu imza için bilinen anahtar bulunamadı GPG Anahtar Kimliği: 4AEE18F83AFDEB23
1 değiştirilmiş dosya ile 15 ekleme ve 0 silme
  1. +15
    -0
      frappe/utils/nestedset.py

+ 15
- 0
frappe/utils/nestedset.py Dosyayı Görüntüle

@@ -10,6 +10,8 @@
# 3. call update_nsm(doc_obj) in the on_upate method

# ------------------------------------------
from typing import Iterator

import frappe
from frappe import _
from frappe.model.document import Document
@@ -271,6 +273,19 @@ class NestedSet(Document):
def get_ancestors(self):
return get_ancestors_of(self.doctype, self.name)

def get_parent(self) -> "NestedSet":
"""Return the parent Document."""
parent_name = self.get(self.nsm_parent_field)
if parent_name:
return frappe.get_doc(self.doctype, parent_name)

def get_children(self) -> Iterator["NestedSet"]:
"""Return a generator that yields child Documents."""
child_names = frappe.get_list(self.doctype, filters={self.nsm_parent_field: self.name}, pluck="name")
for name in child_names:
yield frappe.get_doc(self.doctype, name)


def get_root_of(doctype):
"""Get root element of a DocType with a tree structure"""
result = frappe.db.sql("""select t1.name from `tab{0}` t1 where


Yükleniyor…
İptal
Kaydet