Auto-handle parent tags in tag update

This commit is contained in:
shamoon
2025-08-19 01:16:16 -07:00
parent 39eef0b13a
commit 3c3377799c
2 changed files with 47 additions and 0 deletions

View File

@@ -90,3 +90,17 @@ class TestTagHierarchy(APITestCase):
run_workflows(trigger.type, self.document)
self.document.refresh_from_db()
assert self.document.tags.count() == 0
def test_tag_view_parent_update_adds_parent_to_docs(self):
orphan = Tag.objects.create(name="Orphan")
self.document.tags.add(orphan)
self.client.patch(
f"/api/tags/{orphan.pk}/",
{"parent": self.parent.pk},
format="json",
)
self.document.refresh_from_db()
tags = set(self.document.tags.values_list("pk", flat=True))
assert tags == {self.parent.pk, orphan.pk}

View File

@@ -341,6 +341,39 @@ class TagViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):
filterset_class = TagFilterSet
ordering_fields = ("color", "name", "matching_algorithm", "match", "document_count")
def perform_update(self, serializer):
old_parent = self.get_object().parent
tag = serializer.save()
new_parent = tag.parent
if old_parent != new_parent:
self._update_document_parent_tags(tag, old_parent, new_parent)
def _update_document_parent_tags(self, tag, old_parent, new_parent):
DocumentTagRelationship = Document.tags.through
doc_ids = list(Document.objects.filter(tags=tag).values_list("pk", flat=True))
affected = set()
if new_parent:
parents_to_add = [new_parent, *new_parent.get_all_ancestors()]
to_create = []
for parent in parents_to_add:
missing = Document.objects.filter(id__in=doc_ids).exclude(tags=parent)
to_create.extend(
DocumentTagRelationship(document_id=doc_id, tag_id=parent.id)
for doc_id in missing.values_list("pk", flat=True)
)
affected.update(missing.values_list("pk", flat=True))
if to_create:
DocumentTagRelationship.objects.bulk_create(
to_create,
ignore_conflicts=True,
)
if affected:
from documents.tasks import bulk_update_documents
bulk_update_documents.delay(document_ids=list(affected))
@extend_schema_view(**generate_object_with_permissions_schema(DocumentTypeSerializer))
class DocumentTypeViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):