[REF] mail: simplify email send code and remove unnecessary methods

Send methods of mail.mail model are set private and simplified. Those
methods have been written when a lot of overrides were necessary to
generate notification emails. Since then the flow has changed but
methods have not been updated completely. This commit therefore simplified
a bit the call chain and parameters.

This commit is purely technical and does not hold any functional change.
Purpose is to clean code in order to prepare future changes in mail
application.
This commit is contained in:
Thibault Delavallée
2018-02-26 14:15:08 +01:00
parent d46e358d3e
commit 6eda1dcb7a
4 changed files with 20 additions and 29 deletions
+11 -20
View File
@@ -152,39 +152,30 @@ class MailMail(models.Model):
# ------------------------------------------------------
@api.multi
def send_get_mail_body(self, partner=None):
def _send_prepare_body(self):
"""Return a specific ir_email body. The main purpose of this method
is to be inherited to add custom content depending on some module."""
self.ensure_one()
body = self.body_html or ''
return body
return self.body_html or ''
@api.multi
def send_get_mail_to(self, partner=None):
"""Forge the email_to with the following heuristic:
- if 'partner', recipient specific (Partner Name <email>)
- else fallback on mail.email_to splitting """
self.ensure_one()
if partner:
email_to = [formataddr((partner.name or 'False', partner.email or 'False'))]
else:
email_to = tools.email_split_and_format(self.email_to)
return email_to
@api.multi
def send_get_email_dict(self, partner=None):
def _send_prepare_values(self, partner=None):
"""Return a dictionary for specific email values, depending on a
partner, or generic to the whole recipients given by mail.email_to.
:param Model partner: specific recipient partner
"""
self.ensure_one()
body = self.send_get_mail_body(partner=partner)
body = self._send_prepare_body()
body_alternative = tools.html2plaintext(body)
if partner:
email_to = [formataddr((partner.name or 'False', partner.email or 'False'))]
else:
email_to = tools.email_split_and_format(self.email_to)
res = {
'body': body,
'body_alternative': body_alternative,
'email_to': self.send_get_mail_to(partner=partner),
'email_to': email_to,
}
return res
@@ -265,9 +256,9 @@ class MailMail(models.Model):
# specific behavior to customize the send email for notified partners
email_list = []
if mail.email_to:
email_list.append(mail.send_get_email_dict())
email_list.append(mail._send_prepare_values())
for partner in mail.recipient_ids:
email_list.append(mail.send_get_email_dict(partner=partner))
email_list.append(mail._send_prepare_values(partner=partner))
# headers
headers = {}
+6 -6
View File
@@ -26,7 +26,7 @@ class MailMail(models.Model):
mail_sudo.statistics_ids.write({'message_id': mail_sudo.message_id, 'state': 'outgoing'})
return mail
def _get_tracking_url(self, partner=None):
def _get_tracking_url(self):
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
track_url = werkzeug.urls.url_join(
base_url, 'mail/track/%(mail_id)s/blank.gif?%(params)s' % {
@@ -53,12 +53,12 @@ class MailMail(models.Model):
return url
@api.multi
def send_get_mail_body(self, partner=None):
def _send_prepare_body(self):
""" Override to add the tracking URL to the body and to add
Statistic_id in shorted urls """
# TDE: temporary addition (mail was parameter) due to semi-new-API
self.ensure_one()
body = super(MailMail, self).send_get_mail_body(partner=partner)
body = super(MailMail, self)._send_prepare_body()
if self.mailing_id and body and self.statistics_ids:
for match in re.findall(URL_REGEX, self.body_html):
@@ -75,15 +75,15 @@ class MailMail(models.Model):
# generate tracking URL
if self.statistics_ids:
tracking_url = self._get_tracking_url(partner)
tracking_url = self._get_tracking_url()
if tracking_url:
body = tools.append_content_to_html(body, tracking_url, plaintext=False, container_tag='div')
return body
@api.multi
def send_get_email_dict(self, partner=None):
def _send_prepare_values(self, partner=None):
# TDE: temporary addition (mail was parameter) due to semi-new-API
res = super(MailMail, self).send_get_email_dict(partner)
res = super(MailMail, self)._send_prepare_values(partner)
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
if self.mailing_id and res.get('body') and res.get('email_to'):
emails = tools.email_split(res.get('email_to')[0])
+1 -1
View File
@@ -230,7 +230,7 @@ class TestHeavyMailPerformance(TransactionCase):
})
mail_ids = mail.ids
with self.assertQueryCount(admin=16, emp=24): # com runbot 16 - 24 // test_mail only: 13 - 21
with self.assertQueryCount(admin=15, emp=23): # test_mail only: 13 - 21
self.env['mail.mail'].browse(mail_ids).send()
self.assertEqual(mail.body_html, '<p>Test</p>')
@@ -9,7 +9,7 @@ class MailMail(models.Model):
_inherit = 'mail.mail'
@api.multi
def send_get_mail_body(self, partner=None):
def _send_prepare_body(self):
""" Short-circuit parent method for mail groups, replace the default
footer with one appropriate for mailing-lists."""
if self.model == 'mail.channel' and self.res_id:
@@ -32,4 +32,4 @@ class MailMail(models.Model):
body = tools.append_content_to_html(self.body, footer, container_tag='div')
return body
else:
return super(MailMail, self).send_get_mail_body(partner=partner)
return super(MailMail, self)._send_prepare_body()