changeset 30fb7fbf4767 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=30fb7fbf4767
description:
        Update button_action with returned values

        issue8766
        review268241002
diffstat:

 CHANGELOG                       |   1 +
 doc/ref/models/models.rst       |   2 +-
 trytond/ir/action.py            |  13 +++++++------
 trytond/model/modelview.py      |   9 +++++++--
 trytond/res/user.py             |   5 +----
 trytond/tests/modelview.py      |  23 +++++++++++++++++++++++
 trytond/tests/modelview.xml     |   5 +++++
 trytond/tests/test_modelview.py |  20 ++++++++++++++++++++
 trytond/wizard/wizard.py        |   5 ++---
 9 files changed, 67 insertions(+), 16 deletions(-)

diffs (221 lines):

diff -r 61b2505b4fa3 -r 30fb7fbf4767 CHANGELOG
--- a/CHANGELOG Sat Dec 07 15:39:23 2019 +0100
+++ b/CHANGELOG Sat Dec 07 16:03:36 2019 +0100
@@ -1,3 +1,4 @@
+* Update button_action with returned values
 * Use direct access to backend classes
 * Add weasyprint support
 * Add slugify tool
diff -r 61b2505b4fa3 -r 30fb7fbf4767 doc/ref/models/models.rst
--- a/doc/ref/models/models.rst Sat Dec 07 15:39:23 2019 +0100
+++ b/doc/ref/models/models.rst Sat Dec 07 16:03:36 2019 +0100
@@ -111,7 +111,7 @@
 .. staticmethod:: ModelView.button_action(action)
 
     Same as :meth:`ModelView.button` but return the action id of the XML `id`
-    action.
+    action or the action value updated by the returned value of the method.
 
 .. staticmethod:: ModelView.button_change([\*fields])
 
diff -r 61b2505b4fa3 -r 30fb7fbf4767 trytond/ir/action.py
--- a/trytond/ir/action.py      Sat Dec 07 15:39:23 2019 +0100
+++ b/trytond/ir/action.py      Sat Dec 07 16:03:36 2019 +0100
@@ -70,7 +70,7 @@
     def __setup__(cls):
         super(Action, cls).__setup__()
         cls.__rpc__.update({
-                'get_action_id': RPC(),
+                'get_action_value': RPC(instantiate=1, cache=dict(days=1)),
                 })
 
     @staticmethod
@@ -118,6 +118,10 @@
         columns.difference_update(to_remove)
         return Action.read(action_ids, list(columns))
 
+    def get_action_value(self):
+        return self.get_action_values(
+            self.type, [self.get_action_id(self.id)])[0]
+
 
 class ActionKeyword(ModelSQL, ModelView):
     "Action keyword"
@@ -276,9 +280,6 @@
                 if getattr(Action, default_func, None):
                     setattr(cls, default_func,
                         partial(ActionMixin._default_action, name))
-        cls.__rpc__.update({
-                'fetch_action': RPC(cache=dict(days=1)),
-                })
 
     @staticmethod
     def _default_action(name):
@@ -730,7 +731,7 @@
     def __setup__(cls):
         super(ActionActWindow, cls).__setup__()
         cls.__rpc__.update({
-                'get': RPC(),
+                'get': RPC(cache=dict(days=1)),
                 })
 
     @classmethod
@@ -901,7 +902,7 @@
             action_id = ModelData.get_id(*xml_id.split('.'))
         else:
             action_id = int(xml_id)
-        return Action.get_action_values(cls.__name__, [action_id])[0]
+        return Action(action_id).get_action_value()
 
 
 class ActionActWindowView(
diff -r 61b2505b4fa3 -r 30fb7fbf4767 trytond/model/modelview.py
--- a/trytond/model/modelview.py        Sat Dec 07 15:39:23 2019 +0100
+++ b/trytond/model/modelview.py        Sat Dec 07 16:03:36 2019 +0100
@@ -704,12 +704,17 @@
                 ModelData = pool.get('ir.model.data')
                 Action = pool.get('ir.action')
 
-                func(*args, **kwargs)
+                value = func(*args, **kwargs)
 
                 module, fs_id = action.split('.')
                 action_id = Action.get_action_id(
                     ModelData.get_id(module, fs_id))
-                return action_id
+                if value:
+                    action_value = Action(action_id).get_action_value()
+                    action_value.update(value)
+                    return action_value
+                else:
+                    return action_id
             return wrapper
         return decorator
 
diff -r 61b2505b4fa3 -r 30fb7fbf4767 trytond/res/user.py
--- a/trytond/res/user.py       Sat Dec 07 15:39:23 2019 +0100
+++ b/trytond/res/user.py       Sat Dec 07 16:03:36 2019 +0100
@@ -222,11 +222,8 @@
         return None
 
     def get_pyson_menu(self, name):
-        pool = Pool()
-        Action = pool.get('ir.action')
         encoder = PYSONEncoder()
-        return encoder.encode(
-            Action.get_action_values(self.menu.type, [self.menu.id])[0])
+        return encoder.encode(self.menu.get_action_value())
 
     def get_language_direction(self, name):
         pool = Pool()
diff -r 61b2505b4fa3 -r 30fb7fbf4767 trytond/tests/modelview.py
--- a/trytond/tests/modelview.py        Sat Dec 07 15:39:23 2019 +0100
+++ b/trytond/tests/modelview.py        Sat Dec 07 16:03:36 2019 +0100
@@ -76,6 +76,28 @@
         pass
 
 
+class ModelViewButtonAction(ModelView):
+    'ModelView Button Action'
+    __name__ = 'test.modelview.button_action'
+
+    @classmethod
+    def __setup__(cls):
+        super().__setup__()
+        cls._buttons = {
+            'test': {},
+            }
+
+    @classmethod
+    @ModelView.button_action('tests.test_modelview_button_action')
+    def test(cls, records):
+        pass
+
+    @classmethod
+    @ModelView.button_action('tests.test_modelview_button_action')
+    def test_update(cls, records):
+        return {'url': 'http://www.tryton.org/'}
+
+
 class ModelViewRPC(ModelView):
     'ModelView RPC'
     __name__ = 'test.modelview.rpc'
@@ -149,6 +171,7 @@
         ModelViewChangedValuesStoredTarget,
         ModelViewButton,
         ModelViewButtonDepends,
+        ModelViewButtonAction,
         ModelViewRPC,
         ModelViewEmptyPage,
         ModelViewCircularDepends,
diff -r 61b2505b4fa3 -r 30fb7fbf4767 trytond/tests/modelview.xml
--- a/trytond/tests/modelview.xml       Sat Dec 07 15:39:23 2019 +0100
+++ b/trytond/tests/modelview.xml       Sat Dec 07 16:03:36 2019 +0100
@@ -29,4 +29,9 @@
             ]]>
         </field>
     </record>
+
+    <record model="ir.action.url" id="test_modelview_button_action">
+        <field name="name">Test Button Action</field>
+        <field name="url">http://www.example.com/</field>
+    </record>
 </tryton>
diff -r 61b2505b4fa3 -r 30fb7fbf4767 trytond/tests/test_modelview.py
--- a/trytond/tests/test_modelview.py   Sat Dec 07 15:39:23 2019 +0100
+++ b/trytond/tests/test_modelview.py   Sat Dec 07 16:03:36 2019 +0100
@@ -328,6 +328,26 @@
         self.assertTrue(rule.test(record, clicks))
 
     @with_transaction()
+    def test_button_action(self):
+        "Test button action"
+        pool = Pool()
+        TestModel = pool.get('test.modelview.button_action')
+
+        action_id = TestModel.test([])
+
+        self.assertIsInstance(action_id, int)
+
+    @with_transaction()
+    def test_button_action_return(self):
+        "Test button action update value"
+        pool = Pool()
+        TestModel = pool.get('test.modelview.button_action')
+
+        action = TestModel.test_update([])
+
+        self.assertEqual(action['url'], 'http://www.tryton.org/')
+
+    @with_transaction()
     def test_rpc_setup(self):
         "Testing the computation of the RPC methods"
         pool = Pool()
diff -r 61b2505b4fa3 -r 30fb7fbf4767 trytond/wizard/wizard.py
--- a/trytond/wizard/wizard.py  Sat Dec 07 15:39:23 2019 +0100
+++ b/trytond/wizard/wizard.py  Sat Dec 07 16:03:36 2019 +0100
@@ -159,7 +159,7 @@
         action_id = Action.get_action_id(
             ModelData.get_id(module, fs_id))
         action = Action(action_id)
-        return Action.get_action_values(action.type, [action.id])[0]
+        return action.get_action_value()
 
 
 class StateReport(StateAction):
@@ -173,14 +173,13 @@
         'Return report definition'
         pool = Pool()
         ActionReport = pool.get('ir.action.report')
-        Action = pool.get('ir.action')
         action_reports = ActionReport.search([
                 ('report_name', '=', self.report_name),
                 ])
         assert action_reports, '%s not found' % self.report_name
         action_report = action_reports[0]
         action = action_report.action
-        return Action.get_action_values(action.type, [action.id])[0]
+        return action.get_action_value()
 
 
 class Wizard(URLMixin, PoolBase):

Reply via email to