[IMP] fetchmail_notify_error_to_sender: black, isort, prettier

pull/2286/head
Denis Valenchyts 2020-04-17 13:40:28 +03:00 committed by Jasmin Solanki
parent a775ebacbd
commit b32c7e2c9b
6 changed files with 92 additions and 86 deletions

View File

@ -4,24 +4,16 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{ {
'name': 'Fetchmail Notify Error to Sender', "name": "Fetchmail Notify Error to Sender",
'summary': 'If fetching mails gives error, send an email to sender', "summary": "If fetching mails gives error, send an email to sender",
'version': '12.0.1.0.0', "version": "12.0.1.0.0",
'category': 'Tools', "category": "Tools",
'author': "author": "Agile Business Group,Eficent,Odoo Community Association (OCA)",
"Agile Business Group,Eficent,Odoo Community Association (OCA)", "website": "https://github.com/OCA/server-tools",
'website': 'https://github.com/OCA/server-tools', "license": "AGPL-3",
'license': 'AGPL-3', "depends": ["fetchmail", "test_mail",],
'depends': [ "data": ["views/fetchmail_view.xml", "data/email_template_data.xml",],
'fetchmail', "qweb": [],
'test_mail', "installable": True,
], "application": False,
'data': [
'views/fetchmail_view.xml',
'data/email_template_data.xml',
],
'qweb': [
],
'installable': True,
'application': False,
} }

View File

@ -5,12 +5,16 @@
<field name="name">Fetchmail - error notice</field> <field name="name">Fetchmail - error notice</field>
<field name="email_from">${ctx.get('sender_message').get('to')|safe}</field> <field name="email_from">${ctx.get('sender_message').get('to')|safe}</field>
<field name="email_to">${ctx.get('sender_message').get('from')|safe}</field> <field name="email_to">${ctx.get('sender_message').get('from')|safe}</field>
<field name="subject">Receiving error with: ${ctx.get('sender_message').get('subject')|safe}</field> <field
name="subject"
>Receiving error with: ${ctx.get('sender_message').get('subject')|safe}</field>
<field name="model_id" ref="fetchmail.model_fetchmail_server" /> <field name="model_id" ref="fetchmail.model_fetchmail_server" />
<field name="auto_delete" eval="True" /> <field name="auto_delete" eval="True" />
<field name="user_signature" eval="True" /> <field name="user_signature" eval="True" />
<field name="lang">${ctx.get('lang')}</field> <field name="lang">${ctx.get('lang')}</field>
<field name="body_html"><![CDATA[ <field
name="body_html"
><![CDATA[
<div> <div>
<p>Hello ${ctx.get('sender_message').get('from')},</p> <p>Hello ${ctx.get('sender_message').get('from')},</p>
<p>we got a problem with your email: <i>${ctx.get('sender_message').get('subject')}</i></p> <p>we got a problem with your email: <i>${ctx.get('sender_message').get('subject')}</i></p>

View File

@ -7,9 +7,11 @@ from odoo import fields, models
class FetchmailServer(models.Model): class FetchmailServer(models.Model):
_inherit = 'fetchmail.server' _inherit = "fetchmail.server"
error_notice_template_id = fields.Many2one( 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 " help="Set here the template to use to send notice to sender when "
"errors occur while fetching email") "errors occur while fetching email",
)

View File

@ -7,26 +7,31 @@ from odoo import api, models
class MailThread(models.AbstractModel): class MailThread(models.AbstractModel):
_inherit = 'mail.thread' _inherit = "mail.thread"
@api.model @api.model
def message_route(self, message, message_dict, model=None, thread_id=None, def message_route(
custom_values=None): self, message, message_dict, model=None, thread_id=None, custom_values=None
):
try: try:
res = super(MailThread, self).message_route( res = super(MailThread, self).message_route(
message, message_dict, model=model, thread_id=thread_id, message,
custom_values=custom_values) message_dict,
model=model,
thread_id=thread_id,
custom_values=custom_values,
)
except ValueError as ve: 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: if not fetchmail_server_id:
raise ve raise ve
fetchmail_server = self.env['fetchmail.server'].with_context({ fetchmail_server = (
'sender_message': message, self.env["fetchmail.server"]
'route_exception': ve, .with_context({"sender_message": message, "route_exception": ve,})
}).browse(fetchmail_server_id) .browse(fetchmail_server_id)
)
if not fetchmail_server.error_notice_template_id: if not fetchmail_server.error_notice_template_id:
raise ve raise ve
fetchmail_server.error_notice_template_id.send_mail( fetchmail_server.error_notice_template_id.send_mail(fetchmail_server.id)
fetchmail_server.id)
raise ve raise ve
return res return res

View File

@ -1,38 +1,47 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import socket 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.data.test_mail_data import MAIL_TEMPLATE
from odoo.addons.test_mail.tests.test_mail_gateway import TestMailgateway 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): class TestFetchmailNotifyErrorToSender(TestMailgateway):
def setUp(self): def setUp(self):
super(TestFetchmailNotifyErrorToSender, self).setUp() super(TestFetchmailNotifyErrorToSender, self).setUp()
self.fetchmail_server = self.env['fetchmail.server'].create({ self.fetchmail_server = self.env["fetchmail.server"].create(
'name': 'Test Fetchmail Server', {
'type': 'imap', "name": "Test Fetchmail Server",
'error_notice_template_id': self.env.ref('%s.%s' % ( "type": "imap",
'fetchmail_notify_error_to_sender', "error_notice_template_id": self.env.ref(
'email_template_error_notice', "%s.%s"
)).id % (
}) "fetchmail_notify_error_to_sender",
"email_template_error_notice",
)
).id,
}
)
def format_and_process_with_context( def format_and_process_with_context(
self, template, to_email='groups@example.com, other@gmail.com', self,
subject='Frogs', extra='', template,
email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>', to_email="groups@example.com, other@gmail.com",
cc_email='', subject="Frogs",
msg_id='<1198923581.41972151344608186760.JavaMail@agrolait.com>', extra="",
model=None, target_model='mail.test.simple', target_field='name', email_from="Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>",
cc_email="",
msg_id="<1198923581.41972151344608186760.JavaMail@agrolait.com>",
model=None,
target_model="mail.test.simple",
target_field="name",
ctx=None, ctx=None,
): ):
self.assertFalse(self.env[target_model].search([ self.assertFalse(self.env[target_model].search([(target_field, "=", subject),]))
(target_field, '=', subject),
]))
mail = template.format( mail = template.format(
to=to_email, to=to_email,
subject=subject, subject=subject,
@ -41,39 +50,33 @@ class TestFetchmailNotifyErrorToSender(TestMailgateway):
email_from=email_from, email_from=email_from,
msg_id=msg_id, msg_id=msg_id,
) )
self.env['mail.thread'].with_context(ctx or {}).message_process( self.env["mail.thread"].with_context(ctx or {}).message_process(
model, model, mail,
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): def test_message_process(self):
email_from = formataddr((self.partner_1.name, self.partner_1.email)) email_from = formataddr((self.partner_1.name, self.partner_1.email))
count_return_mails_before = self.env['mail.mail'].search_count([ count_return_mails_before = self.env["mail.mail"].search_count(
('email_to', '=', email_from), [("email_to", "=", email_from),]
]) )
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.format_and_process_with_context( self.format_and_process_with_context(
MAIL_TEMPLATE, MAIL_TEMPLATE,
email_from=email_from, email_from=email_from,
to_email='noone@example.com', to_email="noone@example.com",
subject='spam', subject="spam",
extra='In-Reply-To: <12321321-openerp-%d-mail.test.simple@%s' extra="In-Reply-To: <12321321-openerp-%d-mail.test.simple@%s"
'>' % (self.test_record.id, ">" % (self.test_record.id, socket.gethostname(),),
socket.gethostname(), ctx={"fetchmail_server_id": self.fetchmail_server.id,},
),
ctx={
'fetchmail_server_id': self.fetchmail_server.id,
}
) )
count_return_mails_after = self.env['mail.mail'].search_count([ count_return_mails_after = self.env["mail.mail"].search_count(
('email_to', '=', email_from), [("email_to", "=", email_from),]
]) )
self.assertEqual( self.assertEqual(
count_return_mails_after, count_return_mails_after, count_return_mails_before + 1,
count_return_mails_before + 1,
) )