added some result processing

This commit is contained in:
Jonas Winkler 2020-11-17 15:23:17 +01:00
parent 8c3ff4fa26
commit 292a76137d
2 changed files with 18 additions and 6 deletions

View File

@ -79,6 +79,8 @@ def handle_mail_account(account):
else: else:
raise ValueError("Unknown IMAP security") raise ValueError("Unknown IMAP security")
total_processed_files = 0
with mailbox as M: with mailbox as M:
try: try:
@ -121,14 +123,15 @@ def handle_mail_account(account):
for message in messages: for message in messages:
try: try:
result = handle_message(message, rule) processed_files = handle_message(message, rule)
except Exception: except Exception:
raise MailError( raise MailError(
f"Rule {rule.name}: Error while processing mail " f"Rule {rule.name}: Error while processing mail "
f"{message.uid} of account {account.name}") f"{message.uid} of account {account.name}")
if result: if processed_files > 0:
post_consume_messages.append(message.uid) post_consume_messages.append(message.uid)
total_processed_files += processed_files
try: try:
action.post_consume( action.post_consume(
M, M,
@ -140,10 +143,12 @@ def handle_mail_account(account):
f"Rule {rule.name}: Error while processing post-consume " f"Rule {rule.name}: Error while processing post-consume "
f"actions for account {account.name}") f"actions for account {account.name}")
return total_processed_files
def handle_message(message, rule): def handle_message(message, rule):
if not message.attachments: if not message.attachments:
return False return 0
if rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NOTHING: if rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NOTHING:
correspondent = None correspondent = None
@ -200,9 +205,10 @@ def handle_message(message, rule):
force_title=title, force_title=title,
force_correspondent_id=correspondent.id if correspondent else None, force_correspondent_id=correspondent.id if correspondent else None,
force_document_type_id=doc_type.id if doc_type 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 processed_attachments += 1
return processed_attachments > 0 return processed_attachments

View File

@ -5,8 +5,14 @@ from paperless_mail.models import MailAccount
def process_mail_accounts(): def process_mail_accounts():
total_new_documents = 0
for account in MailAccount.objects.all(): 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): def process_mail_account(name):