From b57fcc9b25036c5c678f75b152b31486f328e97e Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Wed, 22 Apr 2020 23:46:18 +0200 Subject: [PATCH] =?UTF-8?q?Vyr=C3=A1b=C3=ADme=20TNLData=20podle=20filtru?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/views/views_all.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/seminar/views/views_all.py b/seminar/views/views_all.py index 48708b26..32956c35 100644 --- a/seminar/views/views_all.py +++ b/seminar/views/views_all.py @@ -103,8 +103,30 @@ class TNLData(object): outitem = cls.from_treenode(ch,out,idx) out.children.append(outitem) return out + + @classmethod + def filter_treenode(cls, treenode, predicate): + tnll = cls._filter_treenode_recursive(treenode, predicate) # TreeNodeList List :-) + # Teď vyrobíme virtuální TNL, který nemá obsah, ale má za potomky všecny nalezené TNLData + result = cls(None) + for idx, tnl in enumerate(tnll): + result.children.append(tnl) + tnl.parent = result + tnl.index = idx + return result - + @classmethod + def _filter_treenode_recursive(cls, treenode, predicate): + if predicate(treenode): + return [cls.from_treenode(treenode)] + else: + found = [] + for tn in all_children(treenode): + result = cls.filter_treenode(tn, predicate) + # Result by v tuhle chvíli měl být seznam TNLDat odpovídající treenodům, jež matchnuly predikát. + for tnl in result: + found.append(tnl) + return found class TreeNodeView(generic.DetailView): model = s.TreeNode