diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 1cd8ad509..81a4be32b 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -43,7 +43,7 @@ from documents.plugins.helpers import ProgressStatusOptions from documents.signals import document_consumption_finished from documents.signals import document_consumption_started from documents.signals.handlers import run_workflows -from documents.templating.title import parse_doc_title_w_placeholders +from documents.templating.workflows import parse_w_workflow_placeholders from documents.utils import copy_basic_file_stats from documents.utils import copy_file_with_basic_stats from documents.utils import run_subprocess @@ -666,7 +666,7 @@ class ConsumerPlugin( else None ) - return parse_doc_title_w_placeholders( + return parse_w_workflow_placeholders( title, correspondent_name, doc_type_name, diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index 6a95fddc1..fee517182 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -44,7 +44,7 @@ from documents.models import WorkflowRun from documents.models import WorkflowTrigger from documents.permissions import get_objects_for_user_owner_aware from documents.permissions import set_permissions_for_object -from documents.templating.title import parse_doc_title_w_placeholders +from documents.templating.workflows import parse_w_workflow_placeholders logger = logging.getLogger("paperless.handlers") @@ -612,7 +612,7 @@ def run_workflows( if action.assign_title: if not use_overrides: try: - document.title = parse_doc_title_w_placeholders( + document.title = parse_w_workflow_placeholders( action.assign_title, document.correspondent.name if document.correspondent else "", document.document_type.name if document.document_type else "", @@ -870,7 +870,16 @@ def run_workflows( overrides.custom_field_ids.remove(field.pk) def notification_action(): - subject = parse_doc_title_w_placeholders( + title = ( + document.title + if isinstance(document, Document) + else str(document.original_file) + ) + doc_url = None + if isinstance(document, Document): + doc_url = f"{settings.PAPERLESS_URL}/documents/{document.pk}/" + + subject = parse_w_workflow_placeholders( action.notification_subject, document.correspondent.name if document.correspondent else "", document.document_type.name if document.document_type else "", @@ -878,16 +887,21 @@ def run_workflows( timezone.localtime(document.added), document.original_filename or "", timezone.localtime(document.created), + title, + doc_url, ) - body = action.notification_body.format( - title=subject, - document=document, + body = parse_w_workflow_placeholders( + action.notification_body, + document.correspondent.name if document.correspondent else "", + document.document_type.name if document.document_type else "", + document.owner.username if document.owner else "", + timezone.localtime(document.added), + document.original_filename or "", + timezone.localtime(document.created), + title, + doc_url, ) - doc_url = None - if isinstance(document, Document): - doc_url = f"{settings.PAPERLESS_URL}/documents/{document.pk}/" - if doc_url: - body += f"\n\n{doc_url}" + if action.notification_destination_emails: if not settings.EMAIL_ENABLED: logger.error( diff --git a/src/documents/templating/title.py b/src/documents/templating/workflows.py similarity index 83% rename from src/documents/templating/title.py rename to src/documents/templating/workflows.py index 1dc668c27..1eea47dc3 100644 --- a/src/documents/templating/title.py +++ b/src/documents/templating/workflows.py @@ -2,14 +2,16 @@ from datetime import datetime from pathlib import Path -def parse_doc_title_w_placeholders( - title: str, +def parse_w_workflow_placeholders( + text: str, correspondent_name: str, doc_type_name: str, owner_username: str, local_added: datetime, original_filename: str, created: datetime | None = None, + doc_title: str | None = None, + doc_url: str | None = None, ) -> str: """ Available title placeholders for Workflows depend on what has already been assigned, @@ -43,4 +45,8 @@ def parse_doc_title_w_placeholders( "created_time": created.strftime("%H:%M"), }, ) - return title.format(**formatting).strip() + if doc_title is not None: + formatting.update({"doc_title": doc_title}) + if doc_url is not None: + formatting.update({"doc_url": doc_url}) + return text.format(**formatting).strip()