From 3e467c517d93c2b38933fcc4f70658177415a98f Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Wed, 25 Jan 2023 14:06:36 -0800 Subject: [PATCH] Allows filtering email by the TO value(s) as well --- .../fixtures/mail_rules/mail_rules.json | 1 + .../mail-rule-edit-dialog.component.html | 1 + .../mail-rule-edit-dialog.component.ts | 1 + .../manage/settings/settings.component.ts | 2 + src-ui/src/app/data/paperless-mail-rule.ts | 2 + src/paperless_mail/admin.py | 1 + src/paperless_mail/mail.py | 11 +++++- .../migrations/0019_mailrule_filter_to.py | 19 ++++++++++ src/paperless_mail/models.py | 9 +++++ src/paperless_mail/serialisers.py | 1 + src/paperless_mail/tests/test_api.py | 4 ++ src/paperless_mail/tests/test_mail.py | 37 +++++++++++++++---- 12 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 src/paperless_mail/migrations/0019_mailrule_filter_to.py diff --git a/src-ui/cypress/fixtures/mail_rules/mail_rules.json b/src-ui/cypress/fixtures/mail_rules/mail_rules.json index 7767940e7..5341b9cfa 100644 --- a/src-ui/cypress/fixtures/mail_rules/mail_rules.json +++ b/src-ui/cypress/fixtures/mail_rules/mail_rules.json @@ -9,6 +9,7 @@ "account": 2, "folder": "INBOX", "filter_from": null, + "filter_to": null, "filter_subject": "[paperless]", "filter_body": null, "filter_attachment_filename": null, diff --git a/src-ui/src/app/components/common/edit-dialog/mail-rule-edit-dialog/mail-rule-edit-dialog.component.html b/src-ui/src/app/components/common/edit-dialog/mail-rule-edit-dialog/mail-rule-edit-dialog.component.html index ebb7458e6..b9802ea2a 100644 --- a/src-ui/src/app/components/common/edit-dialog/mail-rule-edit-dialog/mail-rule-edit-dialog.component.html +++ b/src-ui/src/app/components/common/edit-dialog/mail-rule-edit-dialog/mail-rule-edit-dialog.component.html @@ -18,6 +18,7 @@

Paperless will only process mails that match all of the filters specified below.

+ diff --git a/src-ui/src/app/components/common/edit-dialog/mail-rule-edit-dialog/mail-rule-edit-dialog.component.ts b/src-ui/src/app/components/common/edit-dialog/mail-rule-edit-dialog/mail-rule-edit-dialog.component.ts index 25821242a..5e0ce49a7 100644 --- a/src-ui/src/app/components/common/edit-dialog/mail-rule-edit-dialog/mail-rule-edit-dialog.component.ts +++ b/src-ui/src/app/components/common/edit-dialog/mail-rule-edit-dialog/mail-rule-edit-dialog.component.ts @@ -149,6 +149,7 @@ export class MailRuleEditDialogComponent extends EditDialogComponent MailMessage: + + if to is None: + to = ["tosomeone@somewhere.com"] + email_msg = email.message.EmailMessage() # TODO: This does NOT set the UID email_msg["Message-ID"] = str(uuid.uuid4()) email_msg["Subject"] = subject email_msg["From"] = from_ + email_msg["To"] = str(" ,".join(to)) email_msg.set_content(body) # Either add some default number of attachments @@ -266,6 +281,7 @@ class TestMail(DirectoriesMixin, FileSystemAssertsMixin, TestCase): self.create_message( subject="Invoice 1", from_="amazon@amazon.de", + to=["me@myselfandi.com", "helpdesk@mydomain.com"], body="cables", seen=True, flagged=False, @@ -276,6 +292,7 @@ class TestMail(DirectoriesMixin, FileSystemAssertsMixin, TestCase): self.create_message( subject="Invoice 2", body="from my favorite electronic store", + to=["invoices@mycompany.com"], seen=False, flagged=True, processed=True, @@ -285,6 +302,7 @@ class TestMail(DirectoriesMixin, FileSystemAssertsMixin, TestCase): self.create_message( subject="Claim your $10M price now!", from_="amazon@amazon-some-indian-site.org", + to="special@me.me", seen=False, ), ) @@ -946,21 +964,26 @@ class TestMail(DirectoriesMixin, FileSystemAssertsMixin, TestCase): password="secret", ) - for (f_body, f_from, f_subject, expected_mail_count) in [ - (None, None, "Claim", 1), - ("electronic", None, None, 1), - (None, "amazon", None, 2), - ("cables", "amazon", "Invoice", 1), + for (f_body, f_from, f_to, f_subject, expected_mail_count) in [ + (None, None, None, "Claim", 1), + ("electronic", None, None, None, 1), + (None, "amazon", None, None, 2), + ("cables", "amazon", None, "Invoice", 1), + (None, None, "test@email.com", None, 0), + (None, None, "invoices@mycompany.com", None, 1), + ("electronic", None, "invoices@mycompany.com", None, 1), + (None, "amazon", "me@myselfandi.com", None, 1), ]: with self.subTest(f_body=f_body, f_from=f_from, f_subject=f_subject): MailRule.objects.all().delete() - rule = MailRule.objects.create( + _ = MailRule.objects.create( name="testrule3", account=account, action=MailRule.MailAction.DELETE, filter_subject=f_subject, filter_body=f_body, filter_from=f_from, + filter_to=f_to, ) self.reset_bogus_mailbox() self._queue_consumption_tasks_mock.reset_mock()