Skip to content

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

Fix: better catch email workflow placeholder parsing errors (#14129)

shamoone8b7a98d3ec5
diff --git a/src/documents/tests/test_workflows.py b/src/documents/tests/test_workflows.py
index 45a3703d2..8f241e289 100644
--- a/src/documents/tests/test_workflows.py
+++ b/src/documents/tests/test_workflows.py
@@ -3769,6 +3769,66 @@ class TestWorkflows(
expected_str = "Error occurred sending email"
self.assertIn(expected_str, cm.output[0])
+ @override_settings(
+ EMAIL_ENABLED=True,
+ PAPERLESS_URL="http://localhost:8000",
+ )
+ @mock.patch("django.core.mail.message.EmailMessage.send")
+ def test_workflow_email_action_template_error(self, mock_email_send) -> None:
+ """
+ GIVEN:
+ - Document added workflow with an email action whose body uses an
+ undefined template variable, followed by an assignment action
+ WHEN:
+ - Document consumption finishes
+ THEN:
+ - Error is logged, consumption is not aborted
+ - No email is sent
+ - Subsequent actions still run
+ """
+ trigger = WorkflowTrigger.objects.create(
+ type=WorkflowTrigger.WorkflowTriggerType.DOCUMENT_ADDED,
+ )
+ email_action = WorkflowActionEmail.objects.create(
+ subject="Test Notification: {{ doc_title }}",
+ body="Document Title: {{ title }}",
+ )
+ action = WorkflowAction.objects.create(
+ type=WorkflowAction.WorkflowActionType.EMAIL,
+ email=email_action,
+ order=0,
+ )
+ assignment_action = WorkflowAction.objects.create(
+ type=WorkflowAction.WorkflowActionType.ASSIGNMENT,
+ assign_correspondent=self.c2,
+ order=1,
+ )
+ w = Workflow.objects.create(
+ name="Workflow 1",
+ order=0,
+ )
+ w.triggers.add(trigger)
+ w.actions.add(action, assignment_action)
+ w.save()
+
+ doc = Document.objects.create(
+ title="sample test",
+ correspondent=self.c,
+ original_filename="sample.pdf",
+ )
+
+ with self.assertLogs("paperless.workflows", level="ERROR") as cm:
+ document_consumption_finished.send(
+ sender=self.__class__,
+ document=doc,
+ )
+
+ self.assertIn("'title' is undefined", cm.output[0])
+ mock_email_send.assert_not_called()
+ doc.refresh_from_db()
+ self.assertEqual(doc.correspondent, self.c2)
+
@override_settings(
PAPERLESS_EMAIL_HOST="localhost",
EMAIL_ENABLED=True,
diff --git a/src/documents/workflows/actions.py b/src/documents/workflows/actions.py
index be0921747..5a2e0c6be 100644
--- a/src/documents/workflows/actions.py
+++ b/src/documents/workflows/actions.py
@@ -102,42 +102,42 @@ def execute_email_action(
)
return
- subject = (
- parse_w_workflow_placeholders(
- action.email.subject,
- context["correspondent"],
- context["document_type"],
- context["owner_username"],
- context["added"],
- context["filename"],
- context["current_filename"],
- context["created"],
- context["title"],
- context["doc_url"],
- context["id"],
+ try:
+ subject = (
+ parse_w_workflow_placeholders(
+ action.email.subject,
+ context["correspondent"],
+ context["document_type"],
+ context["owner_username"],
+ context["added"],
+ context["filename"],
+ context["current_filename"],
+ context["created"],
+ context["title"],
+ context["doc_url"],
+ context["id"],
+ )
+ if action.email.subject
+ else ""
)
- if action.email.subject
- else ""
- )
- body = (
- parse_w_workflow_placeholders(
- action.email.body,
- context["correspondent"],
- context["document_type"],
- context["owner_username"],
- context["added"],
- context["filename"],
- context["current_filename"],
- context["created"],
- context["title"],
- context["doc_url"],
- context["id"],
+ body = (
+ parse_w_workflow_placeholders(
+ action.email.body,
+ context["correspondent"],
+ context["document_type"],
+ context["owner_username"],
+ context["added"],
+ context["filename"],
+ context["current_filename"],
+ context["created"],
+ context["title"],
+ context["doc_url"],
+ context["id"],
+ )
+ if action.email.body
+ else ""
)
- if action.email.body
- else ""
- )
- try:
attachments: list[EmailAttachment] = []
if action.email.include_document:
attachment: EmailAttachment | None = None