changeset 9ae8c0921732 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=9ae8c0921732
description:
Add depends fields on view_attributes
issue8862
review303161002
diffstat:
CHANGELOG | 1 +
doc/ref/models/models.rst | 5 +++--
trytond/model/modelview.py | 15 +++++++++++++--
trytond/tests/modelview.py | 32 ++++++++++++++++++++++++++++++++
trytond/tests/modelview.xml | 25 +++++++++++++++++++++++++
trytond/tests/test_modelview.py | 23 +++++++++++++++++++++++
6 files changed, 97 insertions(+), 4 deletions(-)
diffs (177 lines):
diff -r bdcbaffd85d4 -r 9ae8c0921732 CHANGELOG
--- a/CHANGELOG Sat Mar 28 23:56:19 2020 +0100
+++ b/CHANGELOG Mon Mar 30 09:33:15 2020 +0200
@@ -1,3 +1,4 @@
+* Add depends fields on view_attributes
* Simplify trigger action
* Run trigger in the queue
* Do not copy Binary content with file_id
diff -r bdcbaffd85d4 -r 9ae8c0921732 doc/ref/models/models.rst
--- a/doc/ref/models/models.rst Sat Mar 28 23:56:19 2020 +0100
+++ b/doc/ref/models/models.rst Mon Mar 30 09:33:15 2020 +0200
@@ -151,9 +151,10 @@
.. classmethod:: ModelView.view_attributes()
- Returns a list of XPath, attribute and value.
+ Returns a list of XPath, attribute, value and an optional depends list.
Each element from the XPath will get the attribute set with the JSON
- encoded value.
+ encoded value. If the depends list is set its fields are added to the
+ view if the xpath matches at least one element.
============
ModelStorage
diff -r bdcbaffd85d4 -r 9ae8c0921732 trytond/model/modelview.py
--- a/trytond/model/modelview.py Sat Mar 28 23:56:19 2020 +0100
+++ b/trytond/model/modelview.py Mon Mar 30 09:33:15 2020 +0200
@@ -403,9 +403,16 @@
FieldAccess = pool.get('ir.model.field.access')
encoder = PYSONEncoder()
- for xpath, attribute, value in cls.view_attributes():
- for element in tree.xpath(xpath):
+ view_depends = []
+ for xpath, attribute, value, *extra in cls.view_attributes():
+ depends = []
+ if extra:
+ depends, = extra
+ nodes = tree.xpath(xpath)
+ for element in nodes:
element.set(attribute, encoder.encode(value))
+ if nodes and depends:
+ view_depends.extend(depends)
fields_width = {}
tree_root = tree.getroottree().getroot()
@@ -494,6 +501,10 @@
for depend in field.depends:
fields_def.setdefault(depend, {'name': depend})
+ for depend in view_depends:
+ if depend not in fields_to_remove:
+ fields_def.setdefault(depend, {'name': depend})
+
arch = etree.tostring(
tree, encoding='utf-8', pretty_print=False).decode('utf-8')
# Do not call fields_def without fields as it returns all fields
diff -r bdcbaffd85d4 -r 9ae8c0921732 trytond/tests/modelview.py
--- a/trytond/tests/modelview.py Sat Mar 28 23:56:19 2020 +0100
+++ b/trytond/tests/modelview.py Mon Mar 30 09:33:15 2020 +0200
@@ -3,6 +3,7 @@
from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool
+from trytond.pyson import If, Eval
class ModelViewChangedValues(ModelView):
@@ -164,6 +165,35 @@
foobar = fields.Char("Char", depends=['foo'])
+class ModelViewViewAttributes(ModelView):
+ 'ModelView View Attributes'
+ __name__ = 'test.modelview.view_attributes'
+
+ foo = fields.Char("Char")
+
+ @classmethod
+ def view_attributes(cls):
+ return super().view_attributes() + [
+ ('//field[@name="foo"]',
+ 'visual', If(Eval('foo') == 'foo', 'danger', '')),
+ ]
+
+
+class ModelViewViewAttributesDepends(ModelView):
+ 'ModelView View Attributes Depends'
+ __name__ = 'test.modelview.view_attributes_depends'
+
+ foo = fields.Char("Char")
+ bar = fields.Char("Char")
+
+ @classmethod
+ def view_attributes(cls):
+ return super().view_attributes() + [
+ ('//field[@name="foo"]',
+ 'visual', If(Eval('bar') == 'foo', 'danger', ''), ['bar']),
+ ]
+
+
def register(module):
Pool.register(
ModelViewChangedValues,
@@ -175,4 +205,6 @@
ModelViewRPC,
ModelViewEmptyPage,
ModelViewCircularDepends,
+ ModelViewViewAttributes,
+ ModelViewViewAttributesDepends,
module=module, type_='model')
diff -r bdcbaffd85d4 -r 9ae8c0921732 trytond/tests/modelview.xml
--- a/trytond/tests/modelview.xml Sat Mar 28 23:56:19 2020 +0100
+++ b/trytond/tests/modelview.xml Mon Mar 30 09:33:15 2020 +0200
@@ -34,4 +34,29 @@
<field name="name">Test Button Action</field>
<field name="url">http://www.example.com/</field>
</record>
+
+ <record model="ir.ui.view" id="test_modelview_view_attributes">
+ <field name="model">test.modelview.view_attributes</field>
+ <field name="type">form</field>
+ <field name="arch" type="xml">
+ <![CDATA[
+ <form>
+ <label name="foo"/>
+ <field name="foo"/>
+ </form>
+ ]]>
+ </field>
+ </record>
+ <record model="ir.ui.view" id="test_modelview_view_attributes_depends">
+ <field name="model">test.modelview.view_attributes_depends</field>
+ <field name="type">form</field>
+ <field name="arch" type="xml">
+ <![CDATA[
+ <form>
+ <label name="foo"/>
+ <field name="foo"/>
+ </form>
+ ]]>
+ </field>
+ </record>
</tryton>
diff -r bdcbaffd85d4 -r 9ae8c0921732 trytond/tests/test_modelview.py
--- a/trytond/tests/test_modelview.py Sat Mar 28 23:56:19 2020 +0100
+++ b/trytond/tests/test_modelview.py Mon Mar 30 09:33:15 2020 +0200
@@ -461,6 +461,29 @@
self.assertEqual(len(buttons), 0)
+ @with_transaction()
+ def test_view_attributes(self):
+ "Testing view attributes are applied on view"
+ pool = Pool()
+ ViewAttributes = pool.get('test.modelview.view_attributes')
+
+ arch = ViewAttributes.fields_view_get(view_type='form')['arch']
+ parser = etree.XMLParser()
+ tree = etree.fromstring(arch, parser=parser)
+ field, = tree.xpath('//field[@name="foo"]')
+
+ self.assertTrue(field.attrib.get('visual'))
+
+ @with_transaction()
+ def test_view_attributes_depends(self):
+ "Testing view attributes depends are included on fields"
+ pool = Pool()
+ ViewAttributes = pool.get('test.modelview.view_attributes_depends')
+
+ fields = ViewAttributes.fields_view_get(view_type='form')['fields']
+
+ self.assertIn('bar', fields)
+
def suite():
func = unittest.TestLoader().loadTestsFromTestCase