2013-08-10 17:01:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-09-06 13:09:16 +00:00
|
|
|
from lxml import etree, html
|
2013-08-10 17:01:10 +00:00
|
|
|
from openerp.osv import osv, fields
|
|
|
|
|
|
|
|
|
|
|
|
class view(osv.osv):
|
|
|
|
_inherit = "ir.ui.view"
|
|
|
|
_columns = {
|
|
|
|
'inherit_option_id': fields.many2one('ir.ui.view','Optional Inheritancy'),
|
|
|
|
'inherited_option_ids': fields.one2many('ir.ui.view','inherit_option_id','Optional Inheritancies'),
|
2013-09-06 09:13:51 +00:00
|
|
|
'page': fields.boolean("Whether this view is a web page template (complete)"),
|
|
|
|
}
|
|
|
|
_defaults = {
|
|
|
|
'page': False,
|
2013-08-10 17:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Returns all views (called and inherited) related to a view
|
|
|
|
# Used by translation mechanism, SEO and optional templates
|
2013-08-14 10:18:57 +00:00
|
|
|
def _views_get(self, cr, uid, view, options=True, context=None, root=True, stack_result=None):
|
|
|
|
if not context:
|
|
|
|
context = {}
|
|
|
|
if not stack_result:
|
|
|
|
stack_result = []
|
|
|
|
|
|
|
|
def view_obj(view):
|
|
|
|
if type(view) in (str, unicode):
|
|
|
|
mod_obj = self.pool.get("ir.model.data")
|
|
|
|
m, n = view.split('.')
|
|
|
|
_, view = mod_obj.get_object_reference(cr, uid, m, n)
|
|
|
|
if type(view) == int:
|
|
|
|
view_obj = self.pool.get("ir.ui.view")
|
|
|
|
view = view_obj.browse(cr, uid, view, context=context)
|
|
|
|
return view
|
|
|
|
view = view_obj(view)
|
|
|
|
|
2013-08-10 17:01:10 +00:00
|
|
|
while root and view.inherit_id:
|
|
|
|
view = view.inherit_id
|
|
|
|
|
|
|
|
result = [view]
|
|
|
|
todo = view.inherit_children_ids
|
|
|
|
if options:
|
|
|
|
todo += filter(lambda x: not x.inherit_id, view.inherited_option_ids)
|
|
|
|
for child_view in todo:
|
2013-08-14 10:18:57 +00:00
|
|
|
result += self._views_get(cr, uid, child_view, options=options, context=context, root=False, stack_result=result)
|
2013-08-10 17:01:10 +00:00
|
|
|
node = etree.fromstring(view.arch)
|
|
|
|
for child in node.xpath("//t[@t-call]"):
|
2013-08-14 10:18:57 +00:00
|
|
|
call_view = view_obj(child.get('t-call'))
|
|
|
|
if call_view not in stack_result:
|
|
|
|
result += self._views_get(cr, uid, call_view, options=options, context=context, stack_result=result)
|
2013-08-10 17:01:10 +00:00
|
|
|
return result
|
2013-09-06 13:09:16 +00:00
|
|
|
|
2013-09-09 09:51:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def extract_embedded_fields(self, cr, uid, arch, context=None):
|
|
|
|
return arch.xpath('//*[@data-oe-model != "ir.ui.view"]')
|
|
|
|
|
2013-09-18 14:50:28 +00:00
|
|
|
def convert_embedded_field(self, cr, uid, el, column,
|
|
|
|
type_override=None, context=None):
|
2013-09-18 11:51:36 +00:00
|
|
|
""" Converts the content of an embedded field to a value acceptable
|
|
|
|
for writing in the column
|
|
|
|
|
|
|
|
:param etree._Element el: embedded field being saved
|
|
|
|
:param fields._column column: column object corresponding to the field
|
2013-09-18 14:50:28 +00:00
|
|
|
:param type type_override: column type to dispatch on instead of the
|
|
|
|
column's actual type (for proxy column types
|
|
|
|
e.g. relateds)
|
2013-09-18 11:51:36 +00:00
|
|
|
:return: converted value
|
|
|
|
"""
|
2013-09-18 14:50:28 +00:00
|
|
|
column_type = type_override or type(column)
|
|
|
|
|
|
|
|
if issubclass(column_type, fields.html):
|
2013-09-23 14:57:44 +00:00
|
|
|
content = []
|
|
|
|
if el.text: content.append(el.text)
|
|
|
|
content.extend(html.tostring(child)
|
|
|
|
for child in el.iterchildren(tag=etree.Element))
|
|
|
|
return '\n'.join(content)
|
2013-09-18 14:50:28 +00:00
|
|
|
elif issubclass(column_type, fields.integer):
|
2013-09-18 11:51:36 +00:00
|
|
|
return int(el.text_content())
|
2013-09-18 14:50:28 +00:00
|
|
|
elif issubclass(column_type, fields.float):
|
2013-09-18 11:51:36 +00:00
|
|
|
return float(el.text_content())
|
2013-09-18 14:50:28 +00:00
|
|
|
elif issubclass(column_type, (fields.char, fields.text,
|
|
|
|
fields.date, fields.datetime)):
|
2013-09-18 11:51:36 +00:00
|
|
|
return el.text_content()
|
|
|
|
# TODO: fields.selection
|
2013-09-23 15:16:33 +00:00
|
|
|
elif issubclass(column_type, fields.many2one):
|
|
|
|
matches = self.pool[column._obj].name_search(
|
|
|
|
cr, uid, name=el.text_content().strip(), context=context)
|
|
|
|
# FIXME: more than one match, error reporting
|
|
|
|
return matches[0][0]
|
2013-09-18 14:50:28 +00:00
|
|
|
elif issubclass(column_type, fields.function):
|
2013-09-18 11:51:36 +00:00
|
|
|
# FIXME: special-case selection as in get_pg_type?
|
|
|
|
return self.convert_embedded_field(
|
2013-09-18 14:50:28 +00:00
|
|
|
cr, uid, el, column,
|
|
|
|
type_override=getattr(fields, column._type),
|
|
|
|
context=context)
|
2013-09-18 11:51:36 +00:00
|
|
|
# TODO?: fields.many2many, fields.one2many
|
|
|
|
# TODO?: fields.reference
|
|
|
|
else:
|
2013-09-18 14:50:28 +00:00
|
|
|
raise TypeError("Un-convertable column type %s" % column_type)
|
2013-09-18 11:51:36 +00:00
|
|
|
|
2013-09-09 09:51:12 +00:00
|
|
|
def save_embedded_field(self, cr, uid, el, context=None):
|
2013-09-18 11:51:36 +00:00
|
|
|
Model = self.pool[el.get('data-oe-model')]
|
|
|
|
field = el.get('data-oe-field')
|
|
|
|
|
|
|
|
column = Model._all_columns[field].column
|
2013-09-23 14:35:01 +00:00
|
|
|
Model.write(cr, uid, [int(el.get('data-oe-id'))], {
|
2013-09-18 11:51:36 +00:00
|
|
|
field: self.convert_embedded_field(cr, uid, el, column, context=context)
|
2013-09-09 09:51:12 +00:00
|
|
|
}, context=context)
|
|
|
|
|
|
|
|
def to_field_ref(self, cr, uid, el, context=None):
|
2013-09-19 09:25:46 +00:00
|
|
|
# filter out meta-information inserted in the document
|
|
|
|
attributes = dict((k, v) for k, v in el.items()
|
|
|
|
if not k.startswith('data-oe-'))
|
|
|
|
attributes['t-field'] = el.get('data-oe-expression')
|
|
|
|
|
|
|
|
out = html.html_parser.makeelement(el.tag, attrib=attributes)
|
2013-09-10 14:34:06 +00:00
|
|
|
out.tail = el.tail
|
|
|
|
return out
|
2013-09-09 09:51:12 +00:00
|
|
|
|
|
|
|
def replace_arch_section(self, cr, uid, view_id, section_xpath, replacement, context=None):
|
|
|
|
arch = replacement
|
|
|
|
if section_xpath:
|
|
|
|
previous_arch = etree.fromstring(self.browse(cr, uid, view_id, context=context).arch.encode('utf-8'))
|
|
|
|
# ensure there's only one match
|
|
|
|
[previous_section] = previous_arch.xpath(section_xpath)
|
|
|
|
previous_section.getparent().replace(previous_section, replacement)
|
|
|
|
arch = previous_arch
|
|
|
|
return arch
|
|
|
|
|
|
|
|
def save(self, cr, uid, res_id, value, xpath=None, context=None):
|
|
|
|
""" Update a view section. The view section may embed fields to write
|
2013-09-06 13:09:16 +00:00
|
|
|
|
|
|
|
:param str model:
|
|
|
|
:param int res_id:
|
|
|
|
:param str xpath: valid xpath to the tag to replace
|
|
|
|
"""
|
2013-09-09 09:51:12 +00:00
|
|
|
res_id = int(res_id)
|
|
|
|
|
2013-09-09 11:10:10 +00:00
|
|
|
arch_section = html.fromstring(value)
|
2013-09-09 09:51:12 +00:00
|
|
|
|
2013-09-17 08:57:53 +00:00
|
|
|
if xpath is None:
|
|
|
|
# value is an embedded field on its own, not a view section
|
|
|
|
self.save_embedded_field(cr, uid, arch_section, context=context)
|
|
|
|
return
|
|
|
|
|
2013-09-09 09:51:12 +00:00
|
|
|
for el in self.extract_embedded_fields(cr, uid, arch_section, context=context):
|
|
|
|
self.save_embedded_field(cr, uid, el, context=context)
|
|
|
|
|
|
|
|
# transform embedded field back to t-field
|
|
|
|
el.getparent().replace(el, self.to_field_ref(cr, uid, el, context=context))
|
|
|
|
|
|
|
|
arch = self.replace_arch_section(cr, uid, res_id, xpath, arch_section, context=context)
|
|
|
|
self.write(cr, uid, res_id, {
|
|
|
|
'arch': etree.tostring(arch, encoding='utf-8').decode('utf-8')
|
|
|
|
}, context=context)
|