From cd86bccc13217c26c351d7c28100447095e1eedc Mon Sep 17 00:00:00 2001 From: "Mod2 Team (OpenERP)" <> Date: Thu, 24 Jun 2010 11:47:55 +0530 Subject: [PATCH] [IMP] base_action_rule: * make proxy method of create/write methods of subcribed rule object to generate log details * Move data of scheduler to check action rule every 4 hours from crm module into base_action_rule bzr revid: hmo@tinyerp.com-20100624061755-blx8nzf8h9tr8xar --- addons/base_action_rule/__openerp__.py | 4 +- addons/base_action_rule/base_action_rule.py | 108 ++++++++++++++++-- .../base_action_rule_data.xml | 19 +++ addons/crm/crm_data.xml | 13 --- 4 files changed, 121 insertions(+), 23 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 ab1ae6c965b..cdb62316d15 100644 --- a/addons/base_action_rule/__openerp__.py +++ b/addons/base_action_rule/__openerp__.py @@ -27,7 +27,9 @@ '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 a1213dd7542..7d9d7ea63ff 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,71 @@ 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 model.model, rule.id FROM base_action_rule rule LEFT JOIN ir_model model on (model.id = rule.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 _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 _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 _register_hook(self, cr, uid, ids, context=None): + if not context: + context = {} + model_pool = self.pool.get('ir.model') + for action_rule in self.browse(cr, uid, ids, context=context): + model = action_rule.name.model + obj_pool = self.pool.get(model) + obj_pool.__setattr__('create', self._create(obj_pool.create, model, context=context)) + obj_pool.__setattr__('write', self._write(obj_pool.write, model, context=context)) + return True + + def create(self, cr, uid, vals, context=None): + res_id = super(base_action_rule, self).create(cr, uid, vals, context) + self._register_hook(cr, uid, [res_id], 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) + self._register_hook(cr, uid, ids, context=context) + return res + + def _check(self, cr, uid, automatic=False, use_new_cursor=False, \ + context=None): + """ + This Function is call by scheduler. + """ + rule_pool= self.pool.get('base.action.rule') + rule_ids = rule_pool.search(cr, uid, [], context=context) + return self._register_hook(cr, uid, rule_ids, context=context) + + def format_body(self, body): """ Foramat Action rule's body @param self: The object pointer """ @@ -235,6 +302,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 +347,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 +399,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 +426,27 @@ 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 00000000000..288215202d6 --- /dev/null +++ b/addons/base_action_rule/base_action_rule_data.xml @@ -0,0 +1,19 @@ + + + + + Check Action Rules + 4 + hours + -1 + + + + + + + + + + + diff --git a/addons/crm/crm_data.xml b/addons/crm/crm_data.xml index 1873f53f573..e8d5965a4c0 100644 --- a/addons/crm/crm_data.xml +++ b/addons/crm/crm_data.xml @@ -5,18 +5,5 @@ Sales Department Sales - - - - Check cases rules - 4 - hours - -1 - - - - - -