From d330f6e2628eb805eb684faee9c38cf06bddcc55 Mon Sep 17 00:00:00 2001 From: "rpa (Open ERP)" Date: Wed, 26 May 2010 17:43:46 +0530 Subject: [PATCH] [IMP]: base_action_rule: Improvement in the rule object class, overwrite the create() and write() method to trigger rules if record of object in rule undergoes create/write bzr revid: rpa@tinyerp.com-20100526121346-i71io3zm3cu9jhit --- addons/base_action_rule/__openerp__.py | 2 +- addons/base_action_rule/base_action_rule.py | 112 +++++++++++++++++++-- addons/base_action_rule/base_action_rule_data.xml | 21 ++++ addons/base_action_rule/base_action_rule_view.xml | 12 +-- addons/crm/crm_action_rule.py | 16 +-- addons/crm/crm_phonecall.py | 3 + 6 files changed, 135 insertions(+), 31 deletions(-) create mode 100644 addons/base_action_rule/base_action_rule_data.xml diff --git a/addons/base_action_rule/__openerp__.py b/addons/base_action_rule/__openerp__.py index ab1ae6c..9d44fbc 100644 --- a/addons/base_action_rule/__openerp__.py +++ b/addons/base_action_rule/__openerp__.py @@ -27,7 +27,7 @@ 'author': 'Tiny', 'website': 'http://www.openerp.com', 'depends': ['base'], - 'init_xml': [], + 'init_xml': ['base_action_rule_data.xml'], 'update_xml': [ 'base_action_rule_view.xml', 'security/ir.model.access.csv', diff --git a/addons/base_action_rule/base_action_rule.py b/addons/base_action_rule/base_action_rule.py index a1213dd..764f3da 100644 --- a/addons/base_action_rule/base_action_rule.py +++ b/addons/base_action_rule/base_action_rule.py @@ -19,14 +19,16 @@ # ############################################################################## -import time -import mx.DateTime -import re - -import tools +from datetime import datetime from osv import fields, osv, orm from osv.orm import except_orm +from osv.osv import osv_pool from tools.translate import _ +import mx.DateTime +import pooler +import re +import time +import tools class base_action_rule(osv.osv): """ Base Action Rules """ @@ -132,6 +134,74 @@ the rule to mark CC(mail to any other person defined in actions)."), _order = 'sequence' + def pre_action(self, cr, uid, ids, model, context=None): + # Searching for action rules + cr.execute("SELECT m.model, r.id from base_action_rule r left join ir_model m on (m.id = r.name)") + res = cr.fetchall() + # Check if any rule matching with current object + for obj_name, rule_id in res: + if not (model == obj_name): + continue + else: + obj = self.pool.get(obj_name) + self._action(cr, uid, [rule_id], obj.browse(cr, uid, ids, context=context)) + return True + + def new_create(self, old_create, model, context=None): + if not context: + context = {} + def make_call_old(cr, uid, vals, context=context): + new_id = old_create(cr, uid, vals, context=context) + if not context.get('action'): + self.pre_action(cr, uid, [new_id], model, context=context) + return new_id + return make_call_old + + def new_write(self, old_write, model, context=None): + if not context: + context = {} + def make_call_old(cr, uid, ids, vals, context=context): + if isinstance(ids, (str, int, long)): + ids = [ids] + if not context.get('action'): + self.pre_action(cr, uid, ids, model, context=context) + return old_write(cr, uid, ids, vals, context=context) + return make_call_old + + def create(self, cr, uid, vals, context=None): + if not context: + context = {} + res_id = super(base_action_rule, self).create(cr, uid, vals, context) + model_pool = self.pool.get('ir.model') + model = model_pool.browse(cr, uid, vals.get('name'), context=context).model + obj_pool = self.pool.get(model) + obj_pool.__setattr__('old_create', obj_pool.create) + obj_pool.__setattr__('old_write', obj_pool.write) + obj_pool.__setattr__('create', self.new_create(obj_pool.create, model, context=context)) + obj_pool.__setattr__('write', self.new_write(obj_pool.write, model, context=context)) + return res_id + + def write(self, cr, uid, ids, vals, context=None): + res = super(base_action_rule, self).write(cr, uid, ids, vals, context) + model_pool = self.pool.get('ir.model') + if not context: + context = {} + for rule in self.browse(cr, uid, ids, context=context): + model = model_pool.browse(cr, uid, rule.name.id, context=context).model + obj_pool = self.pool.get(model) + obj_pool.__setattr__('old_create', obj_pool.create) + obj_pool.__setattr__('old_write', obj_pool.write) + obj_pool.__setattr__('create', self.new_create(obj_pool.create, model, context=context)) + obj_pool.__setattr__('write', self.new_write(obj_pool.write, model, context=context)) + return res + + def _check(self, cr, uid, automatic=False, use_new_cursor=False, \ + context=None): + rule_pool= self.pool.get('base.action.rule') + rule_ids = rule_pool.search(cr, uid, [], context=context) + return rule_pool.write(cr, uid, rule_ids, {}, context=context) + + def format_body(self, body): """ Foramat Action rule's body @param self: The object pointer """ @@ -235,6 +305,7 @@ the rule to mark CC(mail to any other person defined in actions)."), context.update({'active_id':obj.id, 'active_ids':[obj.id]}) self.pool.get('ir.actions.server').run(cr, uid, [action.server_action_id.id], context) write = {} + if hasattr(obj, 'user_id') and action.act_user_id: obj.user_id = action.act_user_id write['user_id'] = action.act_user_id.id @@ -279,12 +350,10 @@ the rule to mark CC(mail to any other person defined in actions)."), @param ids: List of Basic Action Rule’s IDs, @param objects: pass objects @param context: A standard dictionary for contextual values """ - + context.update({'action': True}) if not scrit: scrit = [] - cr.execute("select id from base_action_rule order by sequence") - rule_ids = map(lambda x: x[0], cr.fetchall()) - for action in self.browse(cr, uid, rule_ids): + for action in self.browse(cr, uid, ids): level = action.max_level if not level: break @@ -333,6 +402,7 @@ the rule to mark CC(mail to any other person defined in actions)."), self.do_action(cr, uid, action, model_obj, obj, context) break level -= 1 + context.update({'action': False}) return True def _check_mail(self, cr, uid, ids, context=None): @@ -359,4 +429,28 @@ the rule to mark CC(mail to any other person defined in actions)."), base_action_rule() + +class ir_cron(osv.osv): + _inherit = 'ir.cron' + + def _poolJobs(self, db_name, check=False): + + try: + db, pool = pooler.get_db_and_pool(db_name) + except: + return False + cr = db.cursor() + try: + next = datetime.now().strftime('%Y-%m-%d %H:00:00') + # Putting nextcall always less than current time in order to call it every time + cr.execute('UPDATE ir_cron set nextcall = \'%s\' where numbercall<>0 and active and model=\'base.action.rule\' ' % (next)) + cr.commit() + res = super(ir_cron, self)._poolJobs(db_name, check=check) + finally: + cr.commit() + cr.close() + +ir_cron() + + # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/base_action_rule/base_action_rule_data.xml b/addons/base_action_rule/base_action_rule_data.xml new file mode 100644 index 0000000..a7c2a99 --- /dev/null +++ b/addons/base_action_rule/base_action_rule_data.xml @@ -0,0 +1,21 @@ + + + + + + + Run check action rule + + + 1 + minutes + -1 + + + + + + + + diff --git a/addons/base_action_rule/base_action_rule_view.xml b/addons/base_action_rule/base_action_rule_view.xml index ce07647..2bd56a3 100644 --- a/addons/base_action_rule/base_action_rule_view.xml +++ b/addons/base_action_rule/base_action_rule_view.xml @@ -56,15 +56,15 @@ - + - + - + @@ -73,7 +73,7 @@ - + @@ -82,9 +82,9 @@ diff --git a/addons/crm/crm_action_rule.py b/addons/crm/crm_action_rule.py index 3a14659..9092efe 100644 --- a/addons/crm/crm_action_rule.py +++ b/addons/crm/crm_action_rule.py @@ -82,23 +82,9 @@ this if you want the rule to send an email to the partner."), if hasattr(obj, 'categ_id'): ok = ok and (not action.trg_categ_id or action.trg_categ_id.id==obj.categ_id.id) -# TODO: history_line is removed -# if hasattr(obj, 'history_line'): -# ok = ok and (not action.trg_max_history or action.trg_max_history<=(len(obj.history_line)+1)) -# reg_history = action.regex_history -# result_history = True -# if reg_history: -# ptrn = re.compile(str(reg_history)) -# if obj.history_line: -# _result = ptrn.search(str(obj.history_line[0].description)) -# if not _result: -# result_history = False - regex_h = not reg_history or result_history - ok = ok and regex_h return ok def do_action(self, cr, uid, action, model_obj, obj, context={}): - """ @param self: The object pointer @param cr: the current row, from the database cursor, @param uid: the current user’s ID for security checks, @@ -118,7 +104,7 @@ this if you want the rule to send an email to the partner."), write['email_cc'] = obj.email_cc+','+obj.act_email_cc else: write['email_cc'] = obj.act_email_cc - + model_obj.write(cr, uid, [obj.id], write, context) emails = [] diff --git a/addons/crm/crm_phonecall.py b/addons/crm/crm_phonecall.py index 205b30d..cc26ad8 100644 --- a/addons/crm/crm_phonecall.py +++ b/addons/crm/crm_phonecall.py @@ -38,6 +38,9 @@ class crm_phonecall(osv.osv, crm_case): 'name': fields.char('Name', size=64), 'active': fields.boolean('Active', required=False), 'thread_id': fields.many2one('mailgate.thread', 'Thread', required=False), + 'date_action_last': fields.datetime('Last Action', readonly=1), + 'date_action_next': fields.datetime('Next Action', readonly=1), + 'create_date': fields.datetime('Creation Date' , readonly=True), 'section_id': fields.many2one('crm.case.section', 'Sales Team', \ select=True, help='Sales team to which Case belongs to.\ Define Responsible user and Email account for mail gateway.'), -- 1.7.10.4