Skip to content
Snippets Groups Projects
Commit 4192576c authored by Erwan Rouchet's avatar Erwan Rouchet
Browse files

Remove all paths on element deletion

parent 71a97593
No related branches found
No related tags found
1 merge request!91Remove all paths on Element deletion
......@@ -3,3 +3,6 @@ from django.apps import AppConfig
class ElementsConfig(AppConfig):
name = 'documents'
def ready(self):
from arkindex.documents import signals # noqa
......@@ -82,7 +82,7 @@ class ElementPath(models.Model):
An element can have multiple paths up to the top
"""
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
element = models.ForeignKey('documents.Element', related_name='paths', on_delete=models.DO_NOTHING)
element = models.ForeignKey('documents.Element', related_name='paths', on_delete=models.CASCADE)
path = ArrayField(
models.UUIDField(),
)
......
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from arkindex.documents.models import Element, ElementPath
@receiver(pre_delete, sender=Element)
def pre_delete_handler(sender, instance, *args, **kwargs):
assert isinstance(instance, Element)
# Run remove_child on all direct parents of the element
for parent in ElementPath.objects.filter(path__last=instance.id):
parent.remove_child(instance)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment