[IMP] base_action_rule,crm: Added button for activating/deactivating rules. Added remind_user function for general purpose. Changes in crm according to action rules.
bzr revid: uco@tinyerp.co.in-20100208103414-a40idgngbwuqjuui
This commit is contained in:
parent
b082152d90
commit
cc0b6a7706
|
@ -5,6 +5,7 @@ import re
|
|||
import tools
|
||||
from osv import fields, osv, orm
|
||||
from osv.orm import except_orm
|
||||
from tools.translate import _
|
||||
|
||||
AVAILABLE_STATES = [
|
||||
('draft','Draft'),
|
||||
|
@ -25,13 +26,95 @@ AVAILABLE_PRIORITIES = [
|
|||
class base_action_rule(osv.osv):
|
||||
_name = 'base.action.rule'
|
||||
_description = 'Action Rules'
|
||||
|
||||
def _get_max_level(self, cr, uid, ids, field_name, arg, context=None):
|
||||
res = {}
|
||||
for check in self.browse(cr, uid, ids):
|
||||
if check.rule_lines and len(check.rule_lines) < 15:
|
||||
res[check.id] = len(check.rule_lines)
|
||||
elif len(check.rule_lines) > 15:
|
||||
raise osv.except_osv(_('Error !'), _('Max Level exceeded.'))
|
||||
return res
|
||||
|
||||
_columns = {
|
||||
'name': fields.many2one('ir.model', 'Model', required=True),
|
||||
'max_level': fields.integer('Max Level'),
|
||||
'rule_lines': fields.one2many('base.action.rule.line','rule_id','Rule Lines'),
|
||||
'create_date': fields.datetime('Create Date', readonly=1)
|
||||
'name': fields.many2one('ir.model', 'Model', required=True, states={'activate': [('readonly', True)]}),
|
||||
'max_level': fields.function(_get_max_level, method=True, string='Max Level',
|
||||
type='integer', store=True, help='Specifies maximum rule lines can be entered.'),
|
||||
'rule_lines': fields.one2many('base.action.rule.line','rule_id','Rule Lines', states={'activate': [('readonly', True)]}),
|
||||
'create_date': fields.datetime('Create Date', readonly=1),
|
||||
'state': fields.selection([('draft','Draft'),('activate','Activated'),('deactivate','Deactivated')],'State',readonly=1)
|
||||
}
|
||||
|
||||
_defaults = {
|
||||
'state': lambda *a: 'draft',
|
||||
}
|
||||
|
||||
def button_activate_rule(self, cr, uid, ids, context=None):
|
||||
check = self.browse(cr, uid, ids[0]).rule_lines
|
||||
if not check:
|
||||
raise osv.except_osv(_('Error !'), _('Rule Lines are empty ! Cannot activate the Rule.'))
|
||||
cronobj = self.pool.get('ir.cron')
|
||||
cronids = cronobj.search(cr,uid,[('model','=','base.action.rule'),('active','=',False)])
|
||||
if cronids:
|
||||
cronobj.write(cr, uid, cronids, {'active': True})
|
||||
self.write(cr, uid, ids, {'state': 'activate'})
|
||||
return True
|
||||
|
||||
def button_deactivate_rule(self, cr, uid, ids, context=None):
|
||||
cronobj = self.pool.get('ir.cron')
|
||||
cronids = cronobj.search(cr,uid,[('model','=','base.action.rule'),('active','=',True)])
|
||||
if cronids:
|
||||
cronobj.write(cr, uid, cronids, {'active': False})
|
||||
self.write(cr, uid, ids, {'state': 'deactivate'})
|
||||
return True
|
||||
|
||||
def remind_partner(self, cr, uid, ids, context={}, attach=False):
|
||||
return self.remind_user(cr, uid, ids, context, attach,
|
||||
destination=False)
|
||||
|
||||
def remind_user(self, cr, uid, ids, context={}, attach=False, destination=True):
|
||||
ruleline_obj = self.pool.get('base.action.rule.line')
|
||||
for rule in self.browse(cr, uid, ids):
|
||||
for action in rule.rule_lines:
|
||||
if not action.act_remind_user:
|
||||
raise osv.except_osv(_('Warning!'), ("Remind Responsible should be active."))
|
||||
if action.trg_user_id and action.trg_user_id.address_id and not action.trg_user_id.address_id.email:
|
||||
raise osv.except_osv(_('Error!'), ("User Email is not specified."))
|
||||
if action.trg_user_id and action.trg_user_id.address_id and action.trg_user_id.address_id.email:
|
||||
src = action.trg_user_id.address_id.email
|
||||
dest = action.act_reply_to
|
||||
body = action.act_mail_body
|
||||
if not destination:
|
||||
src, dest = dest, src
|
||||
if action.trg_user_id.signature:
|
||||
body += '\n\n%s' % (action.trg_user_id.signature or '')
|
||||
dest = [dest]
|
||||
|
||||
attach_to_send = None
|
||||
|
||||
if attach:
|
||||
attach_ids = self.pool.get('ir.attachment').search(cr, uid, [('res_model', '=', rule.name.model), ('res_id', '=', rule.name.id)])
|
||||
attach_to_send = self.pool.get('ir.attachment').read(cr, uid, attach_ids, ['datas_fname','datas'])
|
||||
attach_to_send = map(lambda x: (x['datas_fname'], base64.decodestring(x['datas'])), attach_to_send)
|
||||
|
||||
# Send an email
|
||||
flag = tools.email_send(
|
||||
src,
|
||||
dest,
|
||||
"Reminder: [%s] %s" % (str(rule.name.id), rule.name.model, ),
|
||||
ruleline_obj.format_body(body),
|
||||
reply_to=action.act_reply_to,
|
||||
openobject_id=str(rule.name.id),
|
||||
attach=attach_to_send
|
||||
)
|
||||
if flag:
|
||||
raise except_orm(_('Email!'),
|
||||
_("Email Successfully Sent by %s") % action.trg_user_id.name)
|
||||
else:
|
||||
raise except_orm(_('Email!'),
|
||||
_("Email is not sent Successfully for %s") % action.trg_user_id.name)
|
||||
return True
|
||||
|
||||
def _check(self, cr, uid, ids=False, context={}):
|
||||
'''
|
||||
Function called by the scheduler to process models
|
||||
|
@ -146,11 +229,9 @@ class base_action_rule(osv.osv):
|
|||
write['email_cc'] = action.act_email_cc
|
||||
obj.write(cr, uid, [data.id], write, context)
|
||||
if action.act_remind_user:
|
||||
obj.remind_user(cr, uid, [data.id], context, attach=action.act_remind_attach)
|
||||
self.remind_user(cr, uid, [rule.id], context, attach=action.act_remind_attach)
|
||||
if action.act_remind_partner:
|
||||
obj.remind_partner(cr, uid, [data.id], context, attach=action.act_remind_attach)
|
||||
if action.act_method:
|
||||
getattr(caseobj, 'act_method')(cr, uid, [data.id], action, context)
|
||||
self.remind_partner(cr, uid, [rule.id], context, attach=action.act_remind_attach)
|
||||
emails = []
|
||||
if action.act_mail_to_user:
|
||||
if data.user_id and data.user_id.address_id:
|
||||
|
@ -203,6 +284,7 @@ class base_action_rule_line(osv.osv):
|
|||
|
||||
'trg_priority_from': fields.selection([('','')] + AVAILABLE_PRIORITIES, 'Minimum Priority'),
|
||||
'trg_priority_to': fields.selection([('','')] + AVAILABLE_PRIORITIES, 'Maximum Priority'),
|
||||
'act_method': fields.char('Call Object Method', size=64),
|
||||
'act_state': fields.selection([('','')]+AVAILABLE_STATES, 'Set state to', size=16),
|
||||
'act_user_id': fields.many2one('res.users', 'Set responsible to'),
|
||||
'act_priority': fields.selection([('','')] + AVAILABLE_PRIORITIES, 'Set priority to'),
|
||||
|
@ -210,6 +292,7 @@ class base_action_rule_line(osv.osv):
|
|||
|
||||
'act_remind_partner': fields.boolean('Remind Partner', help="Check this if you want the rule to send a reminder by email to the partner."),
|
||||
'act_remind_user': fields.boolean('Remind responsible', help="Check this if you want the rule to send a reminder by email to the user."),
|
||||
'act_reply_to': fields.char('Reply-To', size=64),
|
||||
'act_remind_attach': fields.boolean('Remind with attachment', help="Check this if you want that all documents attached to the case be attached to the reminder email sent."),
|
||||
|
||||
'act_mail_to_user': fields.boolean('Mail to responsible',help="Check this if you want the rule to send an email to the responsible person."),
|
||||
|
@ -274,9 +357,9 @@ base_action_rule_line()
|
|||
class base_action_rule_history(osv.osv):
|
||||
_name = 'base.action.rule.history'
|
||||
_description = 'Action Rule History'
|
||||
_rec_name = 'rule_id'
|
||||
_columns = {
|
||||
'rule_id': fields.many2one('base.action.rule','Rule', required=True, readonly=1),
|
||||
'name': fields.related('rule_id', 'name', type='many2one', relation='ir.model', string='Model', readonly=1),
|
||||
'res_id': fields.integer('Resource ID', readonly=1),
|
||||
'date_action_last': fields.datetime('Last Action', readonly=1),
|
||||
'date_action_next': fields.datetime('Next Action', readonly=1),
|
||||
|
|
|
@ -14,7 +14,13 @@
|
|||
<form string="Action Rule">
|
||||
<field name="name" select="1"/>
|
||||
<field name="max_level" select="1"/>
|
||||
<field name="rule_lines" colspan="4"/>
|
||||
<field name="rule_lines" colspan="4" />
|
||||
<newline/>
|
||||
<group col="4" colspan="4">
|
||||
<field name="state"/>
|
||||
<button name="button_activate_rule" states="draft,deactivate" string="Activate" icon="gtk-execute" type="object"/>
|
||||
<button name="button_deactivate_rule" states="activate" string="De-activate" icon="gtk-execute" type="object"/>
|
||||
</group>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
@ -82,9 +88,12 @@
|
|||
<field name="act_user_id"/>
|
||||
<field name="act_priority"/>
|
||||
<separator colspan="4" string="E-Mail Reminders (includes the content of the case)"/>
|
||||
<field name="act_remind_user"/>
|
||||
<field name="act_remind_partner"/>
|
||||
<field name="act_remind_attach"/>
|
||||
<field name="act_remind_user"/>
|
||||
<group col="2" colspan="2" attrs="{'invisible': [('act_remind_user','=',False)]}">
|
||||
<field name="act_reply_to" attrs="{'required':[('act_remind_user','=',True)]}"/>
|
||||
</group>
|
||||
<field colspan="4" name="act_email_cc"/>
|
||||
<separator colspan="4" string="Server Action to be Triggered"/>
|
||||
<field name="server_action_id"/>
|
||||
|
@ -95,7 +104,7 @@
|
|||
<field name="act_mail_to_partner"/>
|
||||
<field name="act_mail_to_watchers"/>
|
||||
<field colspan="4" name="act_mail_to_email"/>
|
||||
<field colspan="4" name="act_mail_body"/>
|
||||
<field colspan="4" name="act_mail_body" attrs="{'required':[('act_remind_user','=',True)]}"/>
|
||||
<separator colspan="4" string="Special Keywords to Be Used in The Body"/>
|
||||
<label align="0.0" string="%%(case_id)s = Case ID" colspan="2"/>
|
||||
<label align="0.0" string="%%(case_subject)s = Case subject" colspan="2"/>
|
||||
|
@ -142,7 +151,7 @@
|
|||
<field name="type">form</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Action Rule History">
|
||||
<field name="rule_id" select="1"/>
|
||||
<field name="name" select="1"/>
|
||||
<field name="res_id" select="1"/>
|
||||
<field name="date_action_last"/>
|
||||
<field name="date_action_next"/>
|
||||
|
@ -156,7 +165,7 @@
|
|||
<field name="type">tree</field>
|
||||
<field name="arch" type="xml">
|
||||
<tree string="Action Rule History">
|
||||
<field name="rule_id" select="1"/>
|
||||
<field name="name" select="1"/>
|
||||
<field name="res_id" select="1"/>
|
||||
<field name="date_action_last"/>
|
||||
<field name="date_action_next"/>
|
||||
|
|
|
@ -198,10 +198,11 @@ class base_action_rule(osv.osv):
|
|||
history_obj.create(cr, uid, {'rule_id': case.id, 'res_id': case.name.id, 'date_action_last': lastDate, 'date_action_next': action_next})
|
||||
caseobj = self.pool.get('crm.case')
|
||||
case_ids = caseobj.search(cr, uid, [('state', 'not in', ('cancel','done'))])
|
||||
ruleobj = self.pool.get('base.action.rule')
|
||||
|
||||
while len(action_ids) and level:
|
||||
newactions = []
|
||||
actions = self.pool.get('base.action.rule').browse(cr, uid, action_ids, context)
|
||||
actions = ruleobj.browse(cr, uid, action_ids, context)
|
||||
for case in cases:
|
||||
for line in actions:
|
||||
for action in line.rule_lines:
|
||||
|
@ -306,9 +307,9 @@ class base_action_rule(osv.osv):
|
|||
write['email_cc'] = action.act_email_cc
|
||||
caseobj.write(cr, uid, [cs.id], write, context)
|
||||
if action.act_remind_user:
|
||||
caseobj.remind_user(cr, uid, [cs.id], context, attach=action.act_remind_attach)
|
||||
ruleobj.remind_user(cr, uid, [case.id], context, attach=action.act_remind_attach)
|
||||
if action.act_remind_partner:
|
||||
caseobj.remind_partner(cr, uid, [cs.id], context, attach=action.act_remind_attach)
|
||||
ruleobj.remind_partner(cr, uid, [case.id], context, attach=action.act_remind_attach)
|
||||
if action.act_method:
|
||||
getattr(caseobj, 'act_method')(cr, uid, [cs.id], action, context)
|
||||
emails = []
|
||||
|
@ -547,50 +548,6 @@ class crm_case(osv.osv):
|
|||
obj._action(cr,uid, rules, 'draft')
|
||||
return res
|
||||
|
||||
def remind_partner(self, cr, uid, ids, context={}, attach=False):
|
||||
return self.remind_user(cr, uid, ids, context, attach,
|
||||
destination=False)
|
||||
|
||||
def remind_user(self, cr, uid, ids, context={}, attach=False,
|
||||
destination=True):
|
||||
for case in self.browse(cr, uid, ids):
|
||||
if not case.section_id.reply_to:
|
||||
raise osv.except_osv(_('Error!'), ("Reply To is not specified in Section"))
|
||||
if not case.email_from:
|
||||
raise osv.except_osv(_('Error!'), ("Partner Email is not specified in Case"))
|
||||
if case.section_id.reply_to and case.email_from:
|
||||
src = case.email_from
|
||||
dest = case.section_id.reply_to
|
||||
body = case.email_last or case.description
|
||||
if not destination:
|
||||
src, dest = dest, src
|
||||
if case.user_id.signature:
|
||||
body += '\n\n%s' % (case.user_id.signature or '')
|
||||
dest = [dest]
|
||||
|
||||
attach_to_send = None
|
||||
|
||||
if attach:
|
||||
attach_ids = self.pool.get('ir.attachment').search(cr, uid, [('res_model', '=', 'crm.case'), ('res_id', '=', case.id)])
|
||||
attach_to_send = self.pool.get('ir.attachment').read(cr, uid, attach_ids, ['datas_fname','datas'])
|
||||
attach_to_send = map(lambda x: (x['datas_fname'], base64.decodestring(x['datas'])), attach_to_send)
|
||||
|
||||
# Send an email
|
||||
flag = tools.email_send(
|
||||
src,
|
||||
dest,
|
||||
"Reminder: [%s] %s" % (str(case.id), case.name, ),
|
||||
self.format_body(body),
|
||||
reply_to=case.section_id.reply_to,
|
||||
openobject_id=str(case.id),
|
||||
attach=attach_to_send
|
||||
)
|
||||
if flag:
|
||||
raise osv.except_osv(_('Email!'),("Email Successfully Sent"))
|
||||
else:
|
||||
raise osv.except_osv(_('Email Fail!'),("Email is not sent successfully"))
|
||||
return True
|
||||
|
||||
def add_reply(self, cursor, user, ids, context=None):
|
||||
for case in self.browse(cursor, user, ids, context=context):
|
||||
if case.email_last:
|
||||
|
|
Loading…
Reference in New Issue