mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
implement better mail actions
This commit is contained in:
parent
8c5ef111d8
commit
94ebe3b61c
@ -9,7 +9,7 @@ from typing import Dict
|
|||||||
|
|
||||||
import magic
|
import magic
|
||||||
import pathvalidate
|
import pathvalidate
|
||||||
from celery import chord
|
from celery import chord, shared_task
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import DatabaseError
|
from django.db import DatabaseError
|
||||||
from documents.loggers import LoggingMixin
|
from documents.loggers import LoggingMixin
|
||||||
@ -26,7 +26,6 @@ from imap_tools import NOT
|
|||||||
from imap_tools.mailbox import MailBoxTls
|
from imap_tools.mailbox import MailBoxTls
|
||||||
from paperless_mail.models import MailAccount
|
from paperless_mail.models import MailAccount
|
||||||
from paperless_mail.models import MailRule
|
from paperless_mail.models import MailRule
|
||||||
from paperless_mail.tasks import apply_mail_action
|
|
||||||
|
|
||||||
# Apple Mail sets multiple IMAP KEYWORD and the general "\Flagged" FLAG
|
# Apple Mail sets multiple IMAP KEYWORD and the general "\Flagged" FLAG
|
||||||
# imaplib => conn.fetch(b"<message_id>", "FLAGS")
|
# imaplib => conn.fetch(b"<message_id>", "FLAGS")
|
||||||
@ -145,6 +144,27 @@ class TagMailAction(BaseMailAction):
|
|||||||
raise MailError("No keyword specified.")
|
raise MailError("No keyword specified.")
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task
|
||||||
|
def apply_mail_action(
|
||||||
|
result: str,
|
||||||
|
rule_id: int,
|
||||||
|
message_uid: str,
|
||||||
|
):
|
||||||
|
rule = MailRule.objects.get(pk=rule_id)
|
||||||
|
account = MailAccount.objects.get(pk=rule.account.pk)
|
||||||
|
|
||||||
|
action = get_rule_action(rule)
|
||||||
|
|
||||||
|
with get_mailbox(
|
||||||
|
server=account.imap_server,
|
||||||
|
port=account.imap_port,
|
||||||
|
security=account.imap_security,
|
||||||
|
) as M:
|
||||||
|
M.login(username=account.username, password=account.password)
|
||||||
|
M.folder.set(rule.folder)
|
||||||
|
action.post_consume(M, message_uid, rule.action_parameter)
|
||||||
|
|
||||||
|
|
||||||
def get_rule_action(rule) -> BaseMailAction:
|
def get_rule_action(rule) -> BaseMailAction:
|
||||||
if rule.action == MailRule.MailAction.FLAG:
|
if rule.action == MailRule.MailAction.FLAG:
|
||||||
return FlagMailAction()
|
return FlagMailAction()
|
||||||
@ -531,10 +551,8 @@ class MailAccountHandler(LoggingMixin):
|
|||||||
)
|
)
|
||||||
|
|
||||||
mail_action_task = apply_mail_action.s(
|
mail_action_task = apply_mail_action.s(
|
||||||
M=M,
|
rule_id=rule.pk,
|
||||||
action=get_rule_action(rule),
|
|
||||||
message_uid=message.uid,
|
message_uid=message.uid,
|
||||||
parameter=rule.action_parameter,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
chord(header=consume_tasks, body=mail_action_task).delay()
|
chord(header=consume_tasks, body=mail_action_task).delay()
|
||||||
@ -597,10 +615,8 @@ class MailAccountHandler(LoggingMixin):
|
|||||||
)
|
)
|
||||||
|
|
||||||
mail_action_task = apply_mail_action.s(
|
mail_action_task = apply_mail_action.s(
|
||||||
M=M,
|
rule_id=rule.pk,
|
||||||
action=get_rule_action(rule),
|
|
||||||
message_uid=message.uid,
|
message_uid=message.uid,
|
||||||
parameter=rule.action_parameter,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
(consume_task | mail_action_task).delay()
|
(consume_task | mail_action_task).delay()
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
from imap_tools import MailBox
|
|
||||||
from paperless_mail.mail import BaseMailAction
|
|
||||||
from paperless_mail.mail import MailAccountHandler
|
from paperless_mail.mail import MailAccountHandler
|
||||||
from paperless_mail.mail import MailError
|
from paperless_mail.mail import MailError
|
||||||
from paperless_mail.models import MailAccount
|
from paperless_mail.models import MailAccount
|
||||||
@ -10,23 +9,12 @@ from paperless_mail.models import MailAccount
|
|||||||
logger = logging.getLogger("paperless.mail.tasks")
|
logger = logging.getLogger("paperless.mail.tasks")
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
|
||||||
def apply_mail_action(
|
|
||||||
result: str,
|
|
||||||
M: MailBox,
|
|
||||||
action: BaseMailAction,
|
|
||||||
message_uid: str,
|
|
||||||
parameter: str,
|
|
||||||
):
|
|
||||||
action.post_consume(M, message_uid, parameter)
|
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
def process_mail_accounts():
|
def process_mail_accounts():
|
||||||
total_new_documents = 0
|
total_new_documents = 0
|
||||||
for account in MailAccount.objects.all():
|
for account in MailAccount.objects.all():
|
||||||
try:
|
try:
|
||||||
total_new_documents += MailAccountHandler().handl2e_mail_account(account)
|
total_new_documents += MailAccountHandler().handle_mail_account(account)
|
||||||
except MailError:
|
except MailError:
|
||||||
logger.exception(f"Error while processing mail account {account}")
|
logger.exception(f"Error while processing mail account {account}")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user