And the backend

This commit is contained in:
shamoon
2026-09-04 07:14:58 -07:00
parent 8da770c3d7
commit 3f4d2a4b5a
2 changed files with 22 additions and 1 deletions
+9 -1
View File
@@ -1220,11 +1220,19 @@ class DocumentSerializer(
prev_tags = set(instance.tags.all())
requested_tags = set(validated_data["tags"])
# Tags being removed in this update and all descendants
# Tags newly added in this update and the ancestors they require
added_tags = requested_tags - prev_tags
required_by_add_tags = set(added_tags)
for t in added_tags:
required_by_add_tags.update(t.get_ancestors())
# Tags being removed in this update and all descendants, except
# those required by a tag that is being added in this same update
removed_tags = prev_tags - requested_tags
blocked_tags = set(removed_tags)
for t in removed_tags:
blocked_tags.update(t.get_descendants())
blocked_tags.difference_update(required_by_add_tags)
# Add all parent tags
final_tags = set(requested_tags)
+13
View File
@@ -75,6 +75,19 @@ class TestTagHierarchy(DirectoriesMixin, APITestCase):
tags = set(self.document.tags.values_list("pk", flat=True))
assert tags == {self.parent.pk, self.child.pk}
def test_document_api_add_child_keeps_parent_already_assigned(self) -> None:
# https://github.com/paperless-ngx/paperless-ngx/issues/13970
inbox = Tag.objects.create(name="Inbox", is_inbox_tag=True)
self.document.add_nested_tags([inbox, self.parent])
self.client.patch(
f"/api/documents/{self.document.pk}/",
{"tags": [self.child.pk]},
format="json",
)
self.document.refresh_from_db()
tags = set(self.document.tags.values_list("pk", flat=True))
assert tags == {self.parent.pk, self.child.pk}
def test_document_api_remove_parent_removes_children(self) -> None:
self.document.add_nested_tags([self.parent, self.child])
self.client.patch(