1 # -*- coding: utf-8 -*-
7 from lxml import etree, html
9 from openerp.addons.website.models import website
10 from openerp.http import request
11 from openerp.osv import osv, fields
14 _inherit = "ir.ui.view"
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),
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
33 elif isinstance(view_id, (int, long)):
34 return self.browse(cr, uid, view_id, context=context)
36 # assume it's already a view object (WTF?)
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:
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)
50 view = self._view_obj(cr, uid, view_id, context=context)
55 while root and view.inherit_id:
56 view = view.inherit_id
60 node = etree.fromstring(view.arch)
61 for child in node.xpath("//t[@t-call]"):
63 called_view = self._view_obj(cr, uid, child.get('t-call'), context=context)
66 if called_view not in result:
67 result += self._views_get(cr, uid, called_view, options=options, context=context)
69 extensions = set(view.inherit_children_ids)
71 extensions.update(view.inherited_option_ids)
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(
77 # only return optional grandchildren if this child is enabled
78 options=bool(extension.inherit_id),
79 context=context, root=False):
84 def extract_embedded_fields(self, cr, uid, arch, context=None):
85 return arch.xpath('//*[@data-oe-model != "ir.ui.view"]')
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')
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)
98 Model.write(cr, uid, [int(el.get('data-oe-id'))], {
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')
108 out = html.html_parser.makeelement(el.tag, attrib=attributes)
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.
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:
122 # ensure there's only one match
123 [root] = arch.xpath(section_xpath)
125 root.text = replacement.text
126 root.tail = replacement.tail
127 # replace all children
129 for child in replacement:
130 root.append(copy.deepcopy(child))
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'
138 if isinstance(id_or_xml_id, list):
139 id_or_xml_id = id_or_xml_id[0]
146 website=request.website,
147 url_for=website.url_for,
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(),
157 qcontext.update(values)
159 # in edit mode ir.ui.view will tag nodes
160 context['inherit_branding'] = qcontext.get('editable', False)
162 view_obj = request.website.get_template(id_or_xml_id)
163 if 'main_object' not in qcontext:
164 qcontext['main_object'] = view_obj
168 return super(view, self).render(cr, uid, id_or_xml_id, values=values, engine=engine, context=context)
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)
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
187 :param str xpath: valid xpath to the tag to replace
191 arch_section = html.fromstring(
192 value, parser=html.HTMLParser(encoding='utf-8'))
195 # value is an embedded field on its own, not a view section
196 self.save_embedded_field(cr, uid, arch_section, context=context)
199 for el in self.extract_embedded_fields(cr, uid, arch_section, context=context):
200 self.save_embedded_field(cr, uid, el, context=context)
202 # transform embedded field back to t-field
203 el.getparent().replace(el, self.to_field_ref(cr, uid, el, context=context))
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)