Fix: bump modified for documents changed by a bulk edit

This commit is contained in:
Trenton H committed 2026-11-15 15:08:45 -08:00
1 parent 08df5db033
commit 5bd597d38c
2 files changed
+40

No files matched your search

+4
View File
@@ -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
+36
View File
@@ -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(