diff options
| author | Christophe Simonis <chs@odoo.com> | 2018-02-12 18:34:43 +0100 |
|---|---|---|
| committer | Christophe Simonis <chs@odoo.com> | 2018-02-12 18:34:43 +0100 |
| commit | adeea2a88db4443b2b426877a28bb1471c2f5db1 (patch) | |
| tree | 2f43fecc9649947d3061e8c482219fa167b3385a /addons/mail/models | |
| parent | 317f5f11112266128ac2292ecfafcd92cda74fd0 (diff) | |
| parent | 0f02dcb63918228d48df50f2d5805b7fda25c14a (diff) | |
[MERGE] forward port branch saas-11.2 up to 0f02dcb6391
Diffstat (limited to 'addons/mail/models')
| -rw-r--r-- | addons/mail/models/mail_message.py | 5 | ||||
| -rw-r--r-- | addons/mail/models/mail_template.py | 39 | ||||
| -rw-r--r-- | addons/mail/models/mail_thread.py | 35 | ||||
| -rw-r--r-- | addons/mail/models/res_partner.py | 7 |
4 files changed, 41 insertions, 45 deletions
diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 0d855dd15de..724f78ed827 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -749,9 +749,10 @@ class Message(models.Model): 'datas_fname': name, 'res_model': 'mail.message', }) + attachment.generate_access_token() values['attachment_ids'].append((4, attachment.id)) - data_to_url[key] = '/web/image/%s' % attachment.id - return '%s%s alt="%s"' % (data_to_url[key], match.group(3), name) + data_to_url[key] = ['/web/image/%s?access_token=%s' % (attachment.id, attachment.access_token), name] + return '%s%s alt="%s"' % (data_to_url[key][0], match.group(3), data_to_url[key][1]) values['body'] = _image_dataurl.sub(base64_to_boundary, tools.ustr(values['body'])) # delegate creation of tracking after the create as sudo to avoid access rights issues diff --git a/addons/mail/models/mail_template.py b/addons/mail/models/mail_template.py index 2407e58fdbd..17cea52deac 100644 --- a/addons/mail/models/mail_template.py +++ b/addons/mail/models/mail_template.py @@ -315,45 +315,8 @@ class MailTemplate(models.Model): # ---------------------------------------- @api.model - def _replace_local_links(self, html): - """ Post-processing of html content to replace local links to absolute - links, using web.base.url as base url. """ - if not html: - return html - - # form a tree - root = lxml.html.fromstring(html) - if not len(root) and root.text is None and root.tail is None: - html = u'<div>%s</div>' % html - root = lxml.html.fromstring(html, encoding='unicode') - - base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') - base = urls.url_parse(base_url) - - def _process_link(url): - new_url = urls.url_parse(url) - if new_url.scheme and (new_url.netloc or new_url.scheme == 'mailto'): - return url - return new_url.replace(scheme=base.scheme, netloc=base.netloc).to_url() - - # check all nodes, replace : - # - img src -> check URL - # - a href -> check URL - for node in root.iter(): - if node.tag == 'a' and node.get('href'): - node.set('href', _process_link(node.get('href'))) - elif node.tag == 'img' and not node.get('src', 'data').startswith(u'data'): - node.set('src', _process_link(node.get('src'))) - - html = lxml.html.tostring(root, pretty_print=False, method='html', encoding='unicode') - # this is ugly, but lxml/etree tostring want to put everything in a 'div' that breaks the editor -> remove that - if html.startswith(u'<div>') and html.endswith(u'</div>'): - html = html[5:-6] - return html - - @api.model def render_post_process(self, html): - html = self._replace_local_links(html) + html = self.env['mail.thread']._replace_local_links(html) return html @api.model diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 4ec3af5159e..32639ae75a9 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -23,9 +23,10 @@ from email.message import Message from email.utils import formataddr from lxml import etree from werkzeug import url_encode +from werkzeug import urls from odoo import _, api, exceptions, fields, models, tools -from odoo.tools import pycompat +from odoo.tools import pycompat, ustr from odoo.tools.safe_eval import safe_eval @@ -374,6 +375,38 @@ class MailThread(models.AbstractModel): # Technical methods / wrappers / 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 = 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 diff --git a/addons/mail/models/res_partner.py b/addons/mail/models/res_partner.py index 6999acc015b..4cee80af16a 100644 --- a/addons/mail/models/res_partner.py +++ b/addons/mail/models/res_partner.py @@ -73,17 +73,15 @@ class Partner(models.Model): if message.res_id and message.model in self.env: record = self.env[message.model].browse(message.res_id) - company = user.company_id; + company = user.company_id if record and hasattr(record, 'company_id'): - company = record.company_id; - company_name = company.name; + company = record.company_id return { 'message': message, 'signature': signature, 'website_url': website_url, 'company': company, - 'company_name': company_name, 'model_name': model_name, 'record': record, 'record_name': record_name, @@ -187,6 +185,7 @@ class Partner(models.Model): 'subject': message.subject or (message.record_name and 'Re: %s' % message.record_name), 'body': base_template.render(template_ctx, engine='ir.qweb'), } + fol_values['body'] = self.env['mail.thread']._replace_local_links(fol_values['body']) # send email new_emails, new_recipients_nbr = self._notify_send(fol_values['body'], fol_values['subject'], recipient_template_values['recipients'], **base_mail_values) # update notifications |
