Thank for the hint. I checked the code and I think this is the part you are talking about
/ir/ui/view.py
@classmethod
def inherit_apply(cls, tree, inherit):
root_inherit = inherit.getroottree().getroot()
for element in root_inherit:
expr = element.get('expr')
targets = tree.xpath(expr)
assert targets, "No elements found for expression %r" % expr # <------
for target in targets:
position = element.get('position', 'inside')
new_tree = getattr(cls, '_inherit_apply_%s' % position)(
tree, element, target)
if new_tree is not None:
tree = new_tree
return tree
if I add this code
@classmethod
def inherit_apply(cls, tree, inherit):
root_inherit = inherit.getroottree().getroot()
for element in root_inherit:
expr = element.get('expr')
targets = tree.xpath(expr)
# NEW: if no targets and a create_expr is specified,
# create the container
if not targets and element.get('create_expr'):
create_expr = element.get('create_expr')
create_position = element.get('create_position', 'after')
create_targets = tree.xpath(create_expr)
if create_targets:
# Build the container (e.g., <notebook>)
# and add element's children inside it
container = element[0] # first child is the container
getattr(cls, '_inherit_apply_%s' % create_position)(
tree, container, create_targets[0])
# Now the original xpath should find the container
targets = tree.xpath(expr)
# END NEW CODE
assert targets, "No elements found for expression %r" % expr
for target in targets:
position = element.get('position', 'inside')
new_tree = getattr(cls, '_inherit_apply_%s' % position)(
tree, element, target)
if new_tree is not None:
tree = new_tree
return tree
I can inherit the view as follow
<!-- document_hr/view/employee_form_documents.xml -->
<data>
<xpath expr="//notebook" position="inside"
create_expr="//field[@name='supervisor']" create_position="after">
<notebook colspan="4">
<page string="Documents" id="page_documents">
<field name="documents" colspan="4"/>
</page>
</notebook>
</xpath>
</data>
with these new elements,
create_expr - where to create the content if `expr` finds nothing
create_position - How to insert at the fallback (after, before, inside)
I don’t know if this make sense, add value, or it is align with the Tryton design principles.
As an alternative I was thinking to create a small module employee_notebook that create the notebook and the rest of modules depend on it… but I am not sure that create a module only to create a notebook in a view is maybe too much.
any feedback or advice?