Fix: skip accounts without enabled rules

This commit is contained in:
shamoon 2024-10-04 23:59:31 -07:00
parent 54293bedb1
commit e49ed58f1a
2 changed files with 42 additions and 0 deletions

View File

@ -5,6 +5,7 @@ from celery import shared_task
from paperless_mail.mail import MailAccountHandler
from paperless_mail.mail import MailError
from paperless_mail.models import MailAccount
from paperless_mail.models import MailRule
logger = logging.getLogger("paperless.mail.tasks")
@ -13,6 +14,9 @@ logger = logging.getLogger("paperless.mail.tasks")
def process_mail_accounts():
total_new_documents = 0
for account in MailAccount.objects.all():
if not MailRule.objects.filter(account=account, enabled=True).exists():
logger.info(f"No rules enabled for account {account}. Skipping.")
continue
try:
total_new_documents += MailAccountHandler().handle_mail_account(account)
except MailError:

View File

@ -1543,6 +1543,14 @@ class TestTasks(TestCase):
username="A",
password="A",
)
MailRule.objects.create(
name="A",
account=MailAccount.objects.get(name="A"),
)
MailRule.objects.create(
name="B",
account=MailAccount.objects.get(name="B"),
)
result = tasks.process_mail_accounts()
@ -1552,3 +1560,33 @@ class TestTasks(TestCase):
m.side_effect = lambda account: 0
result = tasks.process_mail_accounts()
self.assertIn("No new", result)
@mock.patch("paperless_mail.tasks.MailAccountHandler.handle_mail_account")
def test_accounts_no_enabled_rules(self, m):
m.side_effect = lambda account: 6
MailAccount.objects.create(
name="A",
imap_server="A",
username="A",
password="A",
)
MailAccount.objects.create(
name="B",
imap_server="A",
username="A",
password="A",
)
MailRule.objects.create(
name="A",
account=MailAccount.objects.get(name="A"),
enabled=False,
)
MailRule.objects.create(
name="B",
account=MailAccount.objects.get(name="B"),
enabled=False,
)
tasks.process_mail_accounts()
self.assertEqual(m.call_count, 0)