mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-16 10:53:59 +00:00
Collect failed ids for rotate instead of stopping mid loop
This commit is contained in:
3 files changed
+67
-3
No files matched your search
@@ -539,6 +539,7 @@ def rotate(
|
||||
pair = _resolve_root_and_source_doc(doc, source_mode=source_mode)
|
||||
docs_by_root_id.setdefault(pair.root_doc.id, pair)
|
||||
|
||||
failed_ids = []
|
||||
for pair in docs_by_root_id.values():
|
||||
if pair.source_doc.mime_type != "application/pdf":
|
||||
logger.warning(
|
||||
@@ -573,8 +574,13 @@ def rotate(
|
||||
f"Queued new rotated version for document {pair.root_doc.id} by {degrees} degrees",
|
||||
)
|
||||
except Exception as e:
|
||||
failed_ids.append(pair.root_doc.id)
|
||||
logger.exception(f"Error rotating document {pair.root_doc.id}: {e}")
|
||||
raise
|
||||
|
||||
if failed_ids:
|
||||
raise RuntimeError(
|
||||
f"Rotation failed for document(s): {failed_ids}. Check logs for details.",
|
||||
)
|
||||
|
||||
return "OK"
|
||||
|
||||
|
||||
@@ -1243,13 +1243,68 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
||||
doc_ids = [self.doc2.id, self.doc3.id]
|
||||
|
||||
with self.assertLogs("paperless.bulk_edit", level="ERROR") as cm:
|
||||
with self.assertRaisesMessage(Exception, "Error rotating PDF"):
|
||||
with self.assertRaisesMessage(
|
||||
RuntimeError,
|
||||
f"Rotation failed for document(s): {doc_ids}",
|
||||
):
|
||||
bulk_edit.rotate(doc_ids, 90)
|
||||
error_str = cm.output[0]
|
||||
expected_str = "Error rotating document"
|
||||
self.assertIn(expected_str, error_str)
|
||||
self.assertEqual(mock_rotate_pdf.call_count, 2)
|
||||
mock_consume_delay.assert_not_called()
|
||||
|
||||
@mock.patch("documents.tasks.consume_file.apply_async")
|
||||
@mock.patch("documents.pdf_ops.rotate_pdf")
|
||||
def test_rotate_continues_after_preparation_failure(
|
||||
self,
|
||||
mock_rotate_pdf,
|
||||
mock_consume,
|
||||
) -> None:
|
||||
def rotate(source, destination, degrees):
|
||||
if source == self.doc2.source_path:
|
||||
raise RuntimeError("Invalid PDF")
|
||||
shutil.copy(source, destination)
|
||||
|
||||
mock_rotate_pdf.side_effect = rotate
|
||||
with self.assertRaisesMessage(
|
||||
RuntimeError,
|
||||
f"Rotation failed for document(s): [{self.doc2.id}]",
|
||||
):
|
||||
bulk_edit.rotate([self.doc2.id, self.doc3.id], 90)
|
||||
|
||||
self.assertEqual(mock_rotate_pdf.call_count, 2)
|
||||
mock_consume.assert_called_once()
|
||||
self.assertEqual(
|
||||
mock_consume.call_args.kwargs["kwargs"]["input_doc"].root_document_id,
|
||||
self.doc3.id,
|
||||
)
|
||||
|
||||
@mock.patch("documents.tasks.consume_file.apply_async")
|
||||
@mock.patch("documents.pdf_ops.rotate_pdf")
|
||||
def test_rotate_continues_after_queue_failure(
|
||||
self,
|
||||
mock_rotate_pdf,
|
||||
mock_consume,
|
||||
) -> None:
|
||||
mock_rotate_pdf.side_effect = lambda source, destination, degrees: shutil.copy(
|
||||
source,
|
||||
destination,
|
||||
)
|
||||
mock_consume.side_effect = [RuntimeError("Broker unavailable"), None]
|
||||
with self.assertRaisesMessage(
|
||||
RuntimeError,
|
||||
f"Rotation failed for document(s): [{self.doc2.id}]",
|
||||
):
|
||||
bulk_edit.rotate([self.doc2.id, self.doc3.id], 90)
|
||||
|
||||
self.assertEqual(mock_rotate_pdf.call_count, 2)
|
||||
self.assertEqual(mock_consume.call_count, 2)
|
||||
self.assertEqual(
|
||||
mock_consume.call_args.kwargs["kwargs"]["input_doc"].root_document_id,
|
||||
self.doc3.id,
|
||||
)
|
||||
|
||||
@mock.patch("documents.tasks.consume_file.apply_async")
|
||||
def test_rotate_non_pdf(
|
||||
self,
|
||||
|
||||
@@ -209,7 +209,10 @@ def test_preparation_failure_propagates_from_worker(
|
||||
),
|
||||
mock.patch("documents.bulk_edit.consume_file.apply_async") as consume,
|
||||
mock.patch("documents.bulk_edit.consume_file.s") as consume_signature,
|
||||
pytest.raises(RuntimeError, match="preparation failed"),
|
||||
pytest.raises(
|
||||
RuntimeError,
|
||||
match="Rotation failed" if operation == "rotate" else "preparation failed",
|
||||
),
|
||||
):
|
||||
execute_bulk_operation(operation, [doc.pk], parameters, user.pk)
|
||||
consume.assert_not_called()
|
||||
|
||||
Reference in new issue
Block a user