1 # -*- coding: utf-8 -*-
4 from lxml import etree, html
6 from openerp import SUPERUSER_ID
7 from openerp.addons.website.models import website
8 from openerp.http import request
9 from openerp.osv import osv, fields
12 _inherit = "ir.ui.view"
14 'page': fields.boolean("Whether this view is a web page template (complete)"),
15 'website_meta_title': fields.char("Website meta title", size=70, translate=True),
16 'website_meta_description': fields.text("Website meta description", size=160, translate=True),
17 'website_meta_keywords': fields.char("Website meta keywords", translate=True),
18 'customize_show': fields.boolean("Show As Optional Inherit"),
22 'customize_show': False,
26 def _view_obj(self, cr, uid, view_id, context=None):
27 if isinstance(view_id, basestring):
28 return self.pool['ir.model.data'].xmlid_to_object(
29 cr, uid, view_id, raise_if_not_found=True, context=context
31 elif isinstance(view_id, (int, long)):
32 return self.browse(cr, uid, view_id, context=context)
34 # assume it's already a view object (WTF?)
37 # Returns all views (called and inherited) related to a view
38 # Used by translation mechanism, SEO and optional templates
39 def _views_get(self, cr, uid, view_id, options=True, bundles=False, context=None, root=True):
40 """ For a given view ``view_id``, should return:
43 * all views inheriting from it, enabled or not
44 - but not the optional children of a non-enabled child
45 * all views called from it (via t-call)
48 view = self._view_obj(cr, uid, view_id, context=context)
53 while root and view.inherit_id:
54 view = view.inherit_id
58 node = etree.fromstring(view.arch)
59 xpath = "//t[@t-call]"
61 xpath += "| //t[@t-call-assets]"
62 for child in node.xpath(xpath):
64 called_view = self._view_obj(cr, uid, child.get('t-call', child.get('t-call-assets')), context=context)
67 if called_view not in result:
68 result += self._views_get(cr, uid, called_view, options=options, bundles=bundles, context=context)
70 extensions = view.inherit_children_ids
72 # only active children
73 extensions = (v for v in view.inherit_children_ids if v.active)
75 # Keep options in a deterministic order regardless of their applicability
76 for extension in sorted(extensions, key=lambda v: v.id):
77 for r in self._views_get(
79 # only return optional grandchildren if this child is enabled
80 options=extension.active,
81 context=context, root=False):
86 def extract_embedded_fields(self, cr, uid, arch, context=None):
87 return arch.xpath('//*[@data-oe-model != "ir.ui.view"]')
89 def save_embedded_field(self, cr, uid, el, context=None):
90 Model = self.pool[el.get('data-oe-model')]
91 field = el.get('data-oe-field')
93 column = Model._all_columns[field].column
94 converter = self.pool['website.qweb'].get_converter_for(
95 el.get('data-oe-type'))
96 value = converter.from_html(cr, uid, Model, column, el)
100 Model.write(cr, uid, [int(el.get('data-oe-id'))], {
104 def to_field_ref(self, cr, uid, el, context=None):
105 # filter out meta-information inserted in the document
106 attributes = dict((k, v) for k, v in el.items()
107 if not k.startswith('data-oe-'))
108 attributes['t-field'] = el.get('data-oe-expression')
110 out = html.html_parser.makeelement(el.tag, attrib=attributes)
114 def replace_arch_section(self, cr, uid, view_id, section_xpath, replacement, context=None):
115 # the root of the arch section shouldn't actually be replaced as it's
116 # not really editable itself, only the content truly is editable.
118 [view] = self.browse(cr, uid, [view_id], context=context)
119 arch = etree.fromstring(view.arch.encode('utf-8'))
120 # => get the replacement root
121 if not section_xpath:
124 # ensure there's only one match
125 [root] = arch.xpath(section_xpath)
127 root.text = replacement.text
128 root.tail = replacement.tail
129 # replace all children
131 for child in replacement:
132 root.append(copy.deepcopy(child))
136 def render(self, cr, uid, id_or_xml_id, values=None, engine='ir.qweb', context=None):
137 if request and getattr(request, 'website_enabled', False):
138 engine='website.qweb'
140 if isinstance(id_or_xml_id, list):
141 id_or_xml_id = id_or_xml_id[0]
146 company = self.pool['res.company'].browse(cr, SUPERUSER_ID, request.website.company_id.id, context=context)
150 website=request.website,
151 url_for=website.url_for,
154 user_id=self.pool.get("res.users").browse(cr, uid, uid),
155 translatable=context.get('lang') != request.website.default_lang_code,
156 editable=request.website.is_publisher(),
157 menu_data=self.pool['ir.ui.menu'].load_menus_root(cr, uid, context=context) if request.website.is_user() else None,
162 qcontext.update(values)
164 # in edit mode ir.ui.view will tag nodes
165 context = dict(context, inherit_branding=qcontext.get('editable', False))
167 view_obj = request.website.get_template(id_or_xml_id)
168 if 'main_object' not in qcontext:
169 qcontext['main_object'] = view_obj
173 return super(view, self).render(cr, uid, id_or_xml_id, values=values, engine=engine, context=context)
175 def _pretty_arch(self, arch):
176 # remove_blank_string does not seem to work on HTMLParser, and
177 # pretty-printing with lxml more or less requires stripping
178 # whitespace: http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output
179 # so serialize to XML, parse as XML (remove whitespace) then serialize
180 # as XML (pretty print)
181 arch_no_whitespace = etree.fromstring(
182 etree.tostring(arch, encoding='utf-8'),
183 parser=etree.XMLParser(encoding='utf-8', remove_blank_text=True))
184 return etree.tostring(
185 arch_no_whitespace, encoding='unicode', pretty_print=True)
187 def save(self, cr, uid, res_id, value, xpath=None, context=None):
188 """ Update a view section. The view section may embed fields to write
192 :param str xpath: valid xpath to the tag to replace
196 arch_section = html.fromstring(
197 value, parser=html.HTMLParser(encoding='utf-8'))
200 # value is an embedded field on its own, not a view section
201 self.save_embedded_field(cr, uid, arch_section, context=context)
204 for el in self.extract_embedded_fields(cr, uid, arch_section, context=context):
205 self.save_embedded_field(cr, uid, el, context=context)
207 # transform embedded field back to t-field
208 el.getparent().replace(el, self.to_field_ref(cr, uid, el, context=context))
210 arch = self.replace_arch_section(cr, uid, res_id, xpath, arch_section, context=context)
211 self.write(cr, uid, res_id, {
212 'arch': self._pretty_arch(arch)
215 view = self.browse(cr, SUPERUSER_ID, res_id, context=context)
216 if view.model_data_id:
217 view.model_data_id.write({'noupdate': True})