[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
This commit is contained in:
rpa (Open ERP) 2010-05-26 17:43:46 +05:30
parent 2415c11a96
commit d330f6e262
6 changed files with 135 additions and 31 deletions

View File

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

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,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 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 +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:

View File

@ -0,0 +1,21 @@
<?xml version="1.0"?>
<openerp>
<data noupdate="1">
<!-- Scheduler for action rule-->
<record forcecreate="True" id="ir_cron_scheduler_action_rule"
model="ir.cron">
<field name="name">Run check action rule</field>
<field eval="True" name="active" />
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">minutes</field>
<field name="numbercall">-1</field>
<field eval="True" name="doall" />
<field eval="'base.action.rule'" name="model" />
<field eval="'_check'" name="function" />
<field eval="'(False,)'" name="args" />
</record>
</data>
</openerp>

View File

@ -56,15 +56,15 @@
<field name="server_action_id"/>
<field name="filter_id"/>
</page>
<page string="E-Mail Actions">
<page string="Email Actions">
<!-- <group col="4" colspan="2">-->
<separator colspan="4" string="Template of Email to Send"/>
<separator colspan="4" string="Email Information"/>
<field name="act_mail_to_watchers"/>
<field name="act_mail_to_user"/>
<field colspan="4" name="act_mail_to_email"/>
<!-- </group>-->
<!-- <group col="4" colspan="2">-->
<separator colspan="4" string="E-Mail Reminders (includes the content of the object)"/>
<separator colspan="4" string="Email Reminders"/>
<field name="act_remind_partner"/>
<field name="act_remind_attach"/>
<field name="act_remind_user"/>
@ -73,7 +73,7 @@
</group>
<field colspan="4" name="act_email_cc"/>
<!-- </group>-->
<separator colspan="4" string="Mail Body"/>
<separator colspan="4" string="Email Body"/>
<field colspan="4" name="act_mail_body" height="250"
nolabel="1" attrs="{'required':[('act_remind_user','=',True)]}" />
<separator colspan="4" string="Special Keywords to Be Used in The Body"/>
@ -82,9 +82,9 @@
<label align="0.0" string="%%(object_description)s = Object description" colspan="2"/>
<label align="0.0" string="%%(object_date)s = Creation date" colspan="2"/>
<label align="0.0" string="%%(partner)s = Partner name" colspan="2"/>
<label align="0.0" string="%%(partner_email)s = Partner email" colspan="2"/>
<label align="0.0" string="%%(partner_email)s = Partner Email" colspan="2"/>
<label align="0.0" string="%%(object_user)s = Responsible name" colspan="2"/>
<label align="0.0" string="%%(object_user_email)s = Responsible email" colspan="2"/>
<label align="0.0" string="%%(object_user_email)s = Responsible Email" colspan="2"/>
<label align="0.0" string="%%(object_user_phone)s = Responsible phone" colspan="2"/>
</page>
</notebook>

View File

@ -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 users 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 = []

View File

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