From 2fc9b61c3d1838a6615e386240e3917458194915 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Tue, 4 Apr 2017 19:06:26 +0200 Subject: [PATCH] [8.0][ADD] Add list manipulation operations to base_view_inheritance_extension. (#804) * Add list_add operation. * Add list_remove operation. --- base_view_inheritance_extension/README.rst | 42 ++++++++-- .../models/ir_ui_view.py | 37 +++++++++ .../test_base_view_inheritance_extension.py | 83 +++++++++++++++++++ 3 files changed, 154 insertions(+), 8 deletions(-) diff --git a/base_view_inheritance_extension/README.rst b/base_view_inheritance_extension/README.rst index e0186c4bb..88b4b7a3f 100644 --- a/base_view_inheritance_extension/README.rst +++ b/base_view_inheritance_extension/README.rst @@ -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 ===== @@ -24,7 +25,8 @@ Change a python dictionary (context for example) $new_value -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 --------------------------- @@ -33,13 +35,30 @@ Move an element in the view -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 + + + $new_value(s) + + +Remove values from a list (states for example) +---------------------------------------------- + +.. code-block:: xml + + + $remove_value(s) + Known issues / Roadmap ====================== -* add ``$value`` -* add ``$index`` * add ``$value`` * support ```` * support an ``eval`` attribute for our new node types @@ -58,14 +77,21 @@ Credits Images ------ -* Odoo Community Association: `Icon `_. +* Odoo Community Association: + `Icon `_. Contributors ------------ * Holger Brunn - -Do not contact contributors directly about help with questions or problems concerning this addon, but use the `community mailing list `_ or the `appropriate specialized mailinglist `_ for help, and the bug tracker linked in `Bug Tracker`_ above for technical issues. +* Ronald Portier + +Do not contact contributors directly about help with questions or problems +concerning this addon, but use the +`community mailing list `_ or the +`appropriate specialized mailinglist `_ +for help, and the bug tracker linked in `Bug Tracker`_ above for +technical issues. Maintainer ---------- diff --git a/base_view_inheritance_extension/models/ir_ui_view.py b/base_view_inheritance_extension/models/ir_ui_view.py index 752220079..a32baee9d 100644 --- a/base_view_inheritance_extension/models/ir_ui_view.py +++ b/base_view_inheritance_extension/models/ir_ui_view.py @@ -122,3 +122,40 @@ class IrUiView(models.Model): ) target_node.append(node) return source + + @api.model + def inheritance_handler_attributes_list_add( + self, source, specs, inherit_id + ): + """Implement + <$node position="attributes"> + + $new_value + + """ + 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"> + + $value_to_remove + + """ + 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 diff --git a/base_view_inheritance_extension/tests/test_base_view_inheritance_extension.py b/base_view_inheritance_extension/tests/test_base_view_inheritance_extension.py index 2429b4f6f..af1e2c6ba 100644 --- a/base_view_inheritance_extension/tests/test_base_view_inheritance_extension.py +++ b/base_view_inheritance_extension/tests/test_base_view_inheritance_extension.py @@ -6,6 +6,7 @@ from openerp.tests.common import TransactionCase 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( @@ -28,3 +29,85 @@ class TestBaseViewInheritanceExtension(TransactionCase): 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( + """\ +
+
+ """ + ) + 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( + """\ + + """ + ) + 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( + """\ +
+
+ """ + ) + 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' + )