Skip to content

Commit 86efc22

Browse files
Merge pull request #7 from OmenApps/duplicate_edge_check
Added argument to allow or disallow duplicate edges between 2 nodes
2 parents 03c6e9b + efa2c84 commit 86efc22

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

django_postgresql_dag/models.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@ def ordered_queryset_from_pks(self, pks):
7979

8080
def add_child(self, child, **kwargs):
8181
kwargs.update({"parent": self, "child": child})
82-
disable_check = kwargs.pop("disable_circular_check", False)
82+
83+
disable_circular_check = kwargs.pop("disable_circular_check", False)
84+
allow_duplicate_edges = kwargs.pop("allow_duplicate_edges", True)
85+
8386
cls = self.children.through(**kwargs)
84-
return cls.save(disable_circular_check=disable_check)
87+
return cls.save(disable_circular_check=disable_circular_check, allow_duplicate_edges=allow_duplicate_edges)
8588

8689
def remove_child(self, child, delete_node=False):
8790
"""Removes the edge connecting this node to child, and optionally deletes the child node as well"""
@@ -379,6 +382,11 @@ def circular_checker(parent, child):
379382
if child in parent.self_and_ancestors():
380383
raise ValidationError("The object is an ancestor.")
381384

385+
@staticmethod
386+
def duplicate_edge_checker(parent, child):
387+
if child in parent.self_and_descendants():
388+
raise ValidationError("The edge is a duplicate.")
389+
382390
return Node
383391

384392

@@ -475,6 +483,10 @@ class Meta:
475483
def save(self, *args, **kwargs):
476484
if not kwargs.pop("disable_circular_check", False):
477485
self.parent.__class__.circular_checker(self.parent, self.child)
486+
487+
if not kwargs.pop("allow_duplicate_edges", True):
488+
self.parent.__class__.duplicate_edge_checker(self.parent, self.child)
489+
478490
super().save(*args, **kwargs)
479491

480492
return Edge

docs/quickstart.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,24 @@ models.py
5757
def __str__(self):
5858
return self.name
5959

60+
61+
Optional arguments on the Edge model
62+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
64+
```disable_circular_check```: Defaults to False. If set to True,
65+
django-postgresql-dag will not check for circular paths. Essentially,
66+
the resulting graph may no longer be a DAG.
67+
68+
69+
70+
```allow_duplicate_edges```: Defaults to True. Determines whether two
71+
nodes are allowed to have more than one Edge directly connecting them.
72+
73+
6074
Add some Instances via the Shell (or in views, etc)
6175
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6276
::
6377

64-
~/myapp$ python manage.py shell
6578
>>> from myapp.models import NetworkNode, NetworkEdge
6679
6780
>>> root = NetworkNode.objects.create(name="root")
@@ -165,7 +178,6 @@ Work with the Graph in the Shell (or in views, etc)
165178
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
166179
::
167180

168-
~/myapp$ python manage.py shell
169181
>>> from myapp.models import NetworkNode, NetworkEdge
170182
171183
# Descendant methods which return a queryset

0 commit comments

Comments
 (0)