From b32c7e2c9bf596807fe83d52b6763754de423045 Mon Sep 17 00:00:00 2001 From: Denis Valenchyts Date: Fri, 17 Apr 2020 13:40:28 +0300 Subject: [PATCH] [IMP] fetchmail_notify_error_to_sender: black, isort, prettier --- .../__manifest__.py | 32 +++---- .../data/email_template_data.xml | 14 +-- .../models/fetchmail.py | 8 +- .../models/mail_thread.py | 29 +++--- .../test_fetchmail_notify_error_to_sender.py | 89 ++++++++++--------- .../views/fetchmail_view.xml | 6 +- 6 files changed, 92 insertions(+), 86 deletions(-) diff --git a/fetchmail_notify_error_to_sender/__manifest__.py b/fetchmail_notify_error_to_sender/__manifest__.py index c68e034d2..6db92f57e 100644 --- a/fetchmail_notify_error_to_sender/__manifest__.py +++ b/fetchmail_notify_error_to_sender/__manifest__.py @@ -4,24 +4,16 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { - 'name': 'Fetchmail Notify Error to Sender', - 'summary': 'If fetching mails gives error, send an email to sender', - 'version': '12.0.1.0.0', - 'category': 'Tools', - 'author': - "Agile Business Group,Eficent,Odoo Community Association (OCA)", - 'website': 'https://github.com/OCA/server-tools', - 'license': 'AGPL-3', - 'depends': [ - 'fetchmail', - 'test_mail', - ], - 'data': [ - 'views/fetchmail_view.xml', - 'data/email_template_data.xml', - ], - 'qweb': [ - ], - 'installable': True, - 'application': False, + "name": "Fetchmail Notify Error to Sender", + "summary": "If fetching mails gives error, send an email to sender", + "version": "12.0.1.0.0", + "category": "Tools", + "author": "Agile Business Group,Eficent,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/server-tools", + "license": "AGPL-3", + "depends": ["fetchmail", "test_mail",], + "data": ["views/fetchmail_view.xml", "data/email_template_data.xml",], + "qweb": [], + "installable": True, + "application": False, } diff --git a/fetchmail_notify_error_to_sender/data/email_template_data.xml b/fetchmail_notify_error_to_sender/data/email_template_data.xml index d85ba35c7..e20a9b7f0 100644 --- a/fetchmail_notify_error_to_sender/data/email_template_data.xml +++ b/fetchmail_notify_error_to_sender/data/email_template_data.xml @@ -5,12 +5,16 @@ Fetchmail - error notice ${ctx.get('sender_message').get('to')|safe} ${ctx.get('sender_message').get('from')|safe} - Receiving error with: ${ctx.get('sender_message').get('subject')|safe} - - - + Receiving error with: ${ctx.get('sender_message').get('subject')|safe} + + + ${ctx.get('lang')} -

Hello ${ctx.get('sender_message').get('from')},

we got a problem with your email: ${ctx.get('sender_message').get('subject')}

diff --git a/fetchmail_notify_error_to_sender/models/fetchmail.py b/fetchmail_notify_error_to_sender/models/fetchmail.py index 2becf8f2e..25658df99 100644 --- a/fetchmail_notify_error_to_sender/models/fetchmail.py +++ b/fetchmail_notify_error_to_sender/models/fetchmail.py @@ -7,9 +7,11 @@ from odoo import fields, models class FetchmailServer(models.Model): - _inherit = 'fetchmail.server' + _inherit = "fetchmail.server" error_notice_template_id = fields.Many2one( - 'mail.template', string="Error notice template", + "mail.template", + string="Error notice template", help="Set here the template to use to send notice to sender when " - "errors occur while fetching email") + "errors occur while fetching email", + ) diff --git a/fetchmail_notify_error_to_sender/models/mail_thread.py b/fetchmail_notify_error_to_sender/models/mail_thread.py index 1ab797fc2..0320145d3 100644 --- a/fetchmail_notify_error_to_sender/models/mail_thread.py +++ b/fetchmail_notify_error_to_sender/models/mail_thread.py @@ -7,26 +7,31 @@ from odoo import api, models class MailThread(models.AbstractModel): - _inherit = 'mail.thread' + _inherit = "mail.thread" @api.model - def message_route(self, message, message_dict, model=None, thread_id=None, - custom_values=None): + def message_route( + self, message, message_dict, model=None, thread_id=None, custom_values=None + ): try: res = super(MailThread, self).message_route( - message, message_dict, model=model, thread_id=thread_id, - custom_values=custom_values) + message, + message_dict, + model=model, + thread_id=thread_id, + custom_values=custom_values, + ) except ValueError as ve: - fetchmail_server_id = self.env.context.get('fetchmail_server_id') + fetchmail_server_id = self.env.context.get("fetchmail_server_id") if not fetchmail_server_id: raise ve - fetchmail_server = self.env['fetchmail.server'].with_context({ - 'sender_message': message, - 'route_exception': ve, - }).browse(fetchmail_server_id) + fetchmail_server = ( + self.env["fetchmail.server"] + .with_context({"sender_message": message, "route_exception": ve,}) + .browse(fetchmail_server_id) + ) if not fetchmail_server.error_notice_template_id: raise ve - fetchmail_server.error_notice_template_id.send_mail( - fetchmail_server.id) + fetchmail_server.error_notice_template_id.send_mail(fetchmail_server.id) raise ve return res diff --git a/fetchmail_notify_error_to_sender/tests/test_fetchmail_notify_error_to_sender.py b/fetchmail_notify_error_to_sender/tests/test_fetchmail_notify_error_to_sender.py index 47f58b412..d89fd0157 100644 --- a/fetchmail_notify_error_to_sender/tests/test_fetchmail_notify_error_to_sender.py +++ b/fetchmail_notify_error_to_sender/tests/test_fetchmail_notify_error_to_sender.py @@ -1,38 +1,47 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). import socket +from email.utils import formataddr + +from odoo.tools import mute_logger + from odoo.addons.test_mail.data.test_mail_data import MAIL_TEMPLATE from odoo.addons.test_mail.tests.test_mail_gateway import TestMailgateway -from odoo.tools import mute_logger -from email.utils import formataddr class TestFetchmailNotifyErrorToSender(TestMailgateway): - def setUp(self): super(TestFetchmailNotifyErrorToSender, self).setUp() - self.fetchmail_server = self.env['fetchmail.server'].create({ - 'name': 'Test Fetchmail Server', - 'type': 'imap', - 'error_notice_template_id': self.env.ref('%s.%s' % ( - 'fetchmail_notify_error_to_sender', - 'email_template_error_notice', - )).id - }) + self.fetchmail_server = self.env["fetchmail.server"].create( + { + "name": "Test Fetchmail Server", + "type": "imap", + "error_notice_template_id": self.env.ref( + "%s.%s" + % ( + "fetchmail_notify_error_to_sender", + "email_template_error_notice", + ) + ).id, + } + ) def format_and_process_with_context( - self, template, to_email='groups@example.com, other@gmail.com', - subject='Frogs', extra='', - email_from='Sylvie Lelitre ', - cc_email='', - msg_id='<1198923581.41972151344608186760.JavaMail@agrolait.com>', - model=None, target_model='mail.test.simple', target_field='name', + self, + template, + to_email="groups@example.com, other@gmail.com", + subject="Frogs", + extra="", + email_from="Sylvie Lelitre ", + cc_email="", + msg_id="<1198923581.41972151344608186760.JavaMail@agrolait.com>", + model=None, + target_model="mail.test.simple", + target_field="name", ctx=None, ): - self.assertFalse(self.env[target_model].search([ - (target_field, '=', subject), - ])) + self.assertFalse(self.env[target_model].search([(target_field, "=", subject),])) mail = template.format( to=to_email, subject=subject, @@ -41,39 +50,33 @@ class TestFetchmailNotifyErrorToSender(TestMailgateway): email_from=email_from, msg_id=msg_id, ) - self.env['mail.thread'].with_context(ctx or {}).message_process( - model, - mail, + self.env["mail.thread"].with_context(ctx or {}).message_process( + model, mail, ) - return self.env[target_model].search([(target_field, '=', subject)]) + return self.env[target_model].search([(target_field, "=", subject)]) - @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models') + @mute_logger("odoo.addons.mail.models.mail_thread", "odoo.models") def test_message_process(self): email_from = formataddr((self.partner_1.name, self.partner_1.email)) - count_return_mails_before = self.env['mail.mail'].search_count([ - ('email_to', '=', email_from), - ]) + count_return_mails_before = self.env["mail.mail"].search_count( + [("email_to", "=", email_from),] + ) with self.assertRaises(ValueError): self.format_and_process_with_context( MAIL_TEMPLATE, email_from=email_from, - to_email='noone@example.com', - subject='spam', - extra='In-Reply-To: <12321321-openerp-%d-mail.test.simple@%s' - '>' % (self.test_record.id, - socket.gethostname(), - ), - ctx={ - 'fetchmail_server_id': self.fetchmail_server.id, - } + to_email="noone@example.com", + subject="spam", + extra="In-Reply-To: <12321321-openerp-%d-mail.test.simple@%s" + ">" % (self.test_record.id, socket.gethostname(),), + ctx={"fetchmail_server_id": self.fetchmail_server.id,}, ) - count_return_mails_after = self.env['mail.mail'].search_count([ - ('email_to', '=', email_from), - ]) - self.assertEqual( - count_return_mails_after, - count_return_mails_before + 1, + count_return_mails_after = self.env["mail.mail"].search_count( + [("email_to", "=", email_from),] + ) + self.assertEqual( + count_return_mails_after, count_return_mails_before + 1, ) diff --git a/fetchmail_notify_error_to_sender/views/fetchmail_view.xml b/fetchmail_notify_error_to_sender/views/fetchmail_view.xml index 40d7ba9f4..fa25f1fac 100644 --- a/fetchmail_notify_error_to_sender/views/fetchmail_view.xml +++ b/fetchmail_notify_error_to_sender/views/fetchmail_view.xml @@ -1,12 +1,12 @@ - + fetchmail.server.form fetchmail.server - + - +