summaryrefslogtreecommitdiff
path: root/addons/base_automation
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2019-03-13 11:45:01 +0000
committerChristophe Simonis <chs@odoo.com>2019-03-13 14:24:51 +0000
commit44515bc7beae3b4c46b5bc2959c320695b51439d (patch)
tree5f56871242f7b5bc8a323c51f2749411e555159e /addons/base_automation
parentbf69975b54164a21bfe00b2359d972329516a34e (diff)
parentc9f832d9f032b6af51807b387b325b5c2b8aad00 (diff)
[MERGE] forward port branch saas-12.2 up to c9f832d9f03
closes odoo/odoo#31790 Signed-off-by: Christophe Simonis <chs@odoo.com>
Diffstat (limited to 'addons/base_automation')
-rw-r--r--addons/base_automation/data/base_automation_demo.xml15
-rw-r--r--addons/base_automation/models/base_automation.py19
-rw-r--r--addons/base_automation/tests/test_base_automation.py59
3 files changed, 78 insertions, 15 deletions
diff --git a/addons/base_automation/data/base_automation_demo.xml b/addons/base_automation/data/base_automation_demo.xml
index 3e864e34565..7ddc3a74581 100644
--- a/addons/base_automation/data/base_automation_demo.xml
+++ b/addons/base_automation/data/base_automation_demo.xml
@@ -84,4 +84,19 @@ record['name'] = record.name + 'X'
</field>
</record>
+ <record id="test_mail_template_automation" model="mail.template">
+ <field name="name">Template Automation</field>
+ <field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
+ <field name="body_html">&lt;div&gt;Email automation&lt;/div&gt;</field>
+ </record>
+ <record id="test_rule_on_write_recompute_send_email" model="base.automation">
+ <field name="name">Base Automation: test send an email</field>
+ <field name="model_id" ref="base_automation.model_base_automation_lead_test"/>
+ <field name="template_id" ref="base_automation.test_mail_template_automation"/>
+ <field name="state">email</field>
+ <field name="trigger">on_write</field>
+ <field name="active" eval="True"/>
+ <field name="filter_domain">[('deadline', '!=', False)]</field>
+ <field name="filter_pre_domain">[('deadline', '=', False)]</field>
+ </record>
</odoo>
diff --git a/addons/base_automation/models/base_automation.py b/addons/base_automation/models/base_automation.py
index ab460f1e875..e2620d202e7 100644
--- a/addons/base_automation/models/base_automation.py
+++ b/addons/base_automation/models/base_automation.py
@@ -164,14 +164,17 @@ class BaseAutomation(models.Model):
return records
def _filter_post(self, records):
+ return self._filter_post_export_domain(records)[0]
+
+ def _filter_post_export_domain(self, records):
""" Filter the records that satisfy the postcondition of action ``self``. """
if self.filter_domain and records:
domain = [('id', 'in', records.ids)] + safe_eval(self.filter_domain, self._get_eval_context())
- return records.search(domain)
+ return records.search(domain), domain
else:
- return records
+ return records, None
- def _process(self, records):
+ def _process(self, records, domain_post=None):
""" Process action ``self`` on the ``records`` that have not been done yet. """
# filter out the records on which self has already been done
action_done = self._context['__action_done']
@@ -198,7 +201,12 @@ class BaseAutomation(models.Model):
for record in records:
# we process the action if any watched field has been modified
if self._check_trigger_fields(record):
- ctx = {'active_model': record._name, 'active_ids': record.ids, 'active_id': record.id}
+ ctx = {
+ 'active_model': record._name,
+ 'active_ids': record.ids,
+ 'active_id': record.id,
+ 'domain_post': domain_post,
+ }
self.action_server_id.with_context(**ctx).run()
def _check_trigger_fields(self, record):
@@ -274,7 +282,8 @@ class BaseAutomation(models.Model):
_write.origin(records, vals, **kw)
# check postconditions, and execute actions on the records that satisfy them
for action in actions.with_context(old_values=old_values):
- action._process(action._filter_post(pre[action]))
+ records, domain_post = action._filter_post_export_domain(pre[action])
+ action._process(records, domain_post=domain_post)
return True
return _write
diff --git a/addons/base_automation/tests/test_base_automation.py b/addons/base_automation/tests/test_base_automation.py
index 6b34c65c6a7..97fcb2d1827 100644
--- a/addons/base_automation/tests/test_base_automation.py
+++ b/addons/base_automation/tests/test_base_automation.py
@@ -1,5 +1,6 @@
# # -*- coding: utf-8 -*-
# # Part of Odoo. See LICENSE file for full copyright and licensing details.
+from unittest.mock import patch
from odoo.tests import common
@@ -9,13 +10,14 @@ class base_automation_test(common.TransactionCase):
def setUp(self):
super(base_automation_test, self).setUp()
- self.user_admin = self.env.ref('base.user_root')
+ self.user_root = self.env.ref('base.user_root')
+ self.user_admin = self.env.ref('base.user_admin')
self.user_demo = self.env.ref('base.user_demo')
def create_lead(self, **kwargs):
vals = {
'name': "Lead Test",
- 'user_id': self.user_admin.id,
+ 'user_id': self.user_root.id,
}
vals.update(kwargs)
return self.env['base.automation.lead.test'].create(vals)
@@ -27,7 +29,7 @@ class base_automation_test(common.TransactionCase):
"""
lead = self.create_lead(state='open')
self.assertEqual(lead.state, 'open')
- self.assertEqual(lead.user_id, self.user_admin, "Responsible should not change on creation of Lead with state 'open'.")
+ self.assertEqual(lead.user_id, self.user_root, "Responsible should not change on creation of Lead with state 'open'.")
def test_01_check_to_state_draft_post(self):
"""
@@ -49,15 +51,15 @@ class base_automation_test(common.TransactionCase):
"""
lead = self.create_lead(state='open')
self.assertEqual(lead.state, 'open', "Lead state should be 'open'")
- self.assertEqual(lead.user_id, self.user_admin, "Responsible should not change on creation of Lead with state 'open'.")
+ self.assertEqual(lead.user_id, self.user_root, "Responsible should not change on creation of Lead with state 'open'.")
# change state to pending and check that responsible has not changed
lead.write({'state': 'pending'})
self.assertEqual(lead.state, 'pending', "Lead state should be 'pending'")
- self.assertEqual(lead.user_id, self.user_admin, "Responsible should not change on creation of Lead with state from 'draft' to 'open'.")
+ self.assertEqual(lead.user_id, self.user_root, "Responsible should not change on creation of Lead with state from 'draft' to 'open'.")
# change state to done and check that responsible has not changed
lead.write({'state': 'done'})
self.assertEqual(lead.state, 'done', "Lead state should be 'done'")
- self.assertEqual(lead.user_id, self.user_admin, "Responsible should not chang on creation of Lead with state from 'pending' to 'done'.")
+ self.assertEqual(lead.user_id, self.user_root, "Responsible should not chang on creation of Lead with state from 'pending' to 'done'.")
def test_03_check_from_draft_to_done_without_steps(self):
"""
@@ -70,7 +72,7 @@ class base_automation_test(common.TransactionCase):
"""
lead = self.create_lead(state='open')
self.assertEqual(lead.state, 'open', "Lead state should be 'open'")
- self.assertEqual(lead.user_id, self.user_admin, "Responsible should not change on creation of Lead with state 'open'.")
+ self.assertEqual(lead.user_id, self.user_root, "Responsible should not change on creation of Lead with state 'open'.")
# change state to done and check that responsible has changed
lead.write({'state': 'done'})
self.assertEqual(lead.state, 'done', "Lead state should be 'done'")
@@ -85,7 +87,7 @@ class base_automation_test(common.TransactionCase):
partner.write({'customer': False})
lead = self.create_lead(state='open', partner_id=partner.id)
self.assertFalse(lead.customer, "Customer field should updated to False")
- self.assertEqual(lead.user_id, self.user_admin, "Responsible should not change on creation of Lead with state from 'draft' to 'open'.")
+ self.assertEqual(lead.user_id, self.user_root, "Responsible should not change on creation of Lead with state from 'draft' to 'open'.")
# change partner, recompute on lead should trigger the rule
partner.write({'customer': True})
self.assertTrue(lead.customer, "Customer field should updated to True")
@@ -101,15 +103,52 @@ class base_automation_test(common.TransactionCase):
self.assertFalse(lead.deadline, 'There should not be a deadline defined')
# change priority and user; this triggers deadline recomputation, and
# the server action should set the boolean field to True
- lead.write({'priority': True, 'user_id': self.user_admin.id})
+ lead.write({'priority': True, 'user_id': self.user_root.id})
self.assertTrue(lead.deadline, 'Deadline should be defined')
self.assertTrue(lead.is_assigned_to_admin, 'Lead should be assigned to admin')
+ def test_11b_recomputed_field(self):
+ mail_automation = self.env.ref('base_automation.test_rule_on_write_recompute_send_email')
+ send_mail_count = 0
+
+ def _patched_get_actions(*args, **kwargs):
+ obj = args[0]
+ if '__action_done' not in obj._context:
+ obj = obj.with_context(__action_done={})
+ return mail_automation.with_env(obj.env)
+
+ def _patched_send_mail(*args, **kwargs):
+ nonlocal send_mail_count
+ send_mail_count += 1
+
+ patchers = [
+ patch('odoo.addons.base_automation.models.base_automation.BaseAutomation._get_actions', _patched_get_actions),
+ patch('odoo.addons.mail.models.mail_template.MailTemplate.send_mail', _patched_send_mail),
+ ]
+
+ patchers[0].start()
+
+ lead = self.create_lead()
+ self.assertFalse(lead.priority)
+ self.assertFalse(lead.deadline)
+
+ patchers[1].start()
+
+ lead.write({'priority': True})
+
+ self.assertTrue(lead.priority)
+ self.assertTrue(lead.deadline)
+
+ for patcher in patchers:
+ patcher.stop()
+
+ self.assertEqual(send_mail_count, 1)
+
def test_12_recursive(self):
""" Check that a rule is executed recursively by a secondary change. """
lead = self.create_lead(state='open')
self.assertEqual(lead.state, 'open')
- self.assertEqual(lead.user_id, self.user_admin)
+ self.assertEqual(lead.user_id, self.user_root)
# change partner; this should trigger the rule that modifies the state
partner = self.env.ref('base.res_partner_1')
lead.write({'partner_id': partner.id})