Enhancement: process mail button (#8466)

This commit is contained in:
shamoon
2024-12-11 11:03:53 -08:00
committed by GitHub
parent 2ac2a6dec6
commit dafb0b1f21
9 changed files with 196 additions and 49 deletions

View File

@@ -11,9 +11,14 @@ logger = logging.getLogger("paperless.mail.tasks")
@shared_task
def process_mail_accounts():
def process_mail_accounts(account_ids: list[int] | None = None) -> str:
total_new_documents = 0
for account in MailAccount.objects.all():
accounts = (
MailAccount.objects.filter(pk__in=account_ids)
if account_ids
else MailAccount.objects.all()
)
for account in accounts:
if not MailRule.objects.filter(account=account, enabled=True).exists():
logger.info(f"No rules enabled for account {account}. Skipping.")
continue

View File

@@ -1596,6 +1596,40 @@ class TestTasks(TestCase):
tasks.process_mail_accounts()
self.assertEqual(m.call_count, 0)
@mock.patch("paperless_mail.tasks.MailAccountHandler.handle_mail_account")
def test_process_with_account_ids(self, m):
m.side_effect = lambda account: 6
account_a = MailAccount.objects.create(
name="A",
imap_server="A",
username="A",
password="A",
)
account_b = MailAccount.objects.create(
name="B",
imap_server="A",
username="A",
password="A",
)
MailRule.objects.create(
name="A",
account=account_a,
)
MailRule.objects.create(
name="B",
account=account_b,
)
result = tasks.process_mail_accounts(account_ids=[account_a.id])
self.assertEqual(m.call_count, 1)
self.assertIn("Added 6", result)
m.side_effect = lambda account: 0
result = tasks.process_mail_accounts(account_ids=[account_b.id])
self.assertIn("No new", result)
class TestMailAccountTestView(APITestCase):
def setUp(self):
@@ -1720,3 +1754,30 @@ class TestMailAccountTestView(APITestCase):
error_str = cm.output[0]
expected_str = "Unable to refresh oauth token"
self.assertIn(expected_str, error_str)
class TestMailAccountProcess(APITestCase):
def setUp(self):
self.mailMocker = MailMocker()
self.mailMocker.setUp()
self.user = User.objects.create_superuser(
username="testuser",
password="testpassword",
)
self.client.force_authenticate(user=self.user)
self.account = MailAccount.objects.create(
imap_server="imap.example.com",
imap_port=993,
imap_security=MailAccount.ImapSecurity.SSL,
username="admin",
password="secret",
account_type=MailAccount.MailAccountType.IMAP,
owner=self.user,
)
self.url = f"/api/mail_accounts/{self.account.pk}/process/"
@mock.patch("paperless_mail.tasks.process_mail_accounts.delay")
def test_mail_account_process_view(self, m):
response = self.client.post(self.url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
m.assert_called_once()

View File

@@ -24,6 +24,7 @@ from paperless_mail.models import MailRule
from paperless_mail.oauth import PaperlessMailOAuth2Manager
from paperless_mail.serialisers import MailAccountSerializer
from paperless_mail.serialisers import MailRuleSerializer
from paperless_mail.tasks import process_mail_accounts
class MailAccountViewSet(ModelViewSet, PassUserMixin):
@@ -87,6 +88,13 @@ class MailAccountViewSet(ModelViewSet, PassUserMixin):
)
return HttpResponseBadRequest("Unable to connect to server")
@action(methods=["post"], detail=True)
def process(self, request, pk=None):
account = self.get_object()
process_mail_accounts.delay([account.pk])
return Response({"result": "OK"})
class MailRuleViewSet(ModelViewSet, PassUserMixin):
model = MailRule