mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	refactor and testing
This commit is contained in:
		| @@ -55,19 +55,34 @@ class FlagMailAction(BaseMailAction): | |||||||
|         M.flag(message_uids, [MailMessageFlags.FLAGGED], True) |         M.flag(message_uids, [MailMessageFlags.FLAGGED], True) | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_rule_action(action): | def get_rule_action(rule): | ||||||
|     if action == MailRule.ACTION_FLAG: |     if rule.action == MailRule.ACTION_FLAG: | ||||||
|         return FlagMailAction() |         return FlagMailAction() | ||||||
|     elif action == MailRule.ACTION_DELETE: |     elif rule.action == MailRule.ACTION_DELETE: | ||||||
|         return DeleteMailAction() |         return DeleteMailAction() | ||||||
|     elif action == MailRule.ACTION_MOVE: |     elif rule.action == MailRule.ACTION_MOVE: | ||||||
|         return MoveMailAction() |         return MoveMailAction() | ||||||
|     elif action == MailRule.ACTION_MARK_READ: |     elif rule.action == MailRule.ACTION_MARK_READ: | ||||||
|         return MarkReadMailAction() |         return MarkReadMailAction() | ||||||
|     else: |     else: | ||||||
|         raise ValueError("Unknown action.") |         raise ValueError("Unknown action.") | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def make_criterias(rule): | ||||||
|  |     maximum_age = date.today() - timedelta(days=rule.maximum_age) | ||||||
|  |     criterias = { | ||||||
|  |         "date_gte": maximum_age | ||||||
|  |     } | ||||||
|  |     if rule.filter_from: | ||||||
|  |         criterias["from_"] = rule.filter_from | ||||||
|  |     if rule.filter_subject: | ||||||
|  |         criterias["subject"] = rule.filter_subject | ||||||
|  |     if rule.filter_body: | ||||||
|  |         criterias["body"] = rule.filter_body | ||||||
|  |  | ||||||
|  |     return {**criterias, **get_rule_action(rule).get_criteria()} | ||||||
|  |  | ||||||
|  |  | ||||||
| def handle_mail_account(account): | def handle_mail_account(account): | ||||||
|  |  | ||||||
|     if account.imap_security == MailAccount.IMAP_SECURITY_NONE: |     if account.imap_security == MailAccount.IMAP_SECURITY_NONE: | ||||||
| @@ -98,19 +113,7 @@ def handle_mail_account(account): | |||||||
|                     f"Rule {rule.name}: Folder {rule.folder} does not exist " |                     f"Rule {rule.name}: Folder {rule.folder} does not exist " | ||||||
|                     f"in account {account.name}") |                     f"in account {account.name}") | ||||||
|  |  | ||||||
|             maximum_age = date.today() - timedelta(days=rule.maximum_age) |             criterias = make_criterias(rule) | ||||||
|             criterias = { |  | ||||||
|                 "date_gte": maximum_age |  | ||||||
|             } |  | ||||||
|             if rule.filter_from: |  | ||||||
|                 criterias["from_"] = rule.filter_from |  | ||||||
|             if rule.filter_subject: |  | ||||||
|                 criterias["subject"] = rule.filter_subject |  | ||||||
|             if rule.filter_body: |  | ||||||
|                 criterias["body"] = rule.filter_body |  | ||||||
|  |  | ||||||
|             action = get_rule_action(rule.action) |  | ||||||
|             criterias = {**criterias, **action.get_criteria()} |  | ||||||
|  |  | ||||||
|             try: |             try: | ||||||
|                 messages = M.fetch(criteria=AND(**criterias), mark_seen=False) |                 messages = M.fetch(criteria=AND(**criterias), mark_seen=False) | ||||||
| @@ -133,7 +136,7 @@ def handle_mail_account(account): | |||||||
|  |  | ||||||
|                 total_processed_files += processed_files |                 total_processed_files += processed_files | ||||||
|             try: |             try: | ||||||
|                 action.post_consume( |                 get_rule_action(rule).post_consume( | ||||||
|                     M, |                     M, | ||||||
|                     post_consume_messages, |                     post_consume_messages, | ||||||
|                     rule.action_parameter) |                     rule.action_parameter) | ||||||
| @@ -146,10 +149,18 @@ def handle_mail_account(account): | |||||||
|     return total_processed_files |     return total_processed_files | ||||||
|  |  | ||||||
|  |  | ||||||
| def handle_message(message, rule): | def get_title(message, att, rule): | ||||||
|     if not message.attachments: |     if rule.assign_title_from == MailRule.TITLE_FROM_SUBJECT: | ||||||
|         return 0 |         title = message.subject | ||||||
|  |     elif rule.assign_title_from == MailRule.TITLE_FROM_FILENAME: | ||||||
|  |         title = os.path.splitext(os.path.basename(att.filename))[0] | ||||||
|  |     else: | ||||||
|  |         raise ValueError("Unknown title selector.") | ||||||
|  |  | ||||||
|  |     return title | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def get_correspondent(message, rule): | ||||||
|     if rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NOTHING: |     if rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_NOTHING: | ||||||
|         correspondent = None |         correspondent = None | ||||||
|     elif rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_EMAIL: |     elif rule.assign_correspondent_from == MailRule.CORRESPONDENT_FROM_EMAIL: | ||||||
| @@ -175,20 +186,22 @@ def handle_message(message, rule): | |||||||
|     else: |     else: | ||||||
|         raise ValueError("Unknwown correspondent selector") |         raise ValueError("Unknwown correspondent selector") | ||||||
|  |  | ||||||
|     tag = rule.assign_tag |     return correspondent | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def handle_message(message, rule): | ||||||
|  |     if not message.attachments: | ||||||
|  |         return 0 | ||||||
|  |  | ||||||
|  |     correspondent = get_correspondent(message, rule) | ||||||
|  |     tag = rule.assign_tag | ||||||
|     doc_type = rule.assign_document_type |     doc_type = rule.assign_document_type | ||||||
|  |  | ||||||
|     processed_attachments = 0 |     processed_attachments = 0 | ||||||
|  |  | ||||||
|     for att in message.attachments: |     for att in message.attachments: | ||||||
|  |  | ||||||
|         if rule.assign_title_from == MailRule.TITLE_FROM_SUBJECT: |         title = get_title(message, att, rule) | ||||||
|             title = message.subject |  | ||||||
|         elif rule.assign_title_from == MailRule.TITLE_FROM_FILENAME: |  | ||||||
|             title = att.filename |  | ||||||
|         else: |  | ||||||
|             raise ValueError("Unknown title selector.") |  | ||||||
|  |  | ||||||
|         # TODO: check with parsers what files types are supported |         # TODO: check with parsers what files types are supported | ||||||
|         if att.content_type == 'application/pdf': |         if att.content_type == 'application/pdf': | ||||||
|   | |||||||
| @@ -1,8 +1,71 @@ | |||||||
|  | from collections import namedtuple | ||||||
|  | from unittest import mock | ||||||
|  |  | ||||||
| from django.test import TestCase | from django.test import TestCase | ||||||
|  |  | ||||||
|  | from documents.models import Correspondent | ||||||
|  | from paperless_mail.mail import get_correspondent, get_title, handle_message | ||||||
|  | from paperless_mail.models import MailRule | ||||||
|  |  | ||||||
|  |  | ||||||
| class TestMail(TestCase): | class TestMail(TestCase): | ||||||
|  |  | ||||||
|     def testHandleMessage(self): |     def test_get_correspondent(self): | ||||||
|         # TODO: test me. |         message = namedtuple('MailMessage', []) | ||||||
|         pass |         message.from_ = "someone@somewhere.com" | ||||||
|  |         message.from_values = {'name': "Someone!", 'email': "someone@somewhere.com"} | ||||||
|  |  | ||||||
|  |         message2 = namedtuple('MailMessage', []) | ||||||
|  |         message2.from_ = "me@localhost.com" | ||||||
|  |         message2.from_values = {'name': "", 'email': "fake@localhost.com"} | ||||||
|  |  | ||||||
|  |         me_localhost = Correspondent.objects.create(name=message2.from_) | ||||||
|  |         someone_else = Correspondent.objects.create(name="someone else") | ||||||
|  |  | ||||||
|  |         rule = MailRule(assign_correspondent_from=MailRule.CORRESPONDENT_FROM_NOTHING) | ||||||
|  |         self.assertIsNone(get_correspondent(message, rule)) | ||||||
|  |  | ||||||
|  |         rule = MailRule(assign_correspondent_from=MailRule.CORRESPONDENT_FROM_EMAIL) | ||||||
|  |         c = get_correspondent(message, rule) | ||||||
|  |         self.assertIsNotNone(c) | ||||||
|  |         self.assertEqual(c.name, "someone@somewhere.com") | ||||||
|  |         c = get_correspondent(message2, rule) | ||||||
|  |         self.assertIsNotNone(c) | ||||||
|  |         self.assertEqual(c.name, "me@localhost.com") | ||||||
|  |         self.assertEqual(c.id, me_localhost.id) | ||||||
|  |  | ||||||
|  |         rule = MailRule(assign_correspondent_from=MailRule.CORRESPONDENT_FROM_NAME) | ||||||
|  |         c = get_correspondent(message, rule) | ||||||
|  |         self.assertIsNotNone(c) | ||||||
|  |         self.assertEqual(c.name, "Someone!") | ||||||
|  |         c = get_correspondent(message2, rule) | ||||||
|  |         self.assertIsNotNone(c) | ||||||
|  |         self.assertEqual(c.id, me_localhost.id) | ||||||
|  |  | ||||||
|  |         rule = MailRule(assign_correspondent_from=MailRule.CORRESPONDENT_FROM_CUSTOM, assign_correspondent=someone_else) | ||||||
|  |         c = get_correspondent(message, rule) | ||||||
|  |         self.assertEqual(c, someone_else) | ||||||
|  |  | ||||||
|  |     def test_get_title(self): | ||||||
|  |         message = namedtuple('MailMessage', []) | ||||||
|  |         message.subject = "the message title" | ||||||
|  |         att = namedtuple('Attachment', []) | ||||||
|  |         att.filename = "this_is_the_file.pdf" | ||||||
|  |         rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME) | ||||||
|  |         self.assertEqual(get_title(message, att, rule), "this_is_the_file") | ||||||
|  |         rule = MailRule(assign_title_from=MailRule.TITLE_FROM_SUBJECT) | ||||||
|  |         self.assertEqual(get_title(message, att, rule), "the message title") | ||||||
|  |  | ||||||
|  |     @mock.patch("django_q.tasks.async_task") | ||||||
|  |     def test_handle_message(self, m): | ||||||
|  |         message = namedtuple('MailMessage', []) | ||||||
|  |         message.subject = "the message title" | ||||||
|  |         att = namedtuple('Attachment', []) | ||||||
|  |         att.filename = "this_is_the_file.pdf" | ||||||
|  |         att.content_type = 'application/pdf' | ||||||
|  |         att.payload = b"attachment contents" | ||||||
|  |         message.attachments = [att] | ||||||
|  |  | ||||||
|  |         rule = MailRule(assign_title_from=MailRule.TITLE_FROM_FILENAME) | ||||||
|  |  | ||||||
|  |         #handle_message(message, rule) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonas Winkler
					Jonas Winkler