diff --git a/proteus/CHANGELOG b/proteus/CHANGELOG index eebdfe1b22..e156220393 100644 --- a/proteus/CHANGELOG +++ b/proteus/CHANGELOG @@ -1,3 +1,4 @@ +* Add launch_action helper function Version 7.4.0 - 2024-11-04 -------------------------- diff --git a/proteus/proteus/__init__.py b/proteus/proteus/__init__.py index 8964f24e16..9dd01c1554 100644 --- a/proteus/proteus/__init__.py +++ b/proteus/proteus/__init__.py @@ -1312,6 +1312,28 @@ class Report(object): return self._proxy.execute(ids, data, self._context) +def launch_action(xml_id, records, context=None, config=None): + if records: + assert len({type(x) for x in records}) == 1 + if context is None: + context = {} + + if isinstance(xml_id, str): + ModelData = Model.get('ir.model.data') + + if not config: + config = proteus.config.get_config() + context = config.context + + action_id = ModelData.get_id(*xml_id.split('.'), context) + elif isinstance(xml_id, int): + action_id = xml_id + + Action = Model.get('ir.action') + action = Action.get_action_value(action_id, context) + return _convert_action(action, records, context=context, config=config) + + def _convert_action(action, data=None, *, context=None, config=None): if config is None: config = proteus.config.get_config() diff --git a/proteus/proteus/tests/test_action.py b/proteus/proteus/tests/test_action.py new file mode 100644 index 0000000000..06096cff3c --- /dev/null +++ b/proteus/proteus/tests/test_action.py @@ -0,0 +1,49 @@ +# This file is part of Tryton. The COPYRIGHT file at the top level of +# this repository contains the full copyright notices and license terms. + +import unittest + +try: + import pydot +except ImportError: + pydot = None + +from proteus import Model, Wizard, launch_action + +from .common import ProteusTestCase + + +class TestAction(ProteusTestCase): + + def test_act_window(self): + IrModel = Model.get('ir.model') + actions = IrModel.find([ + ('name', '=', 'ir.action'), + ]) + model_access = launch_action( + 'ir.act_model_access_form_relate_model', actions) + self.assertEqual( + {m.__class__.__name__ for m in model_access}, + {'ir.model.access'}) + + def test_wizard(self): + IrModel = Model.get('ir.model') + actions = IrModel.find([ + ('name', '=', 'ir.action'), + ]) + wizard = launch_action('ir.print_model_graph', actions) + self.assertIsInstance(wizard, Wizard) + self.assertEqual(wizard.name, 'ir.model.print_model_graph') + + @unittest.skipIf(not pydot, 'requires pydot') + def test_report(self): + IrModel = Model.get('ir.model') + actions = IrModel.find([ + ('name', '=', 'ir.action'), + ]) + ftype, data, direct_print, report_name = launch_action( + 'ir.report_model_workflow_graph', actions) + self.assertIsInstance(ftype, str) + self.assertIsInstance(data, bytes) + self.assertIsInstance(direct_print, bool) + self.assertIsInstance(report_name, str)