1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2014-Today OpenERP SA (<http://www.openerp.com>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
25 from openerp.osv import osv, fields
26 from openerp.tools.translate import _
29 class EmailTemplate(osv.Model):
30 _inherit = 'email.template'
32 def _get_website_link(self, cr, uid, ids, name, args, context=None):
33 return dict((id, _('<a href="website_mail/email_designer/%s">Open with visual editor</a>') % id) for id in ids)
36 'website_link': fields.function(
37 _get_website_link, type='text',
38 string='Website Link',
39 help='Link to the website',
43 def _postprocess_html_replace_links(self, cr, uid, body_html, context=None):
44 """ Post-processing of body_html. Indeed the content generated by the
45 website builder contains references to local addresses, for example
46 for images. This method changes those addresses to absolute addresses. """
52 root = lxml.html.fromstring(html)
53 if not len(root) and root.text is None and root.tail is None:
54 html = '<div>%s</div>' % html
55 root = lxml.html.fromstring(html)
57 base_url = self.pool['ir.config_parameter'].get_param(cr, uid, 'web.base.url')
58 (base_scheme, base_netloc, bpath, bparams, bquery, bfragment) = urlparse.urlparse(base_url)
60 def _process_link(url):
62 (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(url)
63 if not scheme and not netloc:
64 new_url = urlparse.urlunparse((base_scheme, base_netloc, path, params, query, fragment))
67 # check all nodes, replace :
68 # - img src -> check URL
69 # - a href -> check URL
70 for node in root.iter():
72 node.set('href', _process_link(node.get('href')))
73 elif node.tag == 'img' and not node.get('src', 'data').startswith('data'):
74 node.set('src', _process_link(node.get('src')))
76 html = lxml.html.tostring(root, pretty_print=False, method='html')
77 # this is ugly, but lxml/etree tostring want to put everything in a 'div' that breaks the editor -> remove that
78 if html.startswith('<div>') and html.endswith('</div>'):
82 def create(self, cr, uid, values, context=None):
83 if 'body_html' in values:
84 values['body_html'] = self._postprocess_html_replace_links(cr, uid, values['body_html'], context=context)
85 return super(EmailTemplate, self).create(cr, uid, values, context=context)
87 def write(self, cr, uid, ids, values, context=None):
88 if 'body_html' in values:
89 values['body_html'] = self._postprocess_html_replace_links(cr, uid, values['body_html'], context=context)
90 return super(EmailTemplate, self).write(cr, uid, ids, values, context=context)