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 /addons/mail/models/mail_render_mixin.py | |
| 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
Diffstat (limited to 'addons/mail/models/mail_render_mixin.py')
| -rw-r--r-- | addons/mail/models/mail_render_mixin.py | 39 |
1 files changed, 38 insertions, 1 deletions
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 |
