diff options
| author | Thibault Delavallée <tde@odoo.com> | 2020-03-11 11:29:46 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2020-03-24 10:24:44 +0000 |
| commit | 3d86a29aeb21c7e5812c988866117a495f20ebed (patch) | |
| tree | c64ff0fd8da6f52c57ad3a38bbd022f48906b75e | |
| parent | 2835e0ba38f67f76b7f43948290050dcc68cd9db (diff) | |
[MOV] mail, various: move replace_local_links to render mixin
PURPOSE
Move links shortening tools on mail.render.mixin to have rendering and
post procesing tools available on that mixin.
LINKS
Task ID 1963529
Community PR odoo/odoo#32397
| -rw-r--r-- | addons/hr_expense/models/hr_expense.py | 2 | ||||
| -rw-r--r-- | addons/im_livechat/models/mail_channel.py | 2 | ||||
| -rw-r--r-- | addons/mail/models/mail_render_mixin.py | 39 | ||||
| -rw-r--r-- | addons/mail/models/mail_template.py | 2 | ||||
| -rw-r--r-- | addons/mail/models/mail_thread.py | 36 | ||||
| -rw-r--r-- | addons/mass_mailing/models/mail_mail.py | 2 | ||||
| -rw-r--r-- | addons/mass_mailing/wizard/mailing_mailing_test.py | 2 | ||||
| -rw-r--r-- | addons/payment_paypal/models/payment.py | 2 | ||||
| -rw-r--r-- | addons/survey/wizard/survey_invite.py | 2 | ||||
| -rw-r--r-- | addons/website_slides/wizard/slide_channel_invite.py | 2 |
10 files changed, 48 insertions, 43 deletions
diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index 1f77dcdc4d4..60a402c0015 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -647,7 +647,7 @@ class HrExpense(models.Model): mail_template_id = 'hr_expense.hr_expense_template_register' if expense.employee_id.user_id else 'hr_expense.hr_expense_template_register_no_user' expense_template = self.env.ref(mail_template_id) rendered_body = expense_template.render({'expense': expense}, engine='ir.qweb') - body = self.env['mail.thread']._replace_local_links(rendered_body) + body = self.env['mail.render.mixin']._replace_local_links(rendered_body) # TDE TODO: seems louche, check to use notify if expense.employee_id.user_id.partner_id: expense.message_post( diff --git a/addons/im_livechat/models/mail_channel.py b/addons/im_livechat/models/mail_channel.py index c7ca4098093..2a6f5548d66 100644 --- a/addons/im_livechat/models/mail_channel.py +++ b/addons/im_livechat/models/mail_channel.py @@ -184,7 +184,7 @@ class MailChannel(models.Model): } template = self.env.ref('im_livechat.livechat_email_template') mail_body = template.render(render_context, engine='ir.qweb', minimal_qcontext=True) - mail_body = self.env['mail.thread']._replace_local_links(mail_body) + mail_body = self.env['mail.render.mixin']._replace_local_links(mail_body) mail = self.env['mail.mail'].sudo().create({ 'subject': _('Conversation with %s') % self.livechat_operator_id.name, 'email_from': company.catchall_formatted or company.email_formatted, diff --git a/addons/mail/models/mail_render_mixin.py b/addons/mail/models/mail_render_mixin.py index 5804481f15c..ee989342b42 100644 --- a/addons/mail/models/mail_render_mixin.py +++ b/addons/mail/models/mail_render_mixin.py @@ -7,6 +7,7 @@ import datetime import dateutil.relativedelta as relativedelta import functools import logging +import re from werkzeug import urls @@ -147,6 +148,42 @@ class MailRenderMixin(models.AbstractModel): return expression # ------------------------------------------------------------ + # TOOLS + # ------------------------------------------------------------ + + def _replace_local_links(self, html, base_url=None): + """ Replace local links by absolute links. It is required in various + cases, for example when sending emails on chatter or sending mass + mailings. It replaces + + * href of links (mailto will not match the regex) + * src of images (base64 hardcoded data will not match the regex) + * styling using url like background-image: url + + It is done using regex because it is shorten than using an html parser + to create a potentially complex soupe and hope to have a result that + has not been harmed. + """ + if not html: + return html + + html = tools.ustr(html) + + def _sub_relative2absolute(match): + # compute here to do it only if really necessary + cache will ensure it is done only once + # if not base_url + if not _sub_relative2absolute.base_url: + _sub_relative2absolute.base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url") + return match.group(1) + urls.url_join(_sub_relative2absolute.base_url, match.group(2)) + + _sub_relative2absolute.base_url = base_url + html = re.sub(r"""(<img(?=\s)[^>]*\ssrc=")(/[^/][^"]+)""", _sub_relative2absolute, html) + html = re.sub(r"""(<a(?=\s)[^>]*\shref=")(/[^/][^"]+)""", _sub_relative2absolute, html) + html = re.sub(r"""(<[^>]+\bstyle="[^"]+\burl\('?)(/[^/'][^'")]+)""", _sub_relative2absolute, html) + + return html + + # ------------------------------------------------------------ # RENDERING # ------------------------------------------------------------ @@ -230,7 +267,7 @@ class MailRenderMixin(models.AbstractModel): :return dict: updated version of rendered """ for res_id, html in rendered.items(): - rendered[res_id] = self.env['mail.thread']._replace_local_links(html) + rendered[res_id] = self._replace_local_links(html) return rendered @api.model diff --git a/addons/mail/models/mail_template.py b/addons/mail/models/mail_template.py index 3fbe4b03e68..c4d590e2729 100644 --- a/addons/mail/models/mail_template.py +++ b/addons/mail/models/mail_template.py @@ -268,7 +268,7 @@ class MailTemplate(models.Model): 'record': record, } body = template.render(template_ctx, engine='ir.qweb', minimal_qcontext=True) - values['body_html'] = self.env['mail.thread']._replace_local_links(body) + values['body_html'] = self.env['mail.render.mixin']._replace_local_links(body) mail = self.env['mail.mail'].sudo().create(values) # manage attachments diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 300ab439cf9..43d705b1c79 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -476,38 +476,6 @@ class MailThread(models.AbstractModel): return self.with_context(lang=self.env.user.lang) return self - def _replace_local_links(self, html, base_url=None): - """ Replace local links by absolute links. It is required in various - cases, for example when sending emails on chatter or sending mass - mailings. It replaces - - * href of links (mailto will not match the regex) - * src of images (base64 hardcoded data will not match the regex) - * styling using url like background-image: url - - It is done using regex because it is shorten than using an html parser - to create a potentially complex soupe and hope to have a result that - has not been harmed. - """ - if not html: - return html - - html = ustr(html) - - def _sub_relative2absolute(match): - # compute here to do it only if really necessary + cache will ensure it is done only once - # if not base_url - if not _sub_relative2absolute.base_url: - _sub_relative2absolute.base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url") - return match.group(1) + urls.url_join(_sub_relative2absolute.base_url, match.group(2)) - - _sub_relative2absolute.base_url = base_url - html = re.sub(r"""(<img(?=\s)[^>]*\ssrc=")(/[^/][^"]+)""", _sub_relative2absolute, html) - html = re.sub(r"""(<a(?=\s)[^>]*\shref=")(/[^/][^"]+)""", _sub_relative2absolute, html) - html = re.sub(r"""(<[^>]+\bstyle="[^"]+\burl\('?)(/[^/'][^'")]+)""", _sub_relative2absolute, html) - - return html - @api.model def _garbage_collect_attachments(self): """ Garbage collect lost mail attachments. Those are attachments @@ -2322,7 +2290,7 @@ class MailThread(models.AbstractModel): mail_body = base_template.render(render_values, engine='ir.qweb', minimal_qcontext=True) else: mail_body = message.body - mail_body = self._replace_local_links(mail_body) + mail_body = self.env['mail.render.mixin']._replace_local_links(mail_body) # create email for recipients_ids_chunk in split_every(recipients_max, recipients_ids): @@ -2899,7 +2867,7 @@ class MailThread(models.AbstractModel): 'access_link': self._notify_get_action_link('view'), } assignation_msg = view.render(values, engine='ir.qweb', minimal_qcontext=True) - assignation_msg = self.env['mail.thread']._replace_local_links(assignation_msg) + assignation_msg = self.env['mail.render.mixin']._replace_local_links(assignation_msg) record.message_notify( subject=_('You have been assigned to %s') % record.display_name, body=assignation_msg, diff --git a/addons/mass_mailing/models/mail_mail.py b/addons/mass_mailing/models/mail_mail.py index 749c6dc6133..68a653a38db 100644 --- a/addons/mass_mailing/models/mail_mail.py +++ b/addons/mass_mailing/models/mail_mail.py @@ -69,7 +69,7 @@ class MailMail(models.Model): if tracking_url: body = tools.append_content_to_html(body, tracking_url, plaintext=False, container_tag='div') - body = self.env['mail.thread']._replace_local_links(body) + body = self.env['mail.render.mixin']._replace_local_links(body) return body diff --git a/addons/mass_mailing/wizard/mailing_mailing_test.py b/addons/mass_mailing/wizard/mailing_mailing_test.py index dab0f70f725..45de0d88019 100644 --- a/addons/mass_mailing/wizard/mailing_mailing_test.py +++ b/addons/mass_mailing/wizard/mailing_mailing_test.py @@ -24,7 +24,7 @@ class TestMassMailing(models.TransientModel): mass_mail_layout = self.env.ref('mass_mailing.mass_mailing_mail_layout') for test_mail in test_emails: # Convert links in absolute URLs before the application of the shortener - body = self.env['mail.thread']._replace_local_links(mailing.body_html) + body = self.env['mail.render.mixin']._replace_local_links(mailing.body_html) body = tools.html_sanitize(body, sanitize_attributes=True, sanitize_style=True) mail_values = { 'email_from': mailing.email_from, diff --git a/addons/payment_paypal/models/payment.py b/addons/payment_paypal/models/payment.py index 38fdb2422ce..f3b99925c85 100644 --- a/addons/payment_paypal/models/payment.py +++ b/addons/payment_paypal/models/payment.py @@ -195,7 +195,7 @@ class TxPaypal(models.Model): render_template = template.render({ 'acquirer': self.acquirer_id, }, engine='ir.qweb') - mail_body = self.env['mail.thread']._replace_local_links(render_template) + mail_body = self.env['mail.render.mixin']._replace_local_links(render_template) mail_values = { 'body_html': mail_body, 'subject': _('Add your Paypal account to Odoo'), diff --git a/addons/survey/wizard/survey_invite.py b/addons/survey/wizard/survey_invite.py index 0068ecdb06c..7896cddfa0e 100644 --- a/addons/survey/wizard/survey_invite.py +++ b/addons/survey/wizard/survey_invite.py @@ -231,7 +231,7 @@ class SurveyInvite(models.TransientModel): 'company': self.env.company, } body = template.render(template_ctx, engine='ir.qweb', minimal_qcontext=True) - mail_values['body_html'] = self.env['mail.thread']._replace_local_links(body) + mail_values['body_html'] = self.env['mail.render.mixin']._replace_local_links(body) return self.env['mail.mail'].sudo().create(mail_values) diff --git a/addons/website_slides/wizard/slide_channel_invite.py b/addons/website_slides/wizard/slide_channel_invite.py index 28522e39c4b..9650b4d2f9e 100644 --- a/addons/website_slides/wizard/slide_channel_invite.py +++ b/addons/website_slides/wizard/slide_channel_invite.py @@ -124,6 +124,6 @@ class SlideChannelInvite(models.TransientModel): 'company': self.env.company, } body = template.render(template_ctx, engine='ir.qweb', minimal_qcontext=True) - mail_values['body_html'] = self.env['mail.thread']._replace_local_links(body) + mail_values['body_html'] = self.env['mail.render.mixin']._replace_local_links(body) return mail_values |
