From d88b08eefc5d59a6b0d87b8d36110cfa363120f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Garbely?= Date: Tue, 30 Jun 2020 16:21:11 +0200 Subject: [PATCH] [IMP] mail_outbound_static: black, isort, prettier --- mail_outbound_static/__manifest__.py | 26 ++++---- mail_outbound_static/models/ir_mail_server.py | 31 +++++----- .../tests/test_ir_mail_server.py | 61 +++++++++---------- 3 files changed, 55 insertions(+), 63 deletions(-) diff --git a/mail_outbound_static/__manifest__.py b/mail_outbound_static/__manifest__.py index e6e370d..3c770c3 100644 --- a/mail_outbound_static/__manifest__.py +++ b/mail_outbound_static/__manifest__.py @@ -2,19 +2,15 @@ # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). { - 'name': 'Mail Outbound Static', - 'summary': 'Allows you to configure the from header for a mail server.', - 'version': '13.0.1.0.0', - 'category': 'Discuss', - 'website': 'https://github.com/OCA/social', - 'author': 'brain-tec AG, LasLabs, Odoo Community Association (OCA)', - 'license': 'LGPL-3', - 'application': False, - 'installable': True, - 'depends': [ - 'base', - ], - 'data': [ - 'views/ir_mail_server_view.xml', - ], + "name": "Mail Outbound Static", + "summary": "Allows you to configure the from header for a mail server.", + "version": "13.0.1.0.0", + "category": "Discuss", + "website": "https://github.com/OCA/social", + "author": "brain-tec AG, LasLabs, Odoo Community Association (OCA)", + "license": "LGPL-3", + "application": False, + "installable": True, + "depends": ["base"], + "data": ["views/ir_mail_server_view.xml"], } diff --git a/mail_outbound_static/models/ir_mail_server.py b/mail_outbound_static/models/ir_mail_server.py index e002b46..7ca6430 100644 --- a/mail_outbound_static/models/ir_mail_server.py +++ b/mail_outbound_static/models/ir_mail_server.py @@ -6,43 +6,42 @@ from odoo import api, fields, models class IrMailServer(models.Model): - _inherit = 'ir.mail_server' + _inherit = "ir.mail_server" smtp_from = fields.Char( - string='Email From', - help='Set this in order to email from a specific address.' + string="Email From", help="Set this in order to email from a specific address." ) @api.model - def send_email(self, message, mail_server_id=None, smtp_server=None, - *args, **kwargs): + def send_email( + self, message, mail_server_id=None, smtp_server=None, *args, **kwargs + ): # Replicate logic from core to get mail server mail_server = None if mail_server_id: mail_server = self.sudo().browse(mail_server_id) elif not smtp_server: - mail_server = self.sudo().search([], order='sequence', limit=1) + mail_server = self.sudo().search([], order="sequence", limit=1) if mail_server and mail_server.smtp_from: - split_from = message['From'].rsplit(' <', 1) + split_from = message["From"].rsplit(" <", 1) if len(split_from) > 1: - email_from = '%s <%s>' % ( - split_from[0], mail_server.smtp_from, - ) + email_from = "{} <{}>".format(split_from[0], mail_server.smtp_from) else: email_from = mail_server.smtp_from - message.replace_header('From', email_from) - bounce_alias = self.env['ir.config_parameter'].get_param( - "mail.bounce.alias") + message.replace_header("From", email_from) + bounce_alias = self.env["ir.config_parameter"].get_param( + "mail.bounce.alias" + ) if not bounce_alias: # then, bounce handling is disabled and we want # Return-Path = From - if 'Return-Path' in message: - message.replace_header('Return-Path', email_from) + if "Return-Path" in message: + message.replace_header("Return-Path", email_from) else: - message.add_header('Return-Path', email_from) + message.add_header("Return-Path", email_from) return super(IrMailServer, self).send_email( message, mail_server_id, smtp_server, *args, **kwargs diff --git a/mail_outbound_static/tests/test_ir_mail_server.py b/mail_outbound_static/tests/test_ir_mail_server.py index c0e1e25..ce8ec76 100644 --- a/mail_outbound_static/tests/test_ir_mail_server.py +++ b/mail_outbound_static/tests/test_ir_mail_server.py @@ -3,30 +3,31 @@ import os import threading +from email import message_from_string from mock import MagicMock -from email import message_from_string from odoo.tests.common import TransactionCase class TestIrMailServer(TransactionCase): - def setUp(self): super(TestIrMailServer, self).setUp() - self.email_from = 'derp@example.com' - self.email_from_another = 'another@example.com' - self.Model = self.env['ir.mail_server'] - self.parameter_model = self.env['ir.config_parameter'] - self.Model.create({ - 'name': 'localhost', - 'smtp_host': 'localhost', - 'smtp_from': self.email_from, - }) - message_file = os.path.join( - os.path.dirname(os.path.realpath(__file__)), 'test.msg', + self.email_from = "derp@example.com" + self.email_from_another = "another@example.com" + self.Model = self.env["ir.mail_server"] + self.parameter_model = self.env["ir.config_parameter"] + self.Model.create( + { + "name": "localhost", + "smtp_host": "localhost", + "smtp_from": self.email_from, + } ) - with open(message_file, 'r') as fh: + message_file = os.path.join( + os.path.dirname(os.path.realpath(__file__)), "test.msg" + ) + with open(message_file, "r") as fh: self.message = message_from_string(fh.read()) def _send_mail(self, message=None, mail_server_id=None, smtp_server=None): @@ -34,24 +35,24 @@ class TestIrMailServer(TransactionCase): message = self.message connect = MagicMock() thread = threading.currentThread() - setattr(thread, 'testing', False) + thread.testing = False try: - self.Model._patch_method('connect', connect) + self.Model._patch_method("connect", connect) try: self.Model.send_email(message, mail_server_id, smtp_server) finally: - self.Model._revert_method('connect') + self.Model._revert_method("connect") finally: - setattr(thread, 'testing', True) + thread.testing = True send_from, send_to, message_string = connect().sendmail.call_args[0] return message_from_string(message_string) def test_send_email_injects_from_no_canonical(self): """It should inject the FROM header correctly when no canonical name. """ - self.message.replace_header('From', 'test@example.com') + self.message.replace_header("From", "test@example.com") message = self._send_mail() - self.assertEqual(message['From'], self.email_from) + self.assertEqual(message["From"], self.email_from) def test_send_email_injects_from_with_canonical(self): """It should inject the FROM header correctly with a canonical name. @@ -59,22 +60,18 @@ class TestIrMailServer(TransactionCase): Note that there is an extra `<` in the canonical name to test for proper handling in the split. """ - user = 'Test < User' - self.message.replace_header('From', '%s ' % user) - bounce_parameter = self.parameter_model.search([ - ('key', '=', 'mail.bounce.alias')]) + user = "Test < User" + self.message.replace_header("From", "%s " % user) + bounce_parameter = self.parameter_model.search( + [("key", "=", "mail.bounce.alias")] + ) if bounce_parameter: # Remove mail.bounce.alias to test Return-Path bounce_parameter.unlink() # Also check passing mail_server_id - mail_server_id = self.Model.sudo().search( - [], order='sequence', limit=1)[0].id + mail_server_id = self.Model.sudo().search([], order="sequence", limit=1)[0].id message = self._send_mail(mail_server_id=mail_server_id) + self.assertEqual(message["From"], "{} <{}>".format(user, self.email_from)) self.assertEqual( - message['From'], - '%s <%s>' % (user, self.email_from), - ) - self.assertEqual( - message['Return-Path'], - '%s <%s>' % (user, self.email_from), + message["Return-Path"], "{} <{}>".format(user, self.email_from) )