[IMP] move fetching of root view into ir.ui.view

bzr revid: xmo@openerp.com-20130423124657-zkogrodgdw0ymf2f
This commit is contained in:
Xavier Morel 2013-04-23 14:46:57 +02:00
parent c212710f25
commit aef4ce51a7
2 changed files with 59 additions and 40 deletions

View File

@ -256,6 +256,53 @@ class view(osv.osv):
cr, uid, id, model, exclude_base=True, context=None):
yield info
def get_root_ancestor(self, cr, uid, view_id=None,
model=None, view_type=None, context=None):
"""
Fetches the root of the view tree specified by the id or (type, model)
parameters.
If view_id is specified, view_type and model aren't needed (and the
other way around)
:param view_id: id of view to search the root ancestor of
:param str model: model to use the view for
:param str view_type: expected view type
:return:
"""
assert view_id or (model and view_type),\
"caller must provide either a view_id or a model and a view_type"\
" to be able to fetch a root view"
view = False
# Search for a root (i.e. without any parent) view.
while True:
if view_id:
ids = [view_id]
else:
# read does not guarantee ordering so directly take just first
# search'ed id and read that, this way we don't care
ids = self.search(cr, uid, [
['model', '=', model],
['type', '=', view_type],
['inherit_id', '=', False],
], context=context, order='priority')[:1]
views = self.read(cr, uid, ids,[
'arch', 'name', 'field_parent',
'id', 'type', 'inherit_id', 'model'
], context=context)
view = views[0] if views else False
if not views:
break
view_id = view['inherit_id'] or view['id']
# due to read() inherit_id may be a name_get pair, unpack id
if isinstance(view_id, tuple): view_id, _name = view_id
if not view['inherit_id']:
break
return view
def raise_view_error(self, cr, uid, model, error_msg, view_id, child_view_id, context=None):
view, child_view = self.browse(cr, uid, [view_id, child_view_id], context)

View File

@ -2032,9 +2032,6 @@ class BaseModel(object):
return view
#
# if view_id, view_type is not required
#
def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
"""
Get the detailed composition of the requested view like fields, model, view architecture
@ -2081,8 +2078,6 @@ class BaseModel(object):
result = {'type': view_type, 'model': self._name}
sql_res = False
parent_view_model = None
view_ref = context.get(view_type + '_view_ref')
if view_ref and not view_id and '.' in view_ref:
@ -2091,42 +2086,17 @@ class BaseModel(object):
view_ref_res = cr.fetchone()
if view_ref_res:
view_id = view_ref_res[0]
view_fields = ['arch', 'name', 'field_parent', 'id',
'type', 'inherit_id', 'model']
# Search for a root (i.e. without any parent) view.
while True:
if view_id:
ids = [view_id]
else:
# read does not guarantee ordering so directly take just first
# search'ed id and read that, this way we don't care
ids = View.search(cr, user, [
['model', '=', self._name],
['type', '=', view_type],
['inherit_id', '=', False],
], context=context, order='priority')[:1]
views = View.read(cr, user, ids, view_fields, context=context)
sql_res = views[0] if views else False
if not sql_res:
break
view_id = sql_res['inherit_id'] or sql_res['id']
# due to read() inherit_id may be a name_get pair, unpack id
if isinstance(view_id, tuple): view_id, _name = view_id
parent_view_model = sql_res['model']
if not sql_res['inherit_id']:
break
# if a view was found
if sql_res:
source = etree.fromstring(encode(sql_res['arch']))
root_view = View.get_root_ancestor(
cr, user, view_id, self._name, view_type, context=context)
if root_view:
source = etree.fromstring(encode(root_view['arch']))
result.update(
arch=apply_view_inheritance(cr, user, source, sql_res['id']),
type=sql_res['type'],
view_id=sql_res['id'],
name=sql_res['name'],
field_parent=sql_res['field_parent'] or False)
arch=apply_view_inheritance(cr, user, source, root_view['id']),
type=root_view['type'],
view_id=root_view['id'],
name=root_view['name'],
field_parent=root_view['field_parent'] or False)
else:
# otherwise, build some kind of default view
try:
@ -2142,12 +2112,14 @@ class BaseModel(object):
field_parent=False,
view_id=0)
parent_view_model = root_view['model'] if root_view else None
if parent_view_model != self._name:
ctx = context.copy()
ctx['base_model_name'] = parent_view_model
else:
ctx = context
xarch, xfields = self.__view_look_dom_arch(cr, user, result['arch'], view_id, context=ctx)
xarch, xfields = self.__view_look_dom_arch(
cr, user, result['arch'], result['view_id'], context=ctx)
result['arch'] = xarch
result['fields'] = xfields