[FIX] qweb: render a child view with its xmlid

QWeb's get_template:
  * remove the fallback that returned the entire result of IRQweb's
    `load` if no match was made between the t-name of the first children
    of this result and the template to render
  * When get_template receives an etree, directly return it and don't
    try to find a matching t-name in it. The loader was not called but
    a matching was trying to be made, now that we removed the fallback
    a lot of tests that were relying on passing etree didn't work
    anymore.

IRQweb's load now return consistently the full result of IrUiView's
`read_templace` when asked to load an id or an xmlid. We make a special
case to patch the result if asked with an id or with a child view so
that the "t-name" of one of the first children of what we return is
always what QWeb is looking for.
This commit is contained in:
Simon Lejeune
2016-12-02 12:24:51 +01:00
committed by Olivier Dony
parent 9dd5bd9349
commit 12e36a66f2
3 changed files with 19 additions and 11 deletions
+1 -1
View File
@@ -195,7 +195,7 @@ class TestViewSaving(common.TransactionCase):
def test_save_escaped_text(self):
""" Test saving html special chars in text nodes """
view = self.env['ir.ui.view'].create({
'arch': '<t><p><h1>hello world</h1></p></t>',
'arch': '<t t-name="dummy"><p><h1>hello world</h1></p></t>',
'type': 'qweb'
})
# script and style text nodes should not escaped client side
+16 -8
View File
@@ -77,18 +77,26 @@ class IrQWeb(models.AbstractModel, QWeb):
env = self.env
if lang != env.context.get('lang'):
env = env(context=dict(env.context, lang=lang))
template = env['ir.ui.view'].read_template(name)
res_id = isinstance(name, (int, long)) and name or None
if res_id:
# QWeb's `read_template` will check if one of the first children of
# what we send to it has a "t-name" attribute having `name` as value
# to consider it has found it. As it'll never be the case when working
# with view ids or children view, force it here.
def is_child_view(view):
view_id = self.env['ir.ui.view'].get_view_id(name)
view = self.env['ir.ui.view'].browse(view_id)
return view.inherit_id is not None and view.mode != 'primary'
if isinstance(name, (int, long)) or is_child_view(name):
for node in etree.fromstring(template):
if node.get('t-name'):
return node
elif res_id and node.tag == "t":
node.set('t-name', str(res_id))
return node
return template
node.set('t-name', str(name))
return node.getparent()
return None # trigger "template not found" in QWeb
else:
return template
# order
+2 -2
View File
@@ -335,6 +335,7 @@ class QWeb(object):
if isinstance(template, etree._Element):
document = template
template = etree.tostring(template)
return (document, template)
else:
try:
document = options.get('load', self.load)(template, options)
@@ -352,9 +353,8 @@ class QWeb(object):
else:
element = etree.parse(document).getroot()
for node in element:
if node.get('t-name') == template:
if node.get('t-name') == str(template):
return (node, document)
return (element, document)
raise QWebException("Template not found", name=template)