Skip to content

Commit

Permalink
Merge PR #8 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Sep 12, 2024
2 parents dd7b35b + 6674669 commit 8a15246
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
4 changes: 3 additions & 1 deletion automation_oca/demo/demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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'))]"
/>
<field name="field_id" ref="base.field_res_partner__email" />
<field name="editable_domain">[('email', '!=', False)]</field>
<field
name="editable_domain"
>[ ("email", "!=", False), ("create_date", "&lt;=", datetime.datetime.now())]</field>
</record>
<record id="demo_configuration_welcome_send" model="automation.configuration.step">
<field name="name">Send email</field>
Expand Down
46 changes: 42 additions & 4 deletions automation_oca/models/automation_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -19,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)],
Expand Down Expand Up @@ -184,14 +209,27 @@ 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.
Also, we need to check by autencity field if defined.
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
Expand Down

0 comments on commit 8a15246

Please sign in to comment.