From a1c5ad28b44e34cc07b5c1cc6f086b6baf96678c Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Fri, 15 Nov 2024 09:39:14 +0100 Subject: [PATCH] edi_oca: add edi.configuration.trigger Ease configuration by filtering triggers by model. In the long run we'll have tons of triggers for specific scopes (sale, purchase, etc). This change will make sure the selection of the trigger won't be cluttered. It also adds the possibility to describe triggers as users prefer. --- edi_oca/__manifest__.py | 1 + edi_oca/data/edi_configuration.xml | 43 +++++++++++++----- edi_oca/models/__init__.py | 1 + edi_oca/models/edi_configuration.py | 21 +++------ edi_oca/models/edi_configuration_trigger.py | 24 ++++++++++ edi_oca/tests/test_edi_configuration.py | 6 +-- .../views/edi_configuration_trigger_views.xml | 44 +++++++++++++++++++ edi_oca/views/edi_configuration_views.xml | 12 ++--- 8 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 edi_oca/models/edi_configuration_trigger.py create mode 100644 edi_oca/views/edi_configuration_trigger_views.xml diff --git a/edi_oca/__manifest__.py b/edi_oca/__manifest__.py index bf33064582..ef6f950e64 100644 --- a/edi_oca/__manifest__.py +++ b/edi_oca/__manifest__.py @@ -39,6 +39,7 @@ "views/edi_exchange_type_views.xml", "views/edi_exchange_type_rule_views.xml", "views/edi_configuration_views.xml", + "views/edi_configuration_trigger_views.xml", "views/res_partner.xml", "views/menuitems.xml", "templates/exchange_chatter_msg.xml", diff --git a/edi_oca/data/edi_configuration.xml b/edi_oca/data/edi_configuration.xml index e9784a132c..8623f39ec7 100644 --- a/edi_oca/data/edi_configuration.xml +++ b/edi_oca/data/edi_configuration.xml @@ -4,20 +4,39 @@ Examlple: record._edi_send_via_email(ir_action) --> - - Send Via Email - False - on_send_via_email - on_send_via_email - record._edi_send_via_email() + + + On record create + on_record_create + Trigger when a record is created + + + On record write + on_record_write + Trigger when a record is updated + + + + Send via email + on_send_via_email + Send record via email TBD + + + Send via EDI + on_send_via_edi + Send record via EDI TBD - + + Send Via Email + False + + record._edi_send_via_email() + - Send Via EDI - False - on_send_via_edi - on_send_via_edi - record._edi_send_via_edi(conf.type_id) + Send Via EDI + False + + record._edi_send_via_edi(conf.type_id) diff --git a/edi_oca/models/__init__.py b/edi_oca/models/__init__.py index c5223ae7f6..4c3433a3a8 100644 --- a/edi_oca/models/__init__.py +++ b/edi_oca/models/__init__.py @@ -5,4 +5,5 @@ from . import edi_exchange_type from . import edi_exchange_type_rule from . import edi_id_mixin +from . import edi_configuration_trigger from . import edi_configuration diff --git a/edi_oca/models/edi_configuration.py b/edi_oca/models/edi_configuration.py index d6b693b843..0dbc880031 100644 --- a/edi_oca/models/edi_configuration.py +++ b/edi_oca/models/edi_configuration.py @@ -33,7 +33,6 @@ class EdiConfiguration(models.Model): name = fields.Char(string="Name", required=True) active = fields.Boolean(default=True) - code = fields.Char(required=True, copy=False, index=True, unique=True) description = fields.Char(help="Describe what the conf is for") backend_id = fields.Many2one(string="Backend", comodel_name="edi.backend") # Field `type_id` is not a mandatory field because we will create 2 common confs @@ -52,23 +51,13 @@ class EdiConfiguration(models.Model): help="Model the conf applies to. Leave blank to apply for all models", ) model_name = fields.Char(related="model_id.model", store=True) - trigger = fields.Selection( - # The selections below are intended to assist with basic operations - # and are used to setup common configuration. - [ - ("on_record_write", "Update Record"), - ("on_record_create", "Create Record"), - ("on_send_via_email", "Send Via Email"), - ("on_send_via_edi", "Send Via EDI"), - ("disabled", "Disabled"), - ], + trigger_id = fields.Many2one( string="Trigger", - # The default selection will be disabled. - # which would allow to keep the conf visible but disabled. - required=True, - default="disabled", - ondelete="on default", + comodel_name="edi.configuration.trigger", + help="Trigger that activates this configuration", + domain="['|', ('model_id', '=', model_id), ('model_id', '=', False)]", ) + trigger = fields.Char(related="trigger_id.code") snippet_before_do = fields.Text( string="Snippet Before Do", help="Snippet to validate the state and collect records to do", diff --git a/edi_oca/models/edi_configuration_trigger.py b/edi_oca/models/edi_configuration_trigger.py new file mode 100644 index 0000000000..81f18c16b4 --- /dev/null +++ b/edi_oca/models/edi_configuration_trigger.py @@ -0,0 +1,24 @@ +# Copyright 2024 Camptocamp SA +# @author Simone Orsi +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import fields, models + + +class EdiConfigurationTrigger(models.Model): + _name = "edi.configuration.trigger" + _description = """ + Describe what triggers a specific action for a configuration. + """ + + name = fields.Char(string="Name", required=True) + code = fields.Char(required=True, copy=False) + active = fields.Boolean(default=True) + description = fields.Char(help="Describe what the conf is for") + model_id = fields.Many2one( + "ir.model", + string="Model", + help="Model the conf applies to. Leave blank to apply for all models", + ) + + _sql_constraints = [("code_uniq", "unique(code)", "Code must be unique")] diff --git a/edi_oca/tests/test_edi_configuration.py b/edi_oca/tests/test_edi_configuration.py index 36cfd386d2..d1264979bf 100644 --- a/edi_oca/tests/test_edi_configuration.py +++ b/edi_oca/tests/test_edi_configuration.py @@ -65,10 +65,9 @@ def _setup_records(cls): { "name": "Create Config", "active": True, - "code": "create_config", "backend_id": cls.backend.id, "type_id": cls.exchange_type_out.id, - "trigger": "on_record_create", + "trigger_id": cls.env.ref("edi_oca.edi_conf_trigger_record_create").id, "model_id": cls.env["ir.model"]._get_id("edi.exchange.consumer.test"), "snippet_do": "record._edi_send_via_edi(conf.type_id)", } @@ -77,10 +76,9 @@ def _setup_records(cls): { "name": "Write Config 1", "active": True, - "code": "write_config", "backend_id": cls.backend.id, "type_id": cls.exchange_type_out.id, - "trigger": "on_record_write", + "trigger_id": cls.env.ref("edi_oca.edi_conf_trigger_record_write").id, "model_id": cls.env["ir.model"]._get_id("edi.exchange.consumer.test"), "snippet_do": "record._edi_send_via_edi(conf.type_id)", } diff --git a/edi_oca/views/edi_configuration_trigger_views.xml b/edi_oca/views/edi_configuration_trigger_views.xml new file mode 100644 index 0000000000..e747a62b00 --- /dev/null +++ b/edi_oca/views/edi_configuration_trigger_views.xml @@ -0,0 +1,44 @@ + + + + edi.configuration.trigger + + + + + + + + + + + + edi.configuration.trigger + +
+ + + + + + + + + + + + + + +
+
+
+
diff --git a/edi_oca/views/edi_configuration_views.xml b/edi_oca/views/edi_configuration_views.xml index fed81de213..dd5de3fe9e 100644 --- a/edi_oca/views/edi_configuration_views.xml +++ b/edi_oca/views/edi_configuration_views.xml @@ -8,7 +8,7 @@ - + - - + @@ -53,14 +52,15 @@ - - - +