Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fix: handle legacy bulk edit page range with missing page_count (#14212)

shamoon452ed005bd01
diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py
index c063a72fa..02c3866c6 100644
--- a/src/documents/serialisers.py
+++ b/src/documents/serialisers.py
@@ -2098,6 +2098,8 @@ class BulkEditSerializer(
if not isinstance(parameters["pages"], str):
raise serializers.ValidationError("invalid pages specified")
page_count = Document.objects.get(id=document_id).page_count
+ if not page_count:
+ raise serializers.ValidationError("document page count is unknown")
pages = []
for group in parameters["pages"].split(","):
start, is_range, end = group.partition("-")
@@ -2107,7 +2109,7 @@ class BulkEditSerializer(
except ValueError as e:
raise serializers.ValidationError("invalid pages specified") from e
# Bound the range before building it, a huge one would exhaust memory
- if not 1 <= first <= last or (page_count and last > page_count):
+ if not 1 <= first <= last <= page_count:
raise serializers.ValidationError("invalid pages specified")
pages.append(list(range(first, last + 1)))
parameters["pages"] = pages
diff --git a/src/documents/tests/test_api_bulk_edit.py b/src/documents/tests/test_api_bulk_edit.py
index 275e3aa27..bb43dd8cb 100644
--- a/src/documents/tests/test_api_bulk_edit.py
+++ b/src/documents/tests/test_api_bulk_edit.py
@@ -1784,6 +1784,36 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
self.assertIn(b"invalid pages specified", response.content)
m.assert_not_called()
+ @mock.patch("documents.serialisers.bulk_edit.split")
+ def test_bulk_edit_split_rejects_unknown_page_count(self, m) -> None:
+ """
+ GIVEN:
+ - A legacy split bulk edit of a document without a page count
+ WHEN:
+ - API to bulk edit is called
+ THEN:
+ - API returns HTTP 400
+ - split is not called
+ """
+ self.setup_mock(m, "split")
+
+ for pages in ("1", "1-5000000"):
+ with self.subTest(pages=pages):
+ response = self.client.post(
+ "/api/documents/bulk_edit/",
+ json.dumps(
+ {
+ "documents": [self.doc1.id],
+ "method": "split",
+ "parameters": {"pages": pages},
+ },
+ ),
+ content_type="application/json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn(b"document page count is unknown", response.content)
+ m.assert_not_called()
+
@mock.patch("documents.serialisers.bulk_edit.split")
def test_bulk_edit_split_parses_pages(self, m) -> None:
"""