From 5bd597d38c8091a24e78098e1fc907bd05e8f39e Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:56:24 -0800 Subject: [PATCH] Fix: bump modified for documents changed by a bulk edit --- src/documents/tasks.py | 4 +++ src/documents/tests/test_file_moves.py | 36 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/documents/tasks.py b/src/documents/tasks.py index d396155919..7a879251d5 100644 --- a/src/documents/tasks.py +++ b/src/documents/tasks.py @@ -314,6 +314,10 @@ def bulk_update_documents(document_ids) -> None: from documents.search import get_backend document_ids = list(document_ids) + # Bulk edits write through querysets and relation tables, which skip + # auto_now, and the file mover only writes when a path changes. Record the + # edit here, before the signals and notifications below read the documents. + Document.objects.filter(id__in=document_ids).update(modified=timezone.now()) # Annotated so the signal handlers below (e.g. matching) don't query the # versions of each document. Indexing re-queries and re-annotates its own # copy via add_or_update_ids() below, after these signals (and any diff --git a/src/documents/tests/test_file_moves.py b/src/documents/tests/test_file_moves.py index eb71e29821..c109f252e7 100644 --- a/src/documents/tests/test_file_moves.py +++ b/src/documents/tests/test_file_moves.py @@ -21,6 +21,7 @@ from django.utils.dateparse import parse_datetime from filelock import Timeout from rest_framework import status +from documents import bulk_edit from documents.file_moves import _active_collector from documents.file_moves import _path_matches_checksum from documents.file_moves import adefer_file_moves @@ -1558,6 +1559,41 @@ class TestBulkUpdateNotification: assert send.call_args.kwargs["kwargs"]["files"]["file"][1] == CONTENT +@pytest.mark.django_db +class TestBulkEditModified: + def test_modified_bumped_when_filename_unchanged( + self, + document: Document, + mocker: MockerFixture, + ) -> None: + """ + GIVEN: + - A document whose filename does not depend on its correspondent + WHEN: + - The correspondent is changed by a bulk edit, which writes through + a queryset and so does not touch modified itself + THEN: + - modified is bumped, so modified-time filters and scheduled + workflows see the edit even though no file moved + """ + correspondent = CorrespondentFactory() + mocker.patch( + "documents.bulk_edit.bulk_update_documents.apply_async", + side_effect=lambda kwargs, headers=None: bulk_update_documents.apply( + kwargs=kwargs, + ), + ) + before = timezone.now() - datetime.timedelta(days=1) + Document.objects.filter(pk=document.pk).update(modified=before) + + bulk_edit.set_correspondent([document.pk], correspondent.pk) + + document.refresh_from_db() + assert document.correspondent_id == correspondent.pk + assert document.filename == "original.pdf" + assert document.modified > before + + @pytest.mark.django_db class TestEmptyTrashLockOrdering: def test_hard_delete_takes_media_lock_outside_transaction(