[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
This commit is contained in:
Mod2 Team (OpenERP) 2010-06-24 11:47:55 +05:30 committed by Harry (OpenERP)
parent db704adb67
commit cd86bccc13
4 changed files with 121 additions and 23 deletions

View File

@ -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',

View File

@ -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 Rules 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:

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
<record id="ir_cron_crm_action" model="ir.cron">
<field name="name">Check Action Rules</field>
<field name="interval_number">4</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field eval="'base.action.rule'" name="model"/>
<field eval="'_check'" name="function"/>
<field eval="'()'" name="args"/>
</record>
</data>
</openerp>

View File

@ -5,18 +5,5 @@
<field name="name">Sales Department</field>
<field name="code">Sales</field>
</record>
<record id="ir_cron_crm_action" model="ir.cron">
<field name="name">Check cases rules</field>
<field name="interval_number">4</field>
<field name="interval_type">hours</field>
<field name="numbercall">-1</field>
<field eval="False" name="doall"/>
<field eval="'crm.case'" name="model"/>
<field eval="'_check'" name="function"/>
<field eval="'()'" name="args"/>
</record>
</data>
</openerp>