From c18ca54538d9cbab2f38cb9f228d622a8174eacf Mon Sep 17 00:00:00 2001 From: Omen Apps Date: Sun, 22 Nov 2020 11:31:18 -0500 Subject: [PATCH] Implemented/added several more graph methods --- django_postgresql_dag/models.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/django_postgresql_dag/models.py b/django_postgresql_dag/models.py index ed7d8ea..67a6d2e 100644 --- a/django_postgresql_dag/models.py +++ b/django_postgresql_dag/models.py @@ -239,26 +239,36 @@ def is_island(self): """ return bool(not self.children.exists() and not self.parents.exists()) - def is_descendant_of(self, target): - # ToDo: Implement - pass + def is_ancestor_of(self, ending_node, **kwargs): + try: + return len(self.path_raw(ending_node, **kwargs)) >= 1 + except NodeNotReachableException: + return False + + def is_descendant_of(self, ending_node, **kwargs): + return ( + not self.is_ancestor_of(ending_node, **kwargs) + and len(self.path_raw(ending_node, directional=False, **kwargs)) >= 1 + ) - def is_ancestor_of(self, target): - # ToDo: Implement - pass + def is_sibling_of(self, ending_node): + return ending_node in self.siblings() - def is_sibling_of(self, target): - # ToDo: Implement - pass + def is_partner_of(self, ending_node): + return ending_node in self.partners() def node_depth(self): # Depth from furthest root # ToDo: Implement pass - def entire_graph(self): + def connected_graph_raw(self, **kwargs): # Gets all nodes connected in any way to this node - pass + return ConnectedGraphQuery(instance=self, **kwargs).raw_queryset() + + def connected_graph(self, **kwargs): + pks = [item.pk for item in self.connected_graph_raw(**kwargs)] + return self.ordered_queryset_from_pks(pks) def descendants_tree(self): """ @@ -364,6 +374,7 @@ def circular_checker(parent, child): return Node + class EdgeManager(models.Manager): def from_nodes_queryset(self, nodes_queryset): """Provided a queryset of nodes, returns all edges where a parent and child