6d84f91e5ab52b1e774e17c84452e0eddb63b206
[odoo/odoo.git] / addons / website_mail / models / email_template.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2014-Today OpenERP SA (<http://www.openerp.com>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import lxml
23 import urlparse
24
25 from openerp.osv import osv, fields
26 from openerp.tools.translate import _
27
28
29 class EmailTemplate(osv.Model):
30     _inherit = 'email.template'
31
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)
34
35     _columns = {
36         'website_link': fields.function(
37             _get_website_link, type='text',
38             string='Website Link',
39             help='Link to the website',
40         ),
41     }
42
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. """
47         html = body_html
48         if not body_html:
49             return html
50
51         # form a tree
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)
56
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)
59
60         def _process_link(url):
61             new_url = 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))
65             return new_url
66
67         # check all nodes, replace :
68         # - img src -> check URL
69         # - a href -> check URL
70         for node in root.iter():
71             if node.tag == 'a':
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')))
75
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>'):
79             html = html[5:-6]
80         return html
81
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)
86
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)