Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions base_view_inheritance_extension/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Extended view inheritance
=========================

This module was written to make it simple to add custom operators for view inheritance.
This module was written to make it simple to add custom operators for view
inheritance.

Usage
=====
Expand All @@ -24,7 +25,8 @@ Change a python dictionary (context for example)
$new_value
</attribute>

Note that views are subject to evaluation of xmlids anyways, so if you need to refer to some xmlid, say ``%(xmlid)s``.
Note that views are subject to evaluation of xmlids anyways, so if you need
to refer to some xmlid, say ``%(xmlid)s``.

Move an element in the view
---------------------------
Expand All @@ -33,13 +35,30 @@ Move an element in the view

<xpath expr="$xpath" position="move" target="$targetxpath" />

This can also be used to wrap some element into another, create the target element first, then move the node youwant to wrap there.
This can also be used to wrap some element into another, create the target
element first, then move the node youwant to wrap there.

Add to values in a list (states for example)
--------------------------------------------

.. code-block:: xml

<attribute name="$attribute" operation="list_add">
$new_value(s)
</attribute>

Remove values from a list (states for example)
----------------------------------------------

.. code-block:: xml

<attribute name="$attribute" operation="list_remove">
$remove_value(s)
</attribute>

Known issues / Roadmap
======================

* add ``<attribute operation="python_list_add">$value</attribute>``
* add ``<attribute operation="python_list_remove">$index</attribute>``
* add ``<attribute operation="json_dict" key="$key">$value</attribute>``
* support ``<xpath expr="$xpath" position="move" target="xpath" target_position="position" />``
* support an ``eval`` attribute for our new node types
Expand All @@ -58,14 +77,21 @@ Credits
Images
------

* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
* Odoo Community Association:
`Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.

Contributors
------------

* Holger Brunn <hbrunn@therp.nl>

Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list <mailto:community@mail.odoo.com>`_ or the `appropriate specialized mailinglist <https://odoo-community.org/groups>`_ for help, and the bug tracker linked in `Bug Tracker`_ above for technical issues.
* Ronald Portier <rportier@therp.nl>

Do not contact contributors directly about help with questions or problems
concerning this addon, but use the
`community mailing list <mailto:community@mail.odoo.com>`_ or the
`appropriate specialized mailinglist <https://odoo-community.org/groups>`_
for help, and the bug tracker linked in `Bug Tracker`_ above for
technical issues.

Maintainer
----------
Expand Down
37 changes: 37 additions & 0 deletions base_view_inheritance_extension/models/ir_ui_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,40 @@ def inheritance_handler_xpath(self, source, specs, inherit_id):
)
target_node.append(node)
return source

@api.model
def inheritance_handler_attributes_list_add(
self, source, specs, inherit_id
):
"""Implement
<$node position="attributes">
<attribute name="$attribute" operation="list_add">
$new_value
</attribute>
</$node>"""
node = self.locate_node(source, specs)
for attribute_node in specs:
attribute_name = attribute_node.get('name')
old_value = node.get(attribute_name) or ''
new_value = old_value + ',' + attribute_node.text
node.attrib[attribute_name] = new_value
return source

@api.model
def inheritance_handler_attributes_list_remove(
self, source, specs, inherit_id
):
"""Implement
<$node position="attributes">
<attribute name="$attribute" operation="list_remove">
$value_to_remove
</attribute>
</$node>"""
node = self.locate_node(source, specs)
for attribute_node in specs:
attribute_name = attribute_node.get('name')
old_values = (node.get(attribute_name) or '').split(',')
remove_values = attribute_node.text.split(',')
new_values = [x for x in old_values if x not in remove_values]
node.attrib[attribute_name] = ','.join(filter(None, new_values))
return source
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


class TestBaseViewInheritanceExtension(TransactionCase):

def test_base_view_inheritance_extension(self):
view_id = self.env.ref('base.view_partner_form').id
fields_view_get = self.env['res.partner'].fields_view_get(
Expand All @@ -28,3 +29,85 @@ def test_base_view_inheritance_extension(self):
view.xpath('//field[@name="child_ids"]')[0].getparent(),
view.xpath('//page[@name="my_new_page"]')[0]
)

def test_list_add(self):
view_model = self.env['ir.ui.view']
inherit_id = self.env.ref('base.view_partner_form').id
source = etree.fromstring(
"""\
<form>
<button name="test" states="draft,open"/>
</form>
"""
)
# extend with single value
specs = etree.fromstring(
"""\
<button name="test" position="attributes">
<attribute
name="states"
operation="list_add"
>valid</attribute>
</button>
"""
)
modified_source = \
view_model.inheritance_handler_attributes_list_add(
source, specs, inherit_id
)
button_node = modified_source.xpath('//button[@name="test"]')[0]
self.assertEqual(
button_node.attrib['states'],
'draft,open,valid'
)
# extend with list of values
specs = etree.fromstring(
"""\
<button name="test" position="attributes">
<attribute
name="states"
operation="list_add"
>payable,paid</attribute>
</button>
"""
)
modified_source = \
view_model.inheritance_handler_attributes_list_add(
source, specs, inherit_id
)
button_node = modified_source.xpath('//button[@name="test"]')[0]
self.assertEqual(
button_node.attrib['states'],
'draft,open,valid,payable,paid'
)

def test_list_remove(self):
view_model = self.env['ir.ui.view']
inherit_id = self.env.ref('base.view_partner_form').id
source = etree.fromstring(
"""\
<form>
<button name="test" states="draft,open,valid,payable,paid"/>
</form>
"""
)
# remove list of values
specs = etree.fromstring(
"""\
<button name="test" position="attributes">
<attribute
name="states"
operation="list_remove"
>open,payable</attribute>
</button>
"""
)
modified_source = \
view_model.inheritance_handler_attributes_list_remove(
source, specs, inherit_id
)
button_node = modified_source.xpath('//button[@name="test"]')[0]
self.assertEqual(
button_node.attrib['states'],
'draft,valid,paid'
)