From 8769dc894e748e65b22f04661c1af8d6df08777a Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 8 May 2026 08:36:07 -0700 Subject: [PATCH] Fix: only update modified field in notes actions (#12750) --- src/documents/tests/test_api_documents.py | 40 +++++++++++++++++++++++ src/documents/views.py | 4 +-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py index 98908b80e..b0ec51d68 100644 --- a/src/documents/tests/test_api_documents.py +++ b/src/documents/tests/test_api_documents.py @@ -3112,6 +3112,46 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): # modified was updated to today self.assertEqual(doc.modified.day, timezone.now().day) + def test_create_note_only_saves_document_modified_field(self) -> None: + """ + GIVEN: + - Existing document with a created date + WHEN: + - API request is made to add a note + THEN: + - Only the document modified field is persisted by the note endpoint + - Other document fields are not rewritten by the note endpoint + """ + doc = Document.objects.create( + title="test", + mime_type="application/pdf", + content="this is a document which will have notes added", + created=datetime.date(2026, 3, 31), + ) + original_save = Document.save + + with mock.patch.object( + Document, + "save", + autospec=True, + side_effect=original_save, + ) as save_mock: + resp = self.client.post( + f"/api/documents/{doc.pk}/notes/", + data={"note": "this is a posted note"}, + ) + + self.assertEqual(resp.status_code, status.HTTP_200_OK) + doc.refresh_from_db() + self.assertEqual(doc.created, datetime.date(2026, 3, 31)) + self.assertTrue( + any( + call.kwargs.get("update_fields") == ["modified"] + for call in save_mock.call_args_list + if call.args and call.args[0].pk == doc.pk + ), + ) + def test_notes_permissions_aware(self) -> None: """ GIVEN: diff --git a/src/documents/views.py b/src/documents/views.py index 37dad973e..d93c23c16 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -1658,7 +1658,7 @@ class DocumentViewSet( ) doc.modified = timezone.now() - doc.save() + doc.save(update_fields=["modified"]) from documents.search import get_backend @@ -1702,7 +1702,7 @@ class DocumentViewSet( note.delete() doc.modified = timezone.now() - doc.save() + doc.save(update_fields=["modified"]) from documents.search import get_backend