[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).
{
'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,
}

View File

@ -5,12 +5,16 @@
<field name="name">Fetchmail - error notice</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="subject">Receiving error with: ${ctx.get('sender_message').get('subject')|safe}</field>
<field name="model_id" ref="fetchmail.model_fetchmail_server"/>
<field name="auto_delete" eval="True"/>
<field name="user_signature" eval="True"/>
<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="auto_delete" eval="True" />
<field name="user_signature" eval="True" />
<field name="lang">${ctx.get('lang')}</field>
<field name="body_html"><![CDATA[
<field
name="body_html"
><![CDATA[
<div>
<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>

View File

@ -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",
)

View File

@ -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

View File

@ -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 <test.sylvie.lelitre@agrolait.com>',
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 <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,
):
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,
)

View File

@ -1,12 +1,12 @@
<?xml version="1.0"?>
<?xml version="1.0" ?>
<odoo>
<record model="ir.ui.view" id="view_email_server_form">
<field name="name">fetchmail.server.form</field>
<field name="model">fetchmail.server</field>
<field name="inherit_id" ref="fetchmail.view_email_server_form"/>
<field name="inherit_id" ref="fetchmail.view_email_server_form" />
<field name="arch" type="xml">
<field name="active" position="after">
<field name="error_notice_template_id"/>
<field name="error_notice_template_id" />
</field>
</field>
</record>