🐛(back) keep info if document has deleted children
With the soft delete feature, relying on the is_leaf method from the treebeard is not accurate anymore. To determine if a node is a leaf, it checks if the number of numchild is equal to 0. But a node can have soft deleted children, then numchild is equal to 0, but it is not a leaf because if we want to add a child we have to look for the last child to compute a correct path. Otherwise we will have an error saying that the path already exists.
This commit is contained in:
committed by
Anthony LC
parent
ecb20f6f77
commit
40ed2d2e22
@@ -0,0 +1,17 @@
|
|||||||
|
# Generated by Django 5.1.7 on 2025-03-14 14:03
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("core", "0020_remove_is_public_add_field_attachments_and_duplicated_from"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="document",
|
||||||
|
name="has_deleted_children",
|
||||||
|
field=models.BooleanField(default=False),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -384,6 +384,7 @@ class Document(MP_Node, BaseModel):
|
|||||||
)
|
)
|
||||||
deleted_at = models.DateTimeField(null=True, blank=True)
|
deleted_at = models.DateTimeField(null=True, blank=True)
|
||||||
ancestors_deleted_at = models.DateTimeField(null=True, blank=True)
|
ancestors_deleted_at = models.DateTimeField(null=True, blank=True)
|
||||||
|
has_deleted_children = models.BooleanField(default=False)
|
||||||
duplicated_from = models.ForeignKey(
|
duplicated_from = models.ForeignKey(
|
||||||
"self",
|
"self",
|
||||||
on_delete=models.SET_NULL,
|
on_delete=models.SET_NULL,
|
||||||
@@ -465,6 +466,12 @@ class Document(MP_Node, BaseModel):
|
|||||||
content_file = ContentFile(bytes_content)
|
content_file = ContentFile(bytes_content)
|
||||||
default_storage.save(file_key, content_file)
|
default_storage.save(file_key, content_file)
|
||||||
|
|
||||||
|
def is_leaf(self):
|
||||||
|
"""
|
||||||
|
:returns: True if the node is has no children
|
||||||
|
"""
|
||||||
|
return not self.has_deleted_children and self.numchild == 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def key_base(self):
|
def key_base(self):
|
||||||
"""Key base of the location where the document is stored in object storage."""
|
"""Key base of the location where the document is stored in object storage."""
|
||||||
@@ -886,7 +893,8 @@ class Document(MP_Node, BaseModel):
|
|||||||
|
|
||||||
if self.depth > 1:
|
if self.depth > 1:
|
||||||
self._meta.model.objects.filter(pk=self.get_parent().pk).update(
|
self._meta.model.objects.filter(pk=self.get_parent().pk).update(
|
||||||
numchild=models.F("numchild") - 1
|
numchild=models.F("numchild") - 1,
|
||||||
|
has_deleted_children=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Mark all descendants as soft deleted
|
# Mark all descendants as soft deleted
|
||||||
|
|||||||
Reference in New Issue
Block a user