From 292a76137dda74b1da15def9f1c2db47ea7dafeb Mon Sep 17 00:00:00 2001 From: Jonas Winkler Date: Tue, 17 Nov 2020 15:23:17 +0100 Subject: [PATCH] added some result processing --- src/paperless_mail/mail.py | 16 +++++++++++----- src/paperless_mail/tasks.py | 8 +++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/paperless_mail/mail.py b/src/paperless_mail/mail.py index b586a12a9..cbc83223c 100644 --- a/src/paperless_mail/mail.py +++ b/src/paperless_mail/mail.py @@ -79,6 +79,8 @@ def handle_mail_account(account): else: raise ValueError("Unknown IMAP security") + total_processed_files = 0 + with mailbox as M: try: @@ -121,14 +123,15 @@ def handle_mail_account(account): for message in messages: try: - result = handle_message(message, rule) + processed_files = handle_message(message, rule) except Exception: raise MailError( f"Rule {rule.name}: Error while processing mail " f"{message.uid} of account {account.name}") - if result: + if processed_files > 0: post_consume_messages.append(message.uid) + total_processed_files += processed_files try: action.post_consume( M, @@ -140,10 +143,12 @@ def handle_mail_account(account): f"Rule {rule.name}: Error while processing post-consume " f"actions for account {account.name}") + return total_processed_files + def handle_message(message, rule): if not message.attachments: - return False + return 0 if rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NOTHING: correspondent = None @@ -200,9 +205,10 @@ def handle_message(message, rule): force_title=title, force_correspondent_id=correspondent.id if correspondent else None, force_document_type_id=doc_type.id if doc_type else None, - force_tag_ids=[tag.id] if tag else None + force_tag_ids=[tag.id] if tag else None, + task_name=f"Mail: {att.filename}" ) processed_attachments += 1 - return processed_attachments > 0 + return processed_attachments diff --git a/src/paperless_mail/tasks.py b/src/paperless_mail/tasks.py index d65f83eaa..d34941a8a 100644 --- a/src/paperless_mail/tasks.py +++ b/src/paperless_mail/tasks.py @@ -5,8 +5,14 @@ from paperless_mail.models import MailAccount def process_mail_accounts(): + total_new_documents = 0 for account in MailAccount.objects.all(): - mail.handle_mail_account(account) + total_new_documents += mail.handle_mail_account(account) + + if total_new_documents > 0: + return f"Added {total_new_documents} document(s)." + else: + return "No new documents were added." def process_mail_account(name):