From 2100849abf0d38926bf43784fe6896b27cc68ef0 Mon Sep 17 00:00:00 2001 From: gaelTorrecillas Date: Thu, 5 Sep 2024 11:42:38 +0200 Subject: [PATCH 1/2] [IMP] automation_oca: Allow some expressions in domain This commit adds the corresponding imports and preparation code for using some variables and expressions like `today()`, `datetime`, `user`, etc in the domain (filter) of the automation configuration. --- .../models/automation_configuration.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/automation_oca/models/automation_configuration.py b/automation_oca/models/automation_configuration.py index 26c1e1f..70e6bf4 100644 --- a/automation_oca/models/automation_configuration.py +++ b/automation_oca/models/automation_configuration.py @@ -5,7 +5,12 @@ from odoo import _, api, fields, models from odoo.exceptions import ValidationError -from odoo.tools.safe_eval import safe_eval +from odoo.tools.safe_eval import ( + datetime as safe_datetime, + dateutil as safe_dateutil, + safe_eval, + time as safe_time, +) class AutomationConfiguration(models.Model): @@ -184,6 +189,18 @@ def cron_automation(self): for record in self.search([("state", "=", "periodic")]): record.run_automation() + def _get_eval_context(self): + """Prepare the context used when evaluating python code + :returns: dict -- evaluation context given to safe_eval + """ + return { + "ref": self.env.ref, + "user": self.env.user, + "time": safe_time, + "datetime": safe_datetime, + "dateutil": safe_dateutil, + } + def _get_automation_records_to_create(self): """ We will find all the records that fulfill the domain but don't have a record created. @@ -191,7 +208,8 @@ def _get_automation_records_to_create(self): In order to do this, we will add some extra joins on the query of the domain """ - domain = safe_eval(self.domain) + eval_context = self._get_eval_context() + domain = safe_eval(self.domain, eval_context) Record = self.env[self.model_id.model] if self.company_id and "company_id" in Record._fields: # In case of company defined, we add only if the records have company field From 66746698b4c9439257472831e1ab43b2882ed955 Mon Sep 17 00:00:00 2001 From: gaelTorrecillas Date: Wed, 11 Sep 2024 17:41:17 +0200 Subject: [PATCH 2/2] [16.0] automation_oca : add demo and help for new domain options --- automation_oca/demo/demo.xml | 4 +++- .../models/automation_configuration.py | 24 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/automation_oca/demo/demo.xml b/automation_oca/demo/demo.xml index a5d38b7..a3dd5d0 100644 --- a/automation_oca/demo/demo.xml +++ b/automation_oca/demo/demo.xml @@ -36,7 +36,9 @@ for record in records.filtered(lambda r: not r.is_blacklisted): eval="[(4, ref('demo_tag_process')), (4, ref('demo_tag_demo'))]" /> - [('email', '!=', False)] + [ ("email", "!=", False), ("create_date", "<=", datetime.datetime.now())] Send email diff --git a/automation_oca/models/automation_configuration.py b/automation_oca/models/automation_configuration.py index 70e6bf4..89b4b85 100644 --- a/automation_oca/models/automation_configuration.py +++ b/automation_oca/models/automation_configuration.py @@ -24,9 +24,29 @@ class AutomationConfiguration(models.Model): tag_ids = fields.Many2many("automation.tag") company_id = fields.Many2one("res.company") domain = fields.Char( - required=True, default="[]", help="Filter to apply", compute="_compute_domain" + required=True, + default="[]", + compute="_compute_domain", + help=""" + Filter to apply + Following special variable can be used in filter : + * datetime + * dateutil + * time + * user + * ref """, + ) + editable_domain = fields.Char( + required=True, + default="[]", + help="""Filter to apply + Following special variable can be used in filter : + * datetime + * dateutil + * time + * user + * ref """, ) - editable_domain = fields.Char(required=True, default="[]", help="Filter to apply") model_id = fields.Many2one( "ir.model", domain=[("is_mail_thread", "=", True)],