From bee03356993e77e60a3773b9aa7df470547f5e29 Mon Sep 17 00:00:00 2001 From: Amit Vora Date: Mon, 23 Jun 2014 10:57:42 +0530 Subject: [PATCH] [IMP] base_action_rule: allow using the CharDomain widget to set the filter and pre-update filter. Using ir.filters is still possible, but is not the only way anymore of setting the domains used in the base action rules. --- addons/base_action_rule/base_action_rule.py | 55 ++++++++++++------- .../base_action_rule_view.xml | 42 +++++++++----- addons/crm/crm_action_rule_demo.xml | 2 +- 3 files changed, 62 insertions(+), 37 deletions(-) diff --git a/addons/base_action_rule/base_action_rule.py b/addons/base_action_rule/base_action_rule.py index 864d283d89f..1afecfab147 100644 --- a/addons/base_action_rule/base_action_rule.py +++ b/addons/base_action_rule/base_action_rule.py @@ -88,14 +88,16 @@ class base_action_rule(osv.osv): 'server_action_ids': fields.many2many('ir.actions.server', string='Server Actions', domain="[('model_id', '=', model_id)]", help="Examples: email reminders, call object service, etc."), - 'filter_pre_id': fields.many2one('ir.filters', string='Before Update Filter', - ondelete='restrict', - domain="[('model_id', '=', model_id.model)]", + 'filter_pre_id': fields.many2one( + 'ir.filters', string='Before Update Filter', + ondelete='restrict', domain="[('model_id', '=', model_id.model)]", 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_id.model)]", + '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_id.model)]", 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('Last Run', readonly=1, copy=False), } @@ -114,21 +116,31 @@ class base_action_rule(osv.osv): clear_fields = ['filter_pre_id'] return {'value': dict.fromkeys(clear_fields, False)} - def _filter(self, cr, uid, action, action_filter, record_ids, context=None): - """ filter the list record_ids that satisfy the action filter """ - if record_ids and action_filter: - assert action.model == action_filter.model_id, "Filter model different from action rule model" - model = self.pool[action_filter.model_id] - domain = [('id', 'in', record_ids)] + eval(action_filter.domain) - ctx = dict(context or {}) - ctx.update(eval(action_filter.context)) - record_ids = model.search(cr, uid, domain, context=ctx) + def onchange_filter_pre_id(self, cr, uid, ids, filter_pre_id, context=None): + ir_filter = self.pool['ir.filters'].browse(cr, uid, filter_pre_id, context=context) + return {'value': {'filter_pre_domain': ir_filter.domain}} + + def onchange_filter_id(self, cr, uid, ids, filter_id, context=None): + ir_filter = self.pool['ir.filters'].browse(cr, uid, filter_id, context=context) + return {'value': {'filter_domain': ir_filter.domain}} + + def _filter(self, cr, uid, action, action_filter, record_ids, domain=False, context=None): + """ Filter the list record_ids that satisfy the domain or the action filter. """ + if record_ids and (domain is not False or action_filter): + if domain is not False: + new_domain = [('id', 'in', record_ids)] + eval(domain) + ctx = context + elif action_filter: + assert action.model == action_filter.model_id, "Filter model different from action rule model" + new_domain = [('id', 'in', record_ids)] + eval(action_filter.domain) + ctx = dict(context or {}) + ctx.update(eval(action_filter.context)) + record_ids = self.pool[action.model].search(cr, uid, new_domain, context=ctx) return record_ids def _process(self, cr, uid, action, record_ids, context=None): """ process the given action on the records """ model = self.pool[action.model_id.model] - # modify records values = {} if 'date_action_last' in model._all_columns: @@ -184,7 +196,7 @@ class base_action_rule(osv.osv): # check postconditions, and execute actions on the records that satisfy them for action in action_model.browse(cr, uid, action_ids, context=context): - if action_model._filter(cr, uid, action, action.filter_id, [new_id], context=context): + if action_model._filter(cr, uid, action, action.filter_id, [new_id], domain=action.filter_domain, context=context): action_model._process(cr, uid, action, [new_id], context=context) return new_id @@ -211,14 +223,14 @@ class base_action_rule(osv.osv): # check preconditions pre_ids = {} for action in actions: - pre_ids[action] = action_model._filter(cr, uid, action, action.filter_pre_id, ids, context=context) + pre_ids[action] = action_model._filter(cr, uid, action, action.filter_pre_id, ids, domain=action.filter_pre_domain, context=context) # call original method write.origin(self, cr, uid, ids, vals, context=context, **kwargs) # check postconditions, and execute actions on the records that satisfy them for action in actions: - post_ids = action_model._filter(cr, uid, action, action.filter_id, pre_ids[action], context=context) + post_ids = action_model._filter(cr, uid, action, action.filter_id, pre_ids[action], domain=action.filter_domain, context=context) if post_ids: action_model._process(cr, uid, action, post_ids, context=context) return True @@ -301,12 +313,13 @@ class base_action_rule(osv.osv): last_run = get_datetime(action.last_run) else: last_run = datetime.utcfromtimestamp(0) - # retrieve all the records that satisfy the action's condition model = self.pool[action.model_id.model] domain = [] ctx = dict(context) - if action.filter_id: + if action.filter_domain: + domain = eval(action.context) + elif action.filter_id: domain = eval(action.filter_id.domain) ctx.update(eval(action.filter_id.context)) if 'lang' not in ctx: diff --git a/addons/base_action_rule/base_action_rule_view.xml b/addons/base_action_rule/base_action_rule_view.xml index 2087d51a0d7..8e5ac4a651c 100644 --- a/addons/base_action_rule/base_action_rule_view.xml +++ b/addons/base_action_rule/base_action_rule_view.xml @@ -22,16 +22,30 @@ - + +

Please choose the document type before setting the conditions.

+
+ - - + -

- Select when the action must be run, and add filters and/or timing conditions. -
- In order to create a new filter: -

    +
    +

    Select when the action must be run, and choose records and/or timing conditions.

    +

    You may also use filters instead of choosing records. In order to create a new filter:

    +
    • Go to your "Related Document Model" page and set the filter parameters in the "Search" view (Example of filter based on Leads/Opportunities: Creation Date "is equal to" 01/01/2012)
    • In this same "Search" view, select the menu "Save Current Filter", enter the name (Ex: Create the 01/01/2012) and add the option "Share with all users"
    - The filter must therefore be available in this page. -

    +
    diff --git a/addons/crm/crm_action_rule_demo.xml b/addons/crm/crm_action_rule_demo.xml index d77211a0f12..797ac77d1b8 100644 --- a/addons/crm/crm_action_rule_demo.xml +++ b/addons/crm/crm_action_rule_demo.xml @@ -21,7 +21,7 @@ 1 on_time - + [('stage_id.sequence', '=', 1)] 5 day