mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-26 01:16:16 +00:00
Auto-handle parent tags in tag update
This commit is contained in:
@@ -90,3 +90,17 @@ class TestTagHierarchy(APITestCase):
|
|||||||
run_workflows(trigger.type, self.document)
|
run_workflows(trigger.type, self.document)
|
||||||
self.document.refresh_from_db()
|
self.document.refresh_from_db()
|
||||||
assert self.document.tags.count() == 0
|
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}
|
||||||
|
@@ -341,6 +341,39 @@ class TagViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):
|
|||||||
filterset_class = TagFilterSet
|
filterset_class = TagFilterSet
|
||||||
ordering_fields = ("color", "name", "matching_algorithm", "match", "document_count")
|
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))
|
@extend_schema_view(**generate_object_with_permissions_schema(DocumentTypeSerializer))
|
||||||
class DocumentTypeViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):
|
class DocumentTypeViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):
|
||||||
|
Reference in New Issue
Block a user