From 0837a9633bdbae9ef383b597cc1652a6e43f694e Mon Sep 17 00:00:00 2001 From: Trenton Holmes <797416+stumpylog@users.noreply.github.com> Date: Sat, 2 May 2026 15:20:52 -0700 Subject: [PATCH] In fact, lets let the error propgate upwards for a 400 instead --- src/documents/bulk_edit.py | 55 +++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index ac89fb4d5..a557b9de9 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -218,7 +218,7 @@ def modify_tags( doc_ids: list[int], add_tags: list[int], remove_tags: list[int], -) -> Literal["OK"] | Literal["ERROR"]: +) -> Literal["OK"]: qs = Document.objects.filter(id__in=doc_ids).only("pk") affected_docs = list(qs.values_list("pk", flat=True)) DocumentTagRelationship = Document.tags.through @@ -237,40 +237,35 @@ def modify_tags( expanded_remove_tags.add(int(t.id)) expanded_remove_tags.update(int(pk) for pk in t.get_descendants_pks()) - try: - with transaction.atomic(): - if expanded_remove_tags: + with transaction.atomic(): + if expanded_remove_tags: + DocumentTagRelationship.objects.filter( + document_id__in=affected_docs, + tag_id__in=expanded_remove_tags, + ).delete() + + to_create = [] + if expanded_add_tags: + existing_pairs = set( DocumentTagRelationship.objects.filter( document_id__in=affected_docs, - tag_id__in=expanded_remove_tags, - ).delete() + tag_id__in=expanded_add_tags, + ).values_list("document_id", "tag_id"), + ) - to_create = [] - if expanded_add_tags: - existing_pairs = set( - DocumentTagRelationship.objects.filter( - document_id__in=affected_docs, - tag_id__in=expanded_add_tags, - ).values_list("document_id", "tag_id"), + to_create = [ + DocumentTagRelationship(document_id=doc, tag_id=tag) + for doc in affected_docs + for tag in expanded_add_tags + if (doc, tag) not in existing_pairs + ] + + if to_create: + DocumentTagRelationship.objects.bulk_create( + to_create, + ignore_conflicts=True, ) - to_create = [ - DocumentTagRelationship(document_id=doc, tag_id=tag) - for doc in affected_docs - for tag in expanded_add_tags - if (doc, tag) not in existing_pairs - ] - - if to_create: - DocumentTagRelationship.objects.bulk_create( - to_create, - ignore_conflicts=True, - ) - - except Exception as e: - logger.error(f"Error modifying tags: {e}") - return "ERROR" - if affected_docs: bulk_update_documents.apply_async( kwargs={"document_ids": affected_docs},