From d3c2d86010ce7c1d22d79142df16fc69ffc474f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 28 Mar 2020 23:56:19 +0100 Subject: [PATCH] Simplify trigger action We use now a selection field to choose the action to trigger. It is safer and more user friendly. issue9096 review272961003 --- ir.py | 34 +++++++++++++++----------------- tests/test_notification_email.py | 18 ++--------------- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/ir.py b/ir.py index 993114cff1..ae4c92bc8c 100644 --- a/ir.py +++ b/ir.py @@ -8,25 +8,27 @@ from trytond.pyson import Eval class Trigger(metaclass=PoolMeta): __name__ = 'ir.trigger' - _required = ((Eval('action_function') == 'trigger') - & (Eval('action_model_name') == 'notification.email')) - notification_email = fields.Many2One( 'notification.email', "Email Notification", readonly=True, states={ - 'required': _required, - 'invisible': ~_required, + 'required': Eval('notification_email_required', False), + 'invisible': ~Eval('notification_email_required', False), }, - depends=['action_function', 'action_model_name']) - action_model_name = fields.Function( - fields.Char("Action Model Name"), 'on_change_with_action_model_name') + depends=['notification_email_required']) + notification_email_required = fields.Function( + fields.Boolean("Notification Email Required"), + 'on_change_with_notification_email_required') - del _required + @classmethod + def __setup__(cls): + super().__setup__() + cls.action.selection.append( + ('notification.email|trigger', "Email Notification"), + ) - @fields.depends('action_model') - def on_change_with_action_model_name(self, name=None): - if self.action_model: - return self.action_model.model + @fields.depends('action') + def on_change_with_notification_email_required(self, name=None): + return self.action == 'notification.email|trigger' @fields.depends('notification_email', '_parent_notification_email.model') def on_change_notification_email(self): @@ -41,8 +43,4 @@ class Trigger(metaclass=PoolMeta): pass else: self.model = trigger_model - notification_model, = Model.search([ - ('model', '=', 'notification.email'), - ]) - self.action_model = notification_model - self.action_function = 'trigger' + self.action = 'notification.email|trigger' diff --git a/tests/test_notification_email.py b/tests/test_notification_email.py index 384ded2cbb..903ea2fef7 100644 --- a/tests/test_notification_email.py +++ b/tests/test_notification_email.py @@ -38,9 +38,6 @@ class NotificationEmailTestCase(ModuleTestCase): model, = Model.search([ ('model', '=', User.__name__), ]) - action_model, = Model.search([ - ('model', '=', 'notification.email'), - ]) action = Action(name="Notification Email", type='ir.action.report') action.save() @@ -91,17 +88,13 @@ class NotificationEmailTestCase(ModuleTestCase): model, = Model.search([ ('model', '=', User.__name__), ]) - action_model, = Model.search([ - ('model', '=', 'notification.email'), - ]) Trigger.create([{ 'name': 'Test creation', 'model': model.id, 'on_create': True, 'condition': 'true', 'notification_email': notification_email.id, - 'action_model': action_model.id, - 'action_function': 'trigger', + 'action': 'notification.email|trigger', }]) with patch.object( @@ -141,9 +134,6 @@ class NotificationEmailTestCase(ModuleTestCase): model, = Model.search([ ('model', '=', User.__name__), ]) - action_model, = Model.search([ - ('model', '=', 'notification.email'), - ]) en, = Language.search([('code', '=', 'en')]) attachment = Report() @@ -205,17 +195,13 @@ class NotificationEmailTestCase(ModuleTestCase): model, = Model.search([ ('model', '=', User.__name__), ]) - action_model, = Model.search([ - ('model', '=', 'notification.email'), - ]) Trigger.create([{ 'name': 'Test creation', 'model': model.id, 'on_create': True, 'condition': 'true', 'notification_email': notification_email.id, - 'action_model': action_model.id, - 'action_function': 'trigger', + 'action': 'notification.email|trigger', }]) with patch.object(