f422dae7b2030e32879ad3746a90a1ff836564d1
[odoo/odoo.git] / addons / website / models / ir_ui_view.py
1 # -*- coding: utf-8 -*-
2 import copy
3 import re
4 import simplejson
5 import werkzeug
6
7 from lxml import etree, html
8
9 from openerp.addons.website.models import website
10 from openerp.http import request
11 from openerp.osv import osv, fields
12
13 class view(osv.osv):
14     _inherit = "ir.ui.view"
15     _columns = {
16         'inherit_option_id': fields.many2one('ir.ui.view','Optional Inheritancy'),
17         'inherited_option_ids': fields.one2many('ir.ui.view','inherit_option_id','Optional Inheritancies'),
18         'page': fields.boolean("Whether this view is a web page template (complete)"),
19         'website_meta_title': fields.char("Website meta title", size=70, translate=True),
20         'website_meta_description': fields.text("Website meta description", size=160, translate=True),
21         'website_meta_keywords': fields.char("Website meta keywords", translate=True),
22     }
23     _defaults = {
24         'page': False,
25     }
26
27
28     def _view_obj(self, cr, uid, view_id, context=None):
29         if isinstance(view_id, basestring):
30             return self.pool['ir.model.data'].xmlid_to_object(
31                 cr, uid, view_id, raise_if_not_found=True, context=context
32             )
33         elif isinstance(view_id, (int, long)):
34             return self.browse(cr, uid, view_id, context=context)
35
36         # assume it's already a view object (WTF?)
37         return view_id
38
39     # Returns all views (called and inherited) related to a view
40     # Used by translation mechanism, SEO and optional templates
41     def _views_get(self, cr, uid, view_id, options=True, context=None, root=True):
42         """ For a given view ``view_id``, should return:
43
44         * the view itself
45         * all views inheriting from it, enabled or not
46           - but not the optional children of a non-enabled child
47         * all views called from it (via t-call)
48         """
49         try:
50             view = self._view_obj(cr, uid, view_id, context=context)
51         except ValueError:
52             # Shall we log that ?
53             return []
54
55         while root and view.inherit_id:
56             view = view.inherit_id
57
58         result = [view]
59
60         node = etree.fromstring(view.arch)
61         for child in node.xpath("//t[@t-call]"):
62             try:
63                 called_view = self._view_obj(cr, uid, child.get('t-call'), context=context)
64             except ValueError:
65                 continue
66             if called_view not in result:
67                 result += self._views_get(cr, uid, called_view, options=options, context=context)
68
69         extensions = set(view.inherit_children_ids)
70         if options:
71             extensions.update(view.inherited_option_ids)
72
73         # Keep options in a deterministic order regardless of their applicability
74         for extension in sorted(extensions, key=lambda v: v.id):
75             for r in self._views_get(
76                     cr, uid, extension,
77                     # only return optional grandchildren if this child is enabled
78                     options=bool(extension.inherit_id),
79                     context=context, root=False):
80                 if r not in result:
81                     result.append(r)
82         return result
83
84     def extract_embedded_fields(self, cr, uid, arch, context=None):
85         return arch.xpath('//*[@data-oe-model != "ir.ui.view"]')
86
87     def save_embedded_field(self, cr, uid, el, context=None):
88         Model = self.pool[el.get('data-oe-model')]
89         field = el.get('data-oe-field')
90
91         column = Model._all_columns[field].column
92         converter = self.pool['website.qweb'].get_converter_for(
93             el.get('data-oe-type'))
94         value = converter.from_html(cr, uid, Model, column, el)
95
96         if value is not None:
97             # TODO: batch writes?
98             Model.write(cr, uid, [int(el.get('data-oe-id'))], {
99                 field: value
100             }, context=context)
101
102     def to_field_ref(self, cr, uid, el, context=None):
103         # filter out meta-information inserted in the document
104         attributes = dict((k, v) for k, v in el.items()
105                           if not k.startswith('data-oe-'))
106         attributes['t-field'] = el.get('data-oe-expression')
107
108         out = html.html_parser.makeelement(el.tag, attrib=attributes)
109         out.tail = el.tail
110         return out
111
112     def replace_arch_section(self, cr, uid, view_id, section_xpath, replacement, context=None):
113         # the root of the arch section shouldn't actually be replaced as it's
114         # not really editable itself, only the content truly is editable.
115
116         [view] = self.browse(cr, uid, [view_id], context=context)
117         arch = etree.fromstring(view.arch.encode('utf-8'))
118         # => get the replacement root
119         if not section_xpath:
120             root = arch
121         else:
122             # ensure there's only one match
123             [root] = arch.xpath(section_xpath)
124
125         root.text = replacement.text
126         root.tail = replacement.tail
127         # replace all children
128         del root[:]
129         for child in replacement:
130             root.append(copy.deepcopy(child))
131
132         return arch
133
134     def render(self, cr, uid, id_or_xml_id, values=None, engine='ir.qweb', context=None):
135         if request and getattr(request, 'website_enabled', False):
136             engine='website.qweb'
137
138             if isinstance(id_or_xml_id, list):
139                 id_or_xml_id = id_or_xml_id[0]
140
141             if not context:
142                 context = {}
143
144             qcontext = dict(
145                 context.copy(),
146                 website=request.website,
147                 url_for=website.url_for,
148                 slug=website.slug,
149                 res_company=request.website.company_id,
150                 user_id=self.pool.get("res.users").browse(cr, uid, uid),
151                 translatable=context.get('lang') != request.website.default_lang_code,
152                 editable=request.website.is_publisher(),
153             )
154
155             # add some values
156             if values:
157                 qcontext.update(values)
158
159             # in edit mode ir.ui.view will tag nodes
160             context['inherit_branding'] = qcontext.get('editable', False)
161
162             view_obj = request.website.get_template(id_or_xml_id)
163             if 'main_object' not in qcontext:
164                 qcontext['main_object'] = view_obj
165
166             values = qcontext
167
168         return super(view, self).render(cr, uid, id_or_xml_id, values=values, engine=engine, context=context)
169
170     def _pretty_arch(self, arch):
171         # remove_blank_string does not seem to work on HTMLParser, and
172         # pretty-printing with lxml more or less requires stripping
173         # whitespace: http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output
174         # so serialize to XML, parse as XML (remove whitespace) then serialize
175         # as XML (pretty print)
176         arch_no_whitespace = etree.fromstring(
177             etree.tostring(arch, encoding='utf-8'),
178             parser=etree.XMLParser(encoding='utf-8', remove_blank_text=True))
179         return etree.tostring(
180             arch_no_whitespace, encoding='unicode', pretty_print=True)
181
182     def save(self, cr, uid, res_id, value, xpath=None, context=None):
183         """ Update a view section. The view section may embed fields to write
184
185         :param str model:
186         :param int res_id:
187         :param str xpath: valid xpath to the tag to replace
188         """
189         res_id = int(res_id)
190
191         arch_section = html.fromstring(
192             value, parser=html.HTMLParser(encoding='utf-8'))
193
194         if xpath is None:
195             # value is an embedded field on its own, not a view section
196             self.save_embedded_field(cr, uid, arch_section, context=context)
197             return
198
199         for el in self.extract_embedded_fields(cr, uid, arch_section, context=context):
200             self.save_embedded_field(cr, uid, el, context=context)
201
202             # transform embedded field back to t-field
203             el.getparent().replace(el, self.to_field_ref(cr, uid, el, context=context))
204
205         arch = self.replace_arch_section(cr, uid, res_id, xpath, arch_section, context=context)
206         self.write(cr, uid, res_id, {
207             'arch': self._pretty_arch(arch)
208         }, context=context)