Fix: move files before the bulk update websocket notification

This commit is contained in:
Trenton H committed 2026-11-15 15:08:29 -08:00
1 parent 1721bd559a
commit 08df5db033
2 files changed
+96 -3

No files matched your search

+19 -3
View File
@@ -324,15 +324,31 @@ def bulk_update_documents(document_ids) -> None:
for doc in documents:
clear_document_caches(doc.pk)
# The bulk edit wrote through querysets and relation tables, so this
# post_save is what requests the move. Move before document_updated,
# whose websocket notification must carry the final filename and
# modified timestamp.
post_save.send(Document, instance=doc, created=False)
flush_pending_file_moves()
# The move changed the stored paths. Workflows read source_path from
# this instance, so bring it up to date. Not refresh_from_db(), which
# would drop the effective content annotation.
paths = (
Document.global_objects.filter(pk=doc.pk)
.values_list("filename", "archive_filename")
.first()
)
if paths is not None:
doc.filename, doc.archive_filename = paths
document_updated.send(
sender=None,
document=doc,
logging_group=uuid.uuid4(),
skip_ai_index=True, # bulk path calls update_llm_index once below
)
post_save.send(Document, instance=doc, created=False)
# Move this document's files now rather than at the end of the task,
# so a long bulk edit makes steady progress.
# Moves requested by workflows that document_updated ran. Moving per
# document, rather than at the end of the task, keeps a long bulk
# edit making steady progress.
flush_pending_file_moves()
with get_backend().batch_update() as batch:
+77
View File
@@ -32,6 +32,7 @@ from documents.models import CustomField
from documents.models import Document
from documents.models import Workflow
from documents.models import WorkflowAction
from documents.models import WorkflowActionWebhook
from documents.models import WorkflowTrigger
from documents.tasks import bulk_update_documents
from documents.tasks import check_scheduled_workflows
@@ -1481,6 +1482,82 @@ class TestCeleryTaskScope:
assert state == {"moved": True, "filename": "renamed.pdf"}
@pytest.mark.django_db
class TestBulkUpdateNotification:
def test_websocket_notification_sent_after_move(
self,
document: Document,
paperless_dirs: PaperlessDirs,
mocker: MockerFixture,
) -> None:
"""
GIVEN:
- A document whose title changed in the DB without any signal, as
bulk edits do through querysets and relation tables
WHEN:
- bulk_update_documents runs for it
THEN:
- The websocket notification is sent after the file has moved and
the filename column is updated, so clients see the final state
"""
Document.objects.filter(pk=document.pk).update(title="renamed")
state: dict[str, object] = {}
def notify(**kwargs: object) -> None:
state["moved"] = (paperless_dirs.originals_dir / "renamed.pdf").is_file()
state["filename"] = Document.objects.get(pk=document.pk).filename
status_manager = mocker.patch(
"documents.signals.handlers.DocumentsStatusManager",
)
status_manager.return_value.__enter__.return_value.send_document_updated.side_effect = notify
bulk_update_documents.apply(args=([document.pk],))
assert state == {"moved": True, "filename": "renamed.pdf"}
def test_webhook_workflow_attaches_document_at_moved_path(
self,
document: Document,
mocker: MockerFixture,
) -> None:
"""
GIVEN:
- A document updated workflow with a webhook that includes the
document
- A document whose title changed in the DB without any signal
WHEN:
- bulk_update_documents runs for it
THEN:
- The file moved before the workflow ran, and the webhook still
reads it from its new path
"""
trigger = WorkflowTrigger.objects.create(
type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_UPDATED,
)
webhook = WorkflowActionWebhook.objects.create(
use_params=False,
body="body",
url="http://paperless-ngx.com",
include_document=True,
)
workflow = Workflow.objects.create(name="webhook", order=0)
workflow.triggers.add(trigger)
workflow.actions.add(
WorkflowAction.objects.create(
type=WorkflowAction.WorkflowActionType.WEBHOOK,
webhook=webhook,
),
)
send = mocker.patch("documents.workflows.webhooks.send_webhook.apply_async")
Document.objects.filter(pk=document.pk).update(title="renamed")
bulk_update_documents.apply(args=([document.pk],))
send.assert_called_once()
assert send.call_args.kwargs["kwargs"]["files"]["file"][1] == CONTENT
@pytest.mark.django_db
class TestEmptyTrashLockOrdering:
def test_hard_delete_takes_media_lock_outside_transaction(