[IMP] base_view_inheritance_extension: black, isort

pull/2494/head
Sergio Teruel 2020-03-03 19:35:48 +01:00 committed by Enric Tobella
parent 848ad96c1e
commit 45542a0071
4 changed files with 57 additions and 101 deletions

View File

@ -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"],
}

View File

@ -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">
<attribute name="$attribute" operation="python_dict" key="$key">
@ -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">
<attribute name="$attribute" operation="list_add">
@ -127,16 +107,14 @@ class IrUiView(models.Model):
</$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
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">
<attribute name="$attribute" operation="list_remove">
@ -145,10 +123,9 @@ class IrUiView(models.Model):
</$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(',')
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

View File

@ -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 <https://www.odoo.com/documentation/12.0/reference/views.html#inheritance-specs>`_.

View File

@ -1,32 +1,29 @@
# Copyright 2016 Therp BV <http://therp.nl>
# 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(
"""\
<form>
@ -45,15 +42,11 @@ class TestBaseViewInheritanceExtension(TransactionCase):
</button>
"""
)
modified_source = \
view_model.inheritance_handler_attributes_list_add(
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'
)
self.assertEqual(button_node.attrib["states"], "draft,open,valid")
# extend with list of values
specs = etree.fromstring(
"""\
@ -65,19 +58,15 @@ class TestBaseViewInheritanceExtension(TransactionCase):
</button>
"""
)
modified_source = \
view_model.inheritance_handler_attributes_list_add(
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'
)
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(
"""\
<form>
@ -96,12 +85,8 @@ class TestBaseViewInheritanceExtension(TransactionCase):
</button>
"""
)
modified_source = \
view_model.inheritance_handler_attributes_list_remove(
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'
)
self.assertEqual(button_node.attrib["states"], "draft,valid,paid")