From 45542a0071da51bb07085dee664d2359fba410b8 Mon Sep 17 00:00:00 2001 From: Sergio Teruel Date: Tue, 3 Mar 2020 19:35:48 +0100 Subject: [PATCH] [IMP] base_view_inheritance_extension: black, isort --- .../__manifest__.py | 8 +- .../models/ir_ui_view.py | 85 +++++++------------ .../readme/USAGE.rst | 2 - .../test_base_view_inheritance_extension.py | 63 ++++++-------- 4 files changed, 57 insertions(+), 101 deletions(-) diff --git a/base_view_inheritance_extension/__manifest__.py b/base_view_inheritance_extension/__manifest__.py index e178e5725..f1c8ea16b 100644 --- a/base_view_inheritance_extension/__manifest__.py +++ b/base_view_inheritance_extension/__manifest__.py @@ -8,10 +8,6 @@ "license": "LGPL-3", "category": "Hidden/Dependency", "summary": "Adds more operators for view inheritance", - "depends": [ - 'base', - ], - "demo": [ - "demo/ir_ui_view.xml", - ], + "depends": ["base"], + "demo": ["demo/ir_ui_view.xml"], } diff --git a/base_view_inheritance_extension/models/ir_ui_view.py b/base_view_inheritance_extension/models/ir_ui_view.py index d7386d706..d09f75ab8 100644 --- a/base_view_inheritance_extension/models/ir_ui_view.py +++ b/base_view_inheritance_extension/models/ir_ui_view.py @@ -2,34 +2,30 @@ # Copyright 2018 Tecnativa - Sergio Teruel # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). from lxml import etree + from odoo import api, models, tools from odoo.tools.safe_eval import safe_eval class UnquoteObject(str): def __getattr__(self, name): - return UnquoteObject('%s.%s' % (self, name)) + return UnquoteObject("{}.{}".format(self, name)) def __repr__(self): return self def __call__(self, *args, **kwargs): return UnquoteObject( - '%s(%s)' % ( + "%s(%s)" + % ( self, - ','.join( + ",".join( [ - UnquoteObject( - a if not isinstance(a, str) - else "'%s'" % a - ) + UnquoteObject(a if not isinstance(a, str) else "'%s'" % a) for a in args - ] + - [ - '%s=%s' % (UnquoteObject(k), v) - for (k, v) in kwargs.items() ] - ) + + ["{}={}".format(UnquoteObject(k), v) for (k, v) in kwargs.items()] + ), ) ) @@ -40,7 +36,7 @@ class UnquoteEvalObjectContext(tools.misc.UnquoteEvalContext): class IrUiView(models.Model): - _inherit = 'ir.ui.view' + _inherit = "ir.ui.view" @api.model def apply_inheritance_specs(self, source, specs_tree, inherit_id): @@ -50,52 +46,40 @@ class IrUiView(models.Model): @api.model def _iter_inheritance_specs(self, spec): - if spec.tag == 'data': + if spec.tag == "data": for child in spec: for node, handler in self._iter_inheritance_specs(child): yield node, handler return - if spec.get('position') == 'attributes': - if all(not c.get('operation') for c in spec): + if spec.get("position") == "attributes": + if all(not c.get("operation") for c in spec): yield spec, self._get_inheritance_handler(spec) return for child in spec: node = etree.Element(spec.tag, **spec.attrib) node.insert(0, child) - yield node, self._get_inheritance_handler_attributes( - child - ) + yield node, self._get_inheritance_handler_attributes(child) return yield spec, self._get_inheritance_handler(spec) @api.model def _get_inheritance_handler(self, node): handler = super(IrUiView, self).apply_inheritance_specs - if hasattr( - self, 'inheritance_handler_%s' % node.tag - ): - handler = getattr( - self, - 'inheritance_handler_%s' % node.tag - ) + if hasattr(self, "inheritance_handler_%s" % node.tag): + handler = getattr(self, "inheritance_handler_%s" % node.tag) return handler @api.model def _get_inheritance_handler_attributes(self, node): handler = super(IrUiView, self).apply_inheritance_specs - if hasattr( - self, 'inheritance_handler_attributes_%s' % node.get('operation') - ): + if hasattr(self, "inheritance_handler_attributes_%s" % node.get("operation")): handler = getattr( - self, - 'inheritance_handler_attributes_%s' % node.get('operation') + self, "inheritance_handler_attributes_%s" % node.get("operation") ) return handler @api.model - def inheritance_handler_attributes_python_dict( - self, source, specs, inherit_id - ): + def inheritance_handler_attributes_python_dict(self, source, specs, inherit_id): """Implement <$node position="attributes"> @@ -105,20 +89,16 @@ class IrUiView(models.Model): node = self.locate_node(source, specs) for attribute_node in specs: python_dict = safe_eval( - node.get(attribute_node.get('name')) or '{}', + node.get(attribute_node.get("name")) or "{}", UnquoteEvalObjectContext(), - nocopy=True + nocopy=True, ) - python_dict[attribute_node.get('key')] = UnquoteObject( - attribute_node.text - ) - node.attrib[attribute_node.get('name')] = str(python_dict) + python_dict[attribute_node.get("key")] = UnquoteObject(attribute_node.text) + node.attrib[attribute_node.get("name")] = str(python_dict) return source @api.model - def inheritance_handler_attributes_list_add( - self, source, specs, inherit_id - ): + def inheritance_handler_attributes_list_add(self, source, specs, inherit_id): """Implement <$node position="attributes"> @@ -127,16 +107,14 @@ class IrUiView(models.Model): """ 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 + 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 - ): + def inheritance_handler_attributes_list_remove(self, source, specs, inherit_id): """Implement <$node position="attributes"> @@ -145,10 +123,9 @@ class IrUiView(models.Model): """ 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(',') + 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( - [_f for _f in new_values if _f]) + node.attrib[attribute_name] = ",".join([_f for _f in new_values if _f]) return source diff --git a/base_view_inheritance_extension/readme/USAGE.rst b/base_view_inheritance_extension/readme/USAGE.rst index 2af06adaa..aba349bf5 100644 --- a/base_view_inheritance_extension/readme/USAGE.rst +++ b/base_view_inheritance_extension/readme/USAGE.rst @@ -29,5 +29,3 @@ to refer to some xmlid, say ``%(xmlid)s``. **Move an element in the view** This feature is now native, cf the `official Odoo documentation `_. - - 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 ae30978ad..25f29e539 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 @@ -1,32 +1,29 @@ # Copyright 2016 Therp BV # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). from lxml import etree + from odoo.tests.common import TransactionCase class TestBaseViewInheritanceExtension(TransactionCase): - def test_base_view_inheritance_extension(self): - view_id = self.env.ref('base.view_partner_simple_form').id - fields_view_get = self.env['res.partner'].fields_view_get( - view_id=view_id - ) - view = etree.fromstring(fields_view_get['arch']) + view_id = self.env.ref("base.view_partner_simple_form").id + fields_view_get = self.env["res.partner"].fields_view_get(view_id=view_id) + view = etree.fromstring(fields_view_get["arch"]) # verify normal attributes work - self.assertEqual(view.xpath('//form')[0].get('string'), 'Partner form') + self.assertEqual(view.xpath("//form")[0].get("string"), "Partner form") # verify our extra context key worked self.assertTrue( - 'default_name' in - view.xpath('//field[@name="parent_id"]')[0].get('context') + "default_name" in view.xpath('//field[@name="parent_id"]')[0].get("context") ) self.assertTrue( - "context.get('company_id', context.get('company'))" in - view.xpath('//field[@name="parent_id"]')[0].get('context') + "context.get('company_id', context.get('company'))" + in view.xpath('//field[@name="parent_id"]')[0].get("context") ) def test_list_add(self): - view_model = self.env['ir.ui.view'] - inherit_id = self.env.ref('base.view_partner_form').id + view_model = self.env["ir.ui.view"] + inherit_id = self.env.ref("base.view_partner_form").id source = etree.fromstring( """\
@@ -45,15 +42,11 @@ class TestBaseViewInheritanceExtension(TransactionCase): """ ) - 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' + 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( """\ @@ -65,19 +58,15 @@ class TestBaseViewInheritanceExtension(TransactionCase): """ ) - 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' + 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 + view_model = self.env["ir.ui.view"] + inherit_id = self.env.ref("base.view_partner_form").id source = etree.fromstring( """\ @@ -96,12 +85,8 @@ class TestBaseViewInheritanceExtension(TransactionCase): """ ) - 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' + 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")