Restore validate merge in serialzier and moved to shared helper

This commit is contained in:
shamoon committed 2026-11-15 16:06:52 -08:00
1 parent c914b38bfd
commit 9b56abb50b
3 files changed
+69 -26

No files matched your search

+17 -8
View File
@@ -692,6 +692,22 @@ def merge(
return "OK"
def validate_merge_as_versions_state(
documents: list[Document],
root_document_id: int,
) -> None:
if any(document.root_document_id is not None for document in documents):
raise ValueError("Only top-level documents can be merged as versions.")
source_ids = [
document.pk for document in documents if document.pk != root_document_id
]
if Document.global_objects.filter(root_document_id__in=source_ids).exists():
raise ValueError(
"Documents with existing versions cannot be merged into another document.",
)
def merge_as_versions(
doc_ids: list[int],
*,
@@ -709,15 +725,8 @@ def merge_as_versions(
)
documents_by_id = {document.id: document for document in documents}
if any(document.root_document_id is not None for document in documents):
raise ValueError("Only top-level documents can be merged as versions.")
validate_merge_as_versions_state(documents, root_document_id)
source_ids = [doc_id for doc_id in doc_ids if doc_id != root_document_id]
if Document.global_objects.filter(root_document_id__in=source_ids).exists():
raise ValueError(
"Documents with existing versions cannot be merged into another document.",
)
root_document = documents_by_id[root_document_id]
next_version_index = (
Document.global_objects.filter(
+11 -2
View File
@@ -1717,8 +1717,17 @@ class MergeDocumentsAsVersionsSerializer(DocumentListSerializer):
"root_document_id must be one of the selected documents.",
)
# Version state checks (e.g., whether a document is already part of a version chain)
# moved to merge_as_versions() under row locks, since it could change between queueing and execution
# Rechecked under row locks during execution in case document state changes.
selected_documents = list(
Document.objects.filter(id__in=documents).only("id", "root_document"),
)
try:
bulk_edit.validate_merge_as_versions_state(
selected_documents,
attrs["root_document_id"],
)
except ValueError as error:
raise serializers.ValidationError(str(error)) from error
return attrs
@@ -105,6 +105,24 @@ class TestMergeDocumentsAsVersionsSerializer(TestCase):
self.assertFalse(serializer.is_valid())
self.assertIn("documents", serializer.errors)
def test_rejects_selected_version(self) -> None:
version = Document.objects.create(
checksum="D",
root_document=self.doc1,
version_index=1,
)
serializer = MergeDocumentsAsVersionsSerializer(
data={
"documents": [version.id, self.doc2.id],
"root_document_id": self.doc2.id,
},
)
self.assertFalse(serializer.is_valid())
self.assertEqual(
serializer.errors["non_field_errors"][0],
"Only top-level documents can be merged as versions.",
)
def test_allows_root_document_with_versions(self) -> None:
Document.objects.create(
checksum="D",
@@ -485,28 +503,35 @@ class TestMergeDocumentsAsVersionsAPI(APITestCase):
merge_mock.assert_not_called()
@mock.patch("documents.views.execute_bulk_operation.apply_async")
def test_queues_source_document_with_versions(self, enqueue_mock) -> None:
enqueue_mock.return_value.id = "queued-merge"
Document.objects.create(
def test_rejects_source_document_with_versions_before_queueing(
self,
enqueue_mock,
) -> None:
version = Document.objects.create(
checksum="C",
title="C",
root_document=self.doc1,
version_index=1,
owner=self.user,
)
response = self.client.post(
"/api/documents/merge_as_versions/",
{
"documents": [self.doc1.id, self.doc2.id],
"root_document_id": self.doc2.id,
},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
enqueue_mock.assert_called_once()
self.assertEqual(response.data["task_id"], "queued-merge")
for trashed in (False, True):
with self.subTest(trashed=trashed):
if trashed:
version.delete()
response = self.client.post(
"/api/documents/merge_as_versions/",
{
"documents": [self.doc1.id, self.doc2.id],
"root_document_id": self.doc2.id,
},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data["non_field_errors"][0],
"Documents with existing versions cannot be merged into another document.",
)
enqueue_mock.assert_not_called()
self.doc1.refresh_from_db()
self.assertIsNone(self.doc1.root_document_id)