Skip to content

Commit

Permalink
edi_oca: add edi.configuration.trigger
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
simahawk committed Nov 15, 2024
1 parent 0f1f6cc commit e7f9217
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 38 deletions.
1 change: 1 addition & 0 deletions edi_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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/menuitems.xml",
"templates/exchange_chatter_msg.xml",
"templates/exchange_mixin_buttons.xml",
Expand Down
43 changes: 31 additions & 12 deletions edi_oca/data/edi_configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@
Examlple:
<field name="snippet_do">record._edi_send_via_email(ir_action)</field>
-->
<record id="edi_conf_send_via_email" model="edi.configuration">
<field name="name">Send Via Email</field>
<field name="active">False</field>
<field name="code">on_send_via_email</field>
<field name="trigger">on_send_via_email</field>
<field name="snippet_do">record._edi_send_via_email()</field>

<record id="edi_conf_trigger_record_create" model="edi.configuration.trigger">
<field name="name">On record create</field>
<field name="code">on_record_create</field>
<field name="description">Trigger when a record is created</field>
</record>
<record id="edi_conf_trigger_record_write" model="edi.configuration.trigger">
<field name="name">On record write</field>
<field name="code">on_record_write</field>
<field name="description">Trigger when a record is updated</field>
</record>
<!-- TODO: these 2 have to be triggered somehow -->
<record id="edi_conf_trigger_send_via_email" model="edi.configuration.trigger">
<field name="name">Send via email</field>
<field name="code">on_send_via_email</field>
<field name="description">Send record via email TBD</field>
</record>
<record id="edi_conf_trigger_send_via_edi" model="edi.configuration.trigger">
<field name="name">Send via EDI</field>
<field name="code">on_send_via_edi</field>
<field name="description">Send record via EDI TBD</field>
</record>

<!-- Add type_id to use Send Via EDI -->
<record id="edi_conf_send_via_email" model="edi.configuration">
<field name="name">Send Via Email</field>
<field name="active">False</field>
<field name="trigger_id" ref="edi_conf_trigger_send_via_email" />
<field name="snippet_do">record._edi_send_via_email()</field>
</record>
<record id="edi_conf_send_via_edi" model="edi.configuration">
<field name="name">Send Via EDI</field>
<field name="active">False</field>
<field name="code">on_send_via_edi</field>
<field name="trigger">on_send_via_edi</field>
<field name="snippet_do">record._edi_send_via_edi(conf.type_id)</field>
<field name="name">Send Via EDI</field>
<field name="active">False</field>
<field name="trigger_id" ref="edi_conf_trigger_send_via_edi" />
<field name="snippet_do">record._edi_send_via_edi(conf.type_id)</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions edi_oca/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 5 additions & 16 deletions edi_oca/models/edi_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions edi_oca/models/edi_configuration_trigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Camptocamp SA
# @author Simone Orsi <[email protected]>
# 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")]
6 changes: 2 additions & 4 deletions edi_oca/tests/test_edi_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
}
Expand All @@ -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)",
}
Expand Down
44 changes: 44 additions & 0 deletions edi_oca/views/edi_configuration_trigger_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="edi_configuration_trigger_view_tree" model="ir.ui.view">
<field name="model">edi.configuration.trigger</field>
<field name="arch" type="xml">
<tree>
<field name="name" />
<field name="description" />
<field name="code" />
<field name="model_id" />
<field name="active" />
</tree>
</field>
</record>
<record id="edi_configuration_trigger_view_form" model="ir.ui.view">
<field name="model">edi.configuration.trigger</field>
<field name="arch" type="xml">
<form>
<sheet>
<field name="active" invisible="1" />
<widget
name="web_ribbon"
title="Archived"
bg_color="bg-danger"
attrs="{'invisible': [('active', '=', True)]}"
/>
<group>
<group>
<field name="name" />
<field name="code" />
<field name="description" />
</group>
<group>
<field
name="model_id"
options="{'no_create': True, 'no_create_edit': True}"
/>
</group>
</group>
</sheet>
</form>
</field>
</record>
</odoo>
12 changes: 6 additions & 6 deletions edi_oca/views/edi_configuration_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<field name="model_id" />
<field name="active" />
<field name="type_id" />
<field name="trigger" />
<field name="trigger_id" />
<filter
name="active"
string="Active"
Expand All @@ -28,9 +28,8 @@
<field name="arch" type="xml">
<tree>
<field name="name" />
<field name="code" />
<field name="model_id" />
<field name="trigger" />
<field name="trigger_id" />
<field name="backend_id" />
<field name="type_id" />
<field name="description" />
Expand All @@ -53,14 +52,15 @@
<group>
<group>
<field name="name" />
<field name="active" />
<field name="description" />
<field name="backend_id" />
<field name="type_id" />
<field name="code" />
</group>
<group>
<field name="trigger" />
<field
name="trigger_id"
options="{'no_create': True, 'no_create_edit': True}"
/>
<field
name="model_id"
options="{'no_create': True, 'no_create_edit': True}"
Expand Down

0 comments on commit e7f9217

Please sign in to comment.