Skip to content

Commit 40ed2d2

Browse files
PanchoutNathanAntoLC
authored andcommitted
🐛(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.
1 parent ecb20f6 commit 40ed2d2

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 5.1.7 on 2025-03-14 14:03
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("core", "0020_remove_is_public_add_field_attachments_and_duplicated_from"),
9+
]
10+
11+
operations = [
12+
migrations.AddField(
13+
model_name="document",
14+
name="has_deleted_children",
15+
field=models.BooleanField(default=False),
16+
),
17+
]

src/backend/core/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ class Document(MP_Node, BaseModel):
384384
)
385385
deleted_at = models.DateTimeField(null=True, blank=True)
386386
ancestors_deleted_at = models.DateTimeField(null=True, blank=True)
387+
has_deleted_children = models.BooleanField(default=False)
387388
duplicated_from = models.ForeignKey(
388389
"self",
389390
on_delete=models.SET_NULL,
@@ -465,6 +466,12 @@ def save(self, *args, **kwargs):
465466
content_file = ContentFile(bytes_content)
466467
default_storage.save(file_key, content_file)
467468

469+
def is_leaf(self):
470+
"""
471+
:returns: True if the node is has no children
472+
"""
473+
return not self.has_deleted_children and self.numchild == 0
474+
468475
@property
469476
def key_base(self):
470477
"""Key base of the location where the document is stored in object storage."""
@@ -886,7 +893,8 @@ def soft_delete(self):
886893

887894
if self.depth > 1:
888895
self._meta.model.objects.filter(pk=self.get_parent().pk).update(
889-
numchild=models.F("numchild") - 1
896+
numchild=models.F("numchild") - 1,
897+
has_deleted_children=True,
890898
)
891899

892900
# Mark all descendants as soft deleted

0 commit comments

Comments
 (0)