From 54f70e2971c4e58033a04e38c66b17d7fd3e91ab Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Mon, 19 Feb 2018 11:35:37 +0100 Subject: [PATCH] Add depends on Button issue6919 review40811002 --- CHANGELOG | 1 + doc/topics/views/index.rst | 3 +++ trytond/ir/model.py | 1 + trytond/ir/module.py | 6 ++++++ trytond/ir/ui/view.py | 1 + trytond/model/modelview.py | 3 +++ trytond/res/user.py | 2 ++ trytond/tests/test_tryton.py | 8 +++++++- 8 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 821cd2f13a..7003765a6b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add depends on Button * Add ModelData.has_model cache for ModelStorage.check_xml_record * Limit authentication attempt per IP network * Manage 'X-Forwarded' headers from proxies diff --git a/doc/topics/views/index.rst b/doc/topics/views/index.rst index 7c0d3edce8..28b0218897 100644 --- a/doc/topics/views/index.rst +++ b/doc/topics/views/index.rst @@ -129,6 +129,9 @@ List of attributes shared by many form elements: * ``pre_validate``: Only for button, it contains a domain to apply on the record before calling the button. + * ``depends``: Only for button, it must return the list of field on + which the button depends. + .. _common-attributes-help: * ``help``: The string that will be displayed when the cursor hovers over diff --git a/trytond/ir/model.py b/trytond/ir/model.py index 2d5b2beca8..05d0fc9f86 100644 --- a/trytond/ir/model.py +++ b/trytond/ir/model.py @@ -1035,6 +1035,7 @@ class ModelData(ModelSQL, ModelView): cls._buttons.update({ 'sync': { 'invisible': ~Eval('out_of_sync'), + 'depends': ['out_of_sync'], }, }) diff --git a/trytond/ir/module.py b/trytond/ir/module.py index c5d0d3ae75..f12e65bd2c 100644 --- a/trytond/ir/module.py +++ b/trytond/ir/module.py @@ -76,21 +76,27 @@ class Module(ModelSQL, ModelView): cls._buttons.update({ 'activate': { 'invisible': Eval('state') != 'not activated', + 'depends': ['state'], }, 'activate_cancel': { 'invisible': Eval('state') != 'to activate', + 'depends': ['state'], }, 'deactivate': { 'invisible': Eval('state') != 'activated', + 'depends': ['state'], }, 'deactivate_cancel': { 'invisible': Eval('state') != 'to remove', + 'depends': ['state'], }, 'upgrade': { 'invisible': Eval('state') != 'activated', + 'depends': ['state'], }, 'upgrade_cancel': { 'invisible': Eval('state') != 'to upgrade', + 'depends': ['state'], }, }) diff --git a/trytond/ir/ui/view.py b/trytond/ir/ui/view.py index 23324ca3a7..b70e7f2f59 100644 --- a/trytond/ir/ui/view.py +++ b/trytond/ir/ui/view.py @@ -76,6 +76,7 @@ class View(ModelSQL, ModelView): cls._buttons.update({ 'show': { 'readonly': Eval('type') != 'form', + 'depends': ['type'], }, }) diff --git a/trytond/model/modelview.py b/trytond/model/modelview.py index 2c8949b6e0..7a791efe53 100644 --- a/trytond/model/modelview.py +++ b/trytond/model/modelview.py @@ -560,6 +560,9 @@ class ModelView(Model): else: element.set('type', 'instance') + for depend in states.get('depends', []): + fields_attrs.setdefault(depend, {}) + # translate view if Transaction().language != 'en': for attr in ('string', 'sum', 'confirm', 'help'): diff --git a/trytond/res/user.py b/trytond/res/user.py index 83be90843a..b9d54cc6e4 100644 --- a/trytond/res/user.py +++ b/trytond/res/user.py @@ -825,9 +825,11 @@ class UserApplication(Workflow, ModelSQL, ModelView): cls._buttons.update({ 'validate_': { 'invisible': Eval('state') != 'requested', + 'depends': ['state'], }, 'cancel': { 'invisible': Eval('state') == 'cancelled', + 'depends': ['state'], }, }) diff --git a/trytond/tests/test_tryton.py b/trytond/tests/test_tryton.py index a389a6d5bc..c47389e182 100644 --- a/trytond/tests/test_tryton.py +++ b/trytond/tests/test_tryton.py @@ -22,7 +22,7 @@ from sql import Table from trytond.pool import Pool, isregisteredby from trytond import backend -from trytond.model import Workflow, ModelSQL, ModelSingleton, fields +from trytond.model import Workflow, ModelSQL, ModelSingleton, ModelView, fields from trytond.model.fields import get_eval_fields, Function from trytond.tools import is_instance_method from trytond.transaction import Transaction @@ -308,6 +308,12 @@ class ModuleTestCase(unittest.TestCase): assert depends <= set(model._fields), ( 'Unknown depends %s in "%s"."%s"' % ( list(depends - set(model._fields)), mname, fname)) + if issubclass(model, ModelView): + for bname, button in model._buttons.iteritems(): + depends = set(button.get('depends', [])) + assert depends <= set(model._fields), ( + 'Unknown depends %s in button "%s"."%s"' % ( + list(depends - set(model._fields)), mname, bname)) @with_transaction() def test_field_methods(self):