[IMP] mail_outbound_static: black, isort, prettier

This commit is contained in:
Frédéric Garbely 2020-06-30 16:21:11 +02:00 committed by Maksym Yankin
parent 29dad4da0f
commit d88b08eefc
3 changed files with 55 additions and 63 deletions

View File

@ -2,19 +2,15 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
{ {
'name': 'Mail Outbound Static', "name": "Mail Outbound Static",
'summary': 'Allows you to configure the from header for a mail server.', "summary": "Allows you to configure the from header for a mail server.",
'version': '13.0.1.0.0', "version": "13.0.1.0.0",
'category': 'Discuss', "category": "Discuss",
'website': 'https://github.com/OCA/social', "website": "https://github.com/OCA/social",
'author': 'brain-tec AG, LasLabs, Odoo Community Association (OCA)', "author": "brain-tec AG, LasLabs, Odoo Community Association (OCA)",
'license': 'LGPL-3', "license": "LGPL-3",
'application': False, "application": False,
'installable': True, "installable": True,
'depends': [ "depends": ["base"],
'base', "data": ["views/ir_mail_server_view.xml"],
],
'data': [
'views/ir_mail_server_view.xml',
],
} }

View File

@ -6,43 +6,42 @@ from odoo import api, fields, models
class IrMailServer(models.Model): class IrMailServer(models.Model):
_inherit = 'ir.mail_server' _inherit = "ir.mail_server"
smtp_from = fields.Char( smtp_from = fields.Char(
string='Email From', string="Email From", help="Set this in order to email from a specific address."
help='Set this in order to email from a specific address.'
) )
@api.model @api.model
def send_email(self, message, mail_server_id=None, smtp_server=None, def send_email(
*args, **kwargs): self, message, mail_server_id=None, smtp_server=None, *args, **kwargs
):
# Replicate logic from core to get mail server # Replicate logic from core to get mail server
mail_server = None mail_server = None
if mail_server_id: if mail_server_id:
mail_server = self.sudo().browse(mail_server_id) mail_server = self.sudo().browse(mail_server_id)
elif not smtp_server: 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: 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: if len(split_from) > 1:
email_from = '%s <%s>' % ( email_from = "{} <{}>".format(split_from[0], mail_server.smtp_from)
split_from[0], mail_server.smtp_from,
)
else: else:
email_from = mail_server.smtp_from email_from = mail_server.smtp_from
message.replace_header('From', email_from) message.replace_header("From", email_from)
bounce_alias = self.env['ir.config_parameter'].get_param( bounce_alias = self.env["ir.config_parameter"].get_param(
"mail.bounce.alias") "mail.bounce.alias"
)
if not bounce_alias: if not bounce_alias:
# then, bounce handling is disabled and we want # then, bounce handling is disabled and we want
# Return-Path = From # Return-Path = From
if 'Return-Path' in message: if "Return-Path" in message:
message.replace_header('Return-Path', email_from) message.replace_header("Return-Path", email_from)
else: else:
message.add_header('Return-Path', email_from) message.add_header("Return-Path", email_from)
return super(IrMailServer, self).send_email( return super(IrMailServer, self).send_email(
message, mail_server_id, smtp_server, *args, **kwargs message, mail_server_id, smtp_server, *args, **kwargs

View File

@ -3,30 +3,31 @@
import os import os
import threading import threading
from email import message_from_string
from mock import MagicMock from mock import MagicMock
from email import message_from_string
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
class TestIrMailServer(TransactionCase): class TestIrMailServer(TransactionCase):
def setUp(self): def setUp(self):
super(TestIrMailServer, self).setUp() super(TestIrMailServer, self).setUp()
self.email_from = 'derp@example.com' self.email_from = "derp@example.com"
self.email_from_another = 'another@example.com' self.email_from_another = "another@example.com"
self.Model = self.env['ir.mail_server'] self.Model = self.env["ir.mail_server"]
self.parameter_model = self.env['ir.config_parameter'] self.parameter_model = self.env["ir.config_parameter"]
self.Model.create({ self.Model.create(
'name': 'localhost', {
'smtp_host': 'localhost', "name": "localhost",
'smtp_from': self.email_from, "smtp_host": "localhost",
}) "smtp_from": self.email_from,
message_file = os.path.join( }
os.path.dirname(os.path.realpath(__file__)), 'test.msg',
) )
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()) self.message = message_from_string(fh.read())
def _send_mail(self, message=None, mail_server_id=None, smtp_server=None): def _send_mail(self, message=None, mail_server_id=None, smtp_server=None):
@ -34,24 +35,24 @@ class TestIrMailServer(TransactionCase):
message = self.message message = self.message
connect = MagicMock() connect = MagicMock()
thread = threading.currentThread() thread = threading.currentThread()
setattr(thread, 'testing', False) thread.testing = False
try: try:
self.Model._patch_method('connect', connect) self.Model._patch_method("connect", connect)
try: try:
self.Model.send_email(message, mail_server_id, smtp_server) self.Model.send_email(message, mail_server_id, smtp_server)
finally: finally:
self.Model._revert_method('connect') self.Model._revert_method("connect")
finally: finally:
setattr(thread, 'testing', True) thread.testing = True
send_from, send_to, message_string = connect().sendmail.call_args[0] send_from, send_to, message_string = connect().sendmail.call_args[0]
return message_from_string(message_string) return message_from_string(message_string)
def test_send_email_injects_from_no_canonical(self): def test_send_email_injects_from_no_canonical(self):
"""It should inject the FROM header correctly when no canonical name. """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() 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): def test_send_email_injects_from_with_canonical(self):
"""It should inject the FROM header correctly with a canonical name. """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 Note that there is an extra `<` in the canonical name to test for
proper handling in the split. proper handling in the split.
""" """
user = 'Test < User' user = "Test < User"
self.message.replace_header('From', '%s <test@example.com>' % user) self.message.replace_header("From", "%s <test@example.com>" % user)
bounce_parameter = self.parameter_model.search([ bounce_parameter = self.parameter_model.search(
('key', '=', 'mail.bounce.alias')]) [("key", "=", "mail.bounce.alias")]
)
if bounce_parameter: if bounce_parameter:
# Remove mail.bounce.alias to test Return-Path # Remove mail.bounce.alias to test Return-Path
bounce_parameter.unlink() bounce_parameter.unlink()
# Also check passing mail_server_id # Also check passing mail_server_id
mail_server_id = self.Model.sudo().search( mail_server_id = self.Model.sudo().search([], order="sequence", limit=1)[0].id
[], order='sequence', limit=1)[0].id
message = self._send_mail(mail_server_id=mail_server_id) message = self._send_mail(mail_server_id=mail_server_id)
self.assertEqual(message["From"], "{} <{}>".format(user, self.email_from))
self.assertEqual( self.assertEqual(
message['From'], message["Return-Path"], "{} <{}>".format(user, self.email_from)
'%s <%s>' % (user, self.email_from),
)
self.assertEqual(
message['Return-Path'],
'%s <%s>' % (user, self.email_from),
) )