From cbb7c2442a632e0e89eadd43185805640f3403f3 Mon Sep 17 00:00:00 2001 From: Fabien Pinckaers Date: Fri, 10 Feb 2017 14:10:27 -0800 Subject: [PATCH 1/2] [IMP] base_automation: removing domains based on filters in automation bad ux: it's difficult to understand why there is two way to do the same thing. (4 fields for domains, instead of 2) Example of confusion: What happens if you set both a filter and a domain? What happens if you change the filter? does it change the automation? The new domain widget is good enough. We don't need such extra fields. --- .../data/base_automation_demo.xml | 23 ++--------- .../base_automation/models/base_automation.py | 40 ++----------------- .../views/base_automation_view.xml | 19 ++------- 3 files changed, 10 insertions(+), 72 deletions(-) diff --git a/addons/base_automation/data/base_automation_demo.xml b/addons/base_automation/data/base_automation_demo.xml index a4014a64c3e..d8bedc07020 100644 --- a/addons/base_automation/data/base_automation_demo.xml +++ b/addons/base_automation/data/base_automation_demo.xml @@ -1,22 +1,5 @@ - - Test lead in state 'draft' - base.automation.lead.test - [('state', '=', 'draft')] - - - - Test lead in state 'open' - base.automation.lead.test - [('state', '=', 'open')] - - - - Test lead in state 'done' - base.automation.lead.test - [('state', '=', 'done')] - Base Automation: test rule on create @@ -26,7 +9,7 @@ on_create - + [('state', '=', 'draft')] @@ -37,8 +20,8 @@ on_write - - + [('state', '=', 'done')] + [('state', '=', 'open')] diff --git a/addons/base_automation/models/base_automation.py b/addons/base_automation/models/base_automation.py index 051198edc17..a03ff7678bc 100644 --- a/addons/base_automation/models/base_automation.py +++ b/addons/base_automation/models/base_automation.py @@ -55,14 +55,8 @@ class BaseAutomation(models.Model): string='Delay type', default='day') trg_date_calendar_id = fields.Many2one("resource.calendar", string='Use Calendar', help="When calculating a day-based timed condition, it is possible to use a calendar to compute the date based on working days.") - filter_pre_id = fields.Many2one("ir.filters", string='Before Update Filter', ondelete='restrict', - domain="[('model_id', '=', model_name)]", - help="If present, this condition must be satisfied before the update of the record.") filter_pre_domain = fields.Char(string='Before Update Domain', help="If present, this condition must be satisfied before the update of the record.") - filter_id = fields.Many2one("ir.filters", string='Filter', ondelete='restrict', - domain="[('model_id', '=', model_name)]", - help="If present, this condition must be satisfied before executing the action rule.") filter_domain = fields.Char(string='Domain', help="If present, this condition must be satisfied before executing the action rule.") last_run = fields.Datetime(readonly=True, copy=False) on_change_fields = fields.Char(string="On Change Fields Trigger", help="Comma-separated list of field names that triggers the onchange.") @@ -72,25 +66,16 @@ class BaseAutomation(models.Model): @api.onchange('model_id') def onchange_model_id(self): - self.filter_pre_id = self.filter_id = False self.model_name = self.model_id.model @api.onchange('trigger') def onchange_trigger(self): if self.trigger in ['on_create', 'on_create_or_write', 'on_unlink']: - self.filter_pre_id = self.filter_pre_domain = self.trg_date_id = self.trg_date_range = self.trg_date_range_type = False + self.filter_pre_domain = self.trg_date_id = self.trg_date_range = self.trg_date_range_type = False elif self.trigger in ['on_write', 'on_create_or_write']: self.trg_date_id = self.trg_date_range = self.trg_date_range_type = False elif self.trigger == 'on_time': - self.filter_pre_id = self.filter_pre_domain = False - - @api.onchange('filter_pre_id') - def onchange_filter_pre_id(self): - self.filter_pre_domain = self.filter_pre_id.domain - - @api.onchange('filter_id') - def onchange_filter_id(self): - self.filter_domain = self.filter_id.domain + self.filter_pre_domain = False @api.model def create(self, vals): @@ -155,11 +140,7 @@ class BaseAutomation(models.Model): def _filter_pre(self, records): """ Filter the records that satisfy the precondition of action ``self``. """ - if self.filter_pre_id and records: - domain = [('id', 'in', records.ids)] + safe_eval(self.filter_pre_id.domain, self._get_eval_context()) - ctx = safe_eval(self.filter_pre_id.context) - return records.with_context(**ctx).search(domain).with_env(records.env) - elif self.filter_pre_domain and records: + if self.filter_pre_domain and records: domain = [('id', 'in', records.ids)] + safe_eval(self.filter_pre_domain, self._get_eval_context()) return records.search(domain) else: @@ -167,11 +148,7 @@ class BaseAutomation(models.Model): def _filter_post(self, records): """ Filter the records that satisfy the postcondition of action ``self``. """ - if self.filter_id and records: - domain = [('id', 'in', records.ids)] + safe_eval(self.filter_id.domain, self._get_eval_context()) - ctx = safe_eval(self.filter_id.context) - return records.with_context(**ctx).search(domain).with_env(records.env) - elif self.filter_domain and records: + if self.filter_domain and records: domain = [('id', 'in', records.ids)] + safe_eval(self.filter_domain, self._get_eval_context()) return records.search(domain) else: @@ -347,15 +324,6 @@ class BaseAutomation(models.Model): context = dict(self._context) if action.filter_domain: domain = safe_eval(action.filter_domain, eval_context) - elif action.filter_id: - domain = safe_eval(action.filter_id.domain, eval_context) - context.update(safe_eval(action.filter_id.context)) - if 'lang' not in context: - # Filters might be language-sensitive, attempt to reuse creator lang - # as we are usually running this as super-user in background - filter_meta = action.filter_id.get_metadata()[0] - user_id = (filter_meta['write_uid'] or filter_meta['create_uid'])[0] - context['lang'] = self.env['res.users'].browse(user_id).lang records = self.env[action.model_name].with_context(context).search(domain) # determine when action should occur for the records diff --git a/addons/base_automation/views/base_automation_view.xml b/addons/base_automation/views/base_automation_view.xml index 1fe302d286c..fa8c4b5eab2 100644 --- a/addons/base_automation/views/base_automation_view.xml +++ b/addons/base_automation/views/base_automation_view.xml @@ -19,22 +19,9 @@ - \ No newline at end of file + From c56b7462214114079823c38d38de9f194757d20c Mon Sep 17 00:00:00 2001 From: Fabien Pinckaers Date: Fri, 10 Feb 2017 15:22:45 -0800 Subject: [PATCH 2/2] [IMP] base_automation: default to write instead of python code --- odoo/addons/base/ir/ir_actions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/odoo/addons/base/ir/ir_actions.py b/odoo/addons/base/ir/ir_actions.py index 1b2cbad555d..ae5d74a7af6 100644 --- a/odoo/addons/base/ir/ir_actions.py +++ b/odoo/addons/base/ir/ir_actions.py @@ -466,7 +466,7 @@ class IrActionsServer(models.Model): ('object_create', 'Create a new Record'), ('object_write', 'Update the Record'), ('multi', 'Execute several actions')], string='Action To Do', - default='code', required=True, # set default to write ? + default='object_write', required=True, help="Type of server action. The following values are available:\n" "- 'Execute Python Code': a block of python code that will be executed\n" "- 'Create or Copy a new Record': create a new record with new values, or copy an existing record in your database\n"