changeset bdcbaffd85d4 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=bdcbaffd85d4
description:
        Simplify trigger action

        We use now a selection field to choose the action to trigger.
        It is safer and more user friendly.

        issue9096
        review272961003
diffstat:

 CHANGELOG                        |   1 +
 trytond/ir/trigger.py            |  26 ++++++++++++++++++++++----
 trytond/ir/view/trigger_form.xml |   6 ++----
 trytond/ir/view/trigger_list.xml |   1 +
 trytond/tests/test_trigger.py    |  30 +++++-------------------------
 trytond/tests/trigger.py         |  13 ++++++++++++-
 6 files changed, 43 insertions(+), 34 deletions(-)

diffs (235 lines):

diff -r 9a93b7d87ca2 -r bdcbaffd85d4 CHANGELOG
--- a/CHANGELOG Sat Mar 28 23:37:53 2020 +0100
+++ b/CHANGELOG Sat Mar 28 23:56:19 2020 +0100
@@ -1,3 +1,4 @@
+* Simplify trigger action
 * Run trigger in the queue
 * Do not copy Binary content with file_id
 * Replace memoize with functools.lru_cache
diff -r 9a93b7d87ca2 -r bdcbaffd85d4 trytond/ir/trigger.py
--- a/trytond/ir/trigger.py     Sat Mar 28 23:37:53 2020 +0100
+++ b/trytond/ir/trigger.py     Sat Mar 28 23:56:19 2020 +0100
@@ -5,6 +5,7 @@
 from sql import Literal, Null, Select
 from sql.aggregate import Count, Max
 from sql.functions import CurrentTimestamp
+from sql.operators import Concat
 
 from trytond.model.exceptions import ValidationError
 from trytond.i18n import gettext
@@ -55,8 +56,7 @@
         help='Set a minimum time delay between call to "Action Function" '
         'for the same record.\n'
         'empty for no delay.')
-    action_model = fields.Many2One('ir.model', 'Action Model', required=True)
-    action_function = fields.Char('Action Function', required=True)
+    action = fields.Selection([], "Action", required=True)
     _get_triggers_cache = Cache('ir_trigger.get_triggers')
 
     @classmethod
@@ -81,6 +81,8 @@
 
         super(Trigger, cls).__register__(module_name)
 
+        table_h = cls.__table_handler__(module_name)
+
         # Migration from 3.4:
         # change minimum_delay into timedelta minimum_time_delay
         if table.column_exist('minimum_delay'):
@@ -95,6 +97,21 @@
                         where=sql_table.id == id_))
             table.drop_column('minimum_delay')
 
+        # Migration from 5.4: merge action
+        if (table_h.column_exist('action_model')
+                and table_h.column_exist('action_function')):
+            pool = Pool()
+            Model = pool.get('ir.model')
+            model = Model.__table__()
+            action_model = model.select(
+                model.model, where=model.id == sql_table.action_model)
+            cursor.execute(*sql_table.update(
+                    [sql_table.action],
+                    [Concat(action_model, Concat(
+                                '|', sql_table.action_function))]))
+            table_h.drop_column('action_model')
+            table_h.drop_column('action_function')
+
     @classmethod
     def validate(cls, triggers):
         super(Trigger, cls).validate(triggers)
@@ -187,7 +204,8 @@
         pool = Pool()
         TriggerLog = pool.get('ir.trigger.log')
         Model = pool.get(self.model.model)
-        ActionModel = pool.get(self.action_model.model)
+        model, method = self.action.split('|')
+        ActionModel = pool.get(model)
         cursor = Transaction().connection.cursor()
         trigger_log = TriggerLog.__table__()
 
@@ -255,7 +273,7 @@
 
         records = Model.browse(ids)
         if records:
-            getattr(ActionModel, self.action_function)(records, self)
+            getattr(ActionModel, method)(records, self)
         if self.limit_number or self.minimum_time_delay:
             to_create = []
             for record in records:
diff -r 9a93b7d87ca2 -r bdcbaffd85d4 trytond/ir/view/trigger_form.xml
--- a/trytond/ir/view/trigger_form.xml  Sat Mar 28 23:37:53 2020 +0100
+++ b/trytond/ir/view/trigger_form.xml  Sat Mar 28 23:56:19 2020 +0100
@@ -24,8 +24,6 @@
     <field name="limit_number"/>
     <label name="minimum_time_delay"/>
     <field name="minimum_time_delay"/>
-    <label name="action_model"/>
-    <field name="action_model"/>
-    <label name="action_function"/>
-    <field name="action_function"/>
+    <label name="action"/>
+    <field name="action"/>
 </form>
diff -r 9a93b7d87ca2 -r bdcbaffd85d4 trytond/ir/view/trigger_list.xml
--- a/trytond/ir/view/trigger_list.xml  Sat Mar 28 23:37:53 2020 +0100
+++ b/trytond/ir/view/trigger_list.xml  Sat Mar 28 23:56:19 2020 +0100
@@ -8,4 +8,5 @@
     <field name="on_create"/>
     <field name="on_write"/>
     <field name="on_delete"/>
+    <field name="action"/>
 </tree>
diff -r 9a93b7d87ca2 -r bdcbaffd85d4 trytond/tests/test_trigger.py
--- a/trytond/tests/test_trigger.py     Sat Mar 28 23:37:53 2020 +0100
+++ b/trytond/tests/test_trigger.py     Sat Mar 28 23:56:19 2020 +0100
@@ -45,17 +45,13 @@
         model, = Model.search([
                 ('model', '=', 'test.triggered'),
                 ])
-        action_model, = Model.search([
-                ('model', '=', 'test.trigger_action'),
-                ])
 
         values = {
             'name': 'Test',
             'model': model.id,
             'on_time': True,
             'condition': 'true',
-            'action_model': action_model.id,
-            'action_function': 'test',
+            'action': 'test.trigger_action|trigger',
             }
         self.assertTrue(Trigger.create([values]))
 
@@ -93,17 +89,13 @@
         model, = Model.search([
                 ('model', '=', 'test.triggered'),
                 ])
-        action_model, = Model.search([
-                ('model', '=', 'test.trigger_action'),
-                ])
 
         trigger, = Trigger.create([{
                     'name': 'Test',
                     'model': model.id,
                     'on_create': True,
                     'condition': 'true',
-                    'action_model': action_model.id,
-                    'action_function': 'trigger',
+                    'action': 'test.trigger_action|trigger',
                     }])
 
         triggered, = Triggered.create([{
@@ -175,17 +167,13 @@
         model, = Model.search([
                 ('model', '=', 'test.triggered'),
                 ])
-        action_model, = Model.search([
-                ('model', '=', 'test.trigger_action'),
-                ])
 
         trigger, = Trigger.create([{
                     'name': 'Test',
                     'model': model.id,
                     'on_write': True,
                     'condition': 'true',
-                    'action_model': action_model.id,
-                    'action_function': 'trigger',
+                    'action': 'test.trigger_action|trigger',
                     }])
 
         triggered, = Triggered.create([{
@@ -311,9 +299,6 @@
         model, = Model.search([
                 ('model', '=', 'test.triggered'),
                 ])
-        action_model, = Model.search([
-                ('model', '=', 'test.trigger_action'),
-                ])
 
         triggered, = Triggered.create([{
                     'name': 'Test',
@@ -324,8 +309,7 @@
                     'model': model.id,
                     'on_delete': True,
                     'condition': 'true',
-                    'action_model': action_model.id,
-                    'action_function': 'trigger',
+                    'action': 'test.trigger_action|trigger',
                     }])
 
         Triggered.delete([triggered])
@@ -406,17 +390,13 @@
         model, = Model.search([
                 ('model', '=', 'test.triggered'),
                 ])
-        action_model, = Model.search([
-                ('model', '=', 'test.trigger_action'),
-                ])
 
         trigger, = Trigger.create([{
                     'name': 'Test',
                     'model': model.id,
                     'on_time': True,
                     'condition': 'true',
-                    'action_model': action_model.id,
-                    'action_function': 'trigger',
+                    'action': 'test.trigger_action|trigger',
                     }])
 
         triggered, = Triggered.create([{
diff -r 9a93b7d87ca2 -r bdcbaffd85d4 trytond/tests/trigger.py
--- a/trytond/tests/trigger.py  Sat Mar 28 23:37:53 2020 +0100
+++ b/trytond/tests/trigger.py  Sat Mar 28 23:56:19 2020 +0100
@@ -1,11 +1,21 @@
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 from trytond.model import ModelSQL, fields
-from trytond.pool import Pool
+from trytond.pool import Pool, PoolMeta
 
 TRIGGER_LOGS = []
 
 
+class Trigger(metaclass=PoolMeta):
+    __name__ = 'ir.trigger'
+
+    @classmethod
+    def __setup__(cls):
+        super().__setup__()
+        cls.action.selection.append(
+            ('test.trigger_action|trigger', "Test Trigger"))
+
+
 class Triggered(ModelSQL):
     'Triggered'
     __name__ = 'test.triggered'
@@ -26,6 +36,7 @@
 
 def register(module):
     Pool.register(
+        Trigger,
         Triggered,
         TriggerAction,
         module=module, type_='model')

Reply via email to