1 # -*- coding: utf-8 -*-
4 from lxml import etree, html
6 from openerp import SUPERUSER_ID, tools
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"),
19 'website_id': fields.many2one('website',ondelete='cascade', string="Website"),
23 ('key_website_id_uniq', 'unique(key, website_id)',
24 'Key must be unique per website.'),
29 'customize_show': False,
32 def _view_obj(self, cr, uid, view_id, context=None):
33 if isinstance(view_id, basestring):
34 return self.pool['ir.model.data'].xmlid_to_object(
35 cr, uid, view_id, raise_if_not_found=True, context=context
37 elif isinstance(view_id, (int, long)):
38 return self.browse(cr, uid, view_id, context=context)
40 # assume it's already a view object (WTF?)
43 # Returns all views (called and inherited) related to a view
44 # Used by translation mechanism, SEO and optional templates
45 def _views_get(self, cr, uid, view_id, options=True, bundles=False, context=None, root=True):
46 """ For a given view ``view_id``, should return:
49 * all views inheriting from it, enabled or not
50 - but not the optional children of a non-enabled child
51 * all views called from it (via t-call)
54 view = self._view_obj(cr, uid, view_id, context=context)
59 while root and view.inherit_id:
60 view = view.inherit_id
64 node = etree.fromstring(view.arch)
65 xpath = "//t[@t-call]"
67 xpath += "| //t[@t-call-assets]"
68 for child in node.xpath(xpath):
70 called_view = self._view_obj(cr, uid, child.get('t-call', child.get('t-call-assets')), context=context)
73 if called_view not in result:
74 result += self._views_get(cr, uid, called_view, options=options, bundles=bundles, context=context)
76 extensions = view.inherit_children_ids
78 # only active children
79 extensions = (v for v in view.inherit_children_ids if v.active)
81 # Keep options in a deterministic order regardless of their applicability
82 for extension in sorted(extensions, key=lambda v: v.id):
83 for r in self._views_get(
85 # only return optional grandchildren if this child is enabled
86 options=extension.active,
87 context=context, root=False):
92 def extract_embedded_fields(self, cr, uid, arch, context=None):
93 return arch.xpath('//*[@data-oe-model != "ir.ui.view"]')
95 def save_embedded_field(self, cr, uid, el, context=None):
96 Model = self.pool[el.get('data-oe-model')]
97 field = el.get('data-oe-field')
99 column = Model._all_columns[field].column
100 converter = self.pool['website.qweb'].get_converter_for(
101 el.get('data-oe-type'))
102 value = converter.from_html(cr, uid, Model, column, el)
104 if value is not None:
105 # TODO: batch writes?
106 Model.write(cr, uid, [int(el.get('data-oe-id'))], {
110 def to_field_ref(self, cr, uid, el, context=None):
111 # filter out meta-information inserted in the document
112 attributes = dict((k, v) for k, v in el.items()
113 if not k.startswith('data-oe-'))
114 attributes['t-field'] = el.get('data-oe-expression')
116 out = html.html_parser.makeelement(el.tag, attrib=attributes)
120 def replace_arch_section(self, cr, uid, view_id, section_xpath, replacement, context=None):
121 # the root of the arch section shouldn't actually be replaced as it's
122 # not really editable itself, only the content truly is editable.
124 [view] = self.browse(cr, uid, [view_id], context=context)
125 arch = etree.fromstring(view.arch.encode('utf-8'))
126 # => get the replacement root
127 if not section_xpath:
130 # ensure there's only one match
131 [root] = arch.xpath(section_xpath)
133 root.text = replacement.text
134 root.tail = replacement.tail
135 # replace all children
137 for child in replacement:
138 root.append(copy.deepcopy(child))
142 @tools.ormcache_context(accepted_keys=('website_id',))
143 def get_view_id(self, cr, uid, xml_id, context=None):
144 if context and 'website_id' in context and not isinstance(xml_id, (int, long)):
145 domain = [('key', '=', xml_id), '|', ('website_id', '=', context['website_id']), ('website_id', '=', False)]
146 [xml_id] = self.search(cr, uid, domain, order='website_id', limit=1, context=context)
148 xml_id = super(view, self).get_view_id(cr, uid, xml_id, context=context)
151 def render(self, cr, uid, id_or_xml_id, values=None, engine='ir.qweb', context=None):
152 if request and getattr(request, 'website_enabled', False):
153 engine='website.qweb'
155 if isinstance(id_or_xml_id, list):
156 id_or_xml_id = id_or_xml_id[0]
161 company = self.pool['res.company'].browse(cr, SUPERUSER_ID, request.website.company_id.id, context=context)
165 website=request.website,
166 url_for=website.url_for,
169 user_id=self.pool.get("res.users").browse(cr, uid, uid),
170 translatable=context.get('lang') != request.website.default_lang_code,
171 editable=request.website.is_publisher(),
172 menu_data=self.pool['ir.ui.menu'].load_menus_root(cr, uid, context=context) if request.website.is_user() else None,
177 qcontext.update(values)
179 # in edit mode ir.ui.view will tag nodes
180 if qcontext.get('editable'):
181 context = dict(context, inherit_branding=True)
182 elif request.registry['res.users'].has_group(cr, uid, 'base.group_website_publisher'):
183 context = dict(context, inherit_branding_auto=True)
185 view_obj = request.website.get_template(id_or_xml_id)
186 if 'main_object' not in qcontext:
187 qcontext['main_object'] = view_obj
191 return super(view, self).render(cr, uid, id_or_xml_id, values=values, engine=engine, context=context)
193 def _pretty_arch(self, arch):
194 # remove_blank_string does not seem to work on HTMLParser, and
195 # pretty-printing with lxml more or less requires stripping
196 # whitespace: http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output
197 # so serialize to XML, parse as XML (remove whitespace) then serialize
198 # as XML (pretty print)
199 arch_no_whitespace = etree.fromstring(
200 etree.tostring(arch, encoding='utf-8'),
201 parser=etree.XMLParser(encoding='utf-8', remove_blank_text=True))
202 return etree.tostring(
203 arch_no_whitespace, encoding='unicode', pretty_print=True)
205 def save(self, cr, uid, res_id, value, xpath=None, context=None):
206 """ Update a view section. The view section may embed fields to write
210 :param str xpath: valid xpath to the tag to replace
214 arch_section = html.fromstring(
215 value, parser=html.HTMLParser(encoding='utf-8'))
218 # value is an embedded field on its own, not a view section
219 self.save_embedded_field(cr, uid, arch_section, context=context)
222 for el in self.extract_embedded_fields(cr, uid, arch_section, context=context):
223 self.save_embedded_field(cr, uid, el, context=context)
225 # transform embedded field back to t-field
226 el.getparent().replace(el, self.to_field_ref(cr, uid, el, context=context))
228 arch = self.replace_arch_section(cr, uid, res_id, xpath, arch_section, context=context)
229 self.write(cr, uid, res_id, {
230 'arch': self._pretty_arch(arch)
233 view = self.browse(cr, SUPERUSER_ID, res_id, context=context)
234 if view.model_data_id:
235 view.model_data_id.write({'noupdate': True})