@@ -605,48 +605,52 @@ def ancestors_tree(self):
605
605
tree [parent ] = parent .ancestors_tree ()
606
606
return tree
607
607
608
- def _get_roots (self , ancestors_tree ):
608
+ def _roots (self , ancestors_tree ):
609
609
"""
610
610
Works on objects: no queries
611
611
"""
612
612
if not ancestors_tree :
613
613
return set ([self ])
614
614
roots = set ()
615
615
for ancestor in ancestors_tree :
616
- roots .update (ancestor ._get_roots (ancestors_tree [ancestor ]))
616
+ roots .update (ancestor ._roots (ancestors_tree [ancestor ]))
617
617
return roots
618
618
619
- def get_roots (self ):
619
+ def roots (self ):
620
620
"""
621
621
Returns roots nodes, if any
622
622
# ToDo: Modify to use CTE
623
623
"""
624
624
ancestors_tree = self .ancestors_tree ()
625
625
roots = set ()
626
626
for ancestor in ancestors_tree :
627
- roots .update (ancestor ._get_roots (ancestors_tree [ancestor ]))
627
+ roots .update (ancestor ._roots (ancestors_tree [ancestor ]))
628
+ if len (roots ) < 1 :
629
+ roots .add (self )
628
630
return roots
629
631
630
- def _get_leaves (self , descendants_tree ):
632
+ def _leaves (self , descendants_tree ):
631
633
"""
632
634
Works on objects: no queries
633
635
"""
634
636
if not descendants_tree :
635
637
return set ([self ])
636
638
leaves = set ()
637
639
for descendant in descendants_tree :
638
- leaves .update (descendant ._get_leaves (descendants_tree [descendant ]))
640
+ leaves .update (descendant ._leaves (descendants_tree [descendant ]))
639
641
return leaves
640
642
641
- def get_leaves (self ):
643
+ def leaves (self ):
642
644
"""
643
645
Returns leaves nodes, if any
644
646
# ToDo: Modify to use CTE
645
647
"""
646
648
descendants_tree = self .descendants_tree ()
647
649
leaves = set ()
648
650
for descendant in descendants_tree :
649
- leaves .update (descendant ._get_leaves (descendants_tree [descendant ]))
651
+ leaves .update (descendant ._leaves (descendants_tree [descendant ]))
652
+ if len (leaves ) < 1 :
653
+ leaves .add (self )
650
654
return leaves
651
655
652
656
@staticmethod
0 commit comments