[FIX] 1st test for automated action

bzr revid: api@openerp.com-20121123103113-x9rspq1oh1887153
This commit is contained in:
Arnaud Pineux
2012-11-23 11:31:13 +01:00
parent 25a82eab5b
commit db5a4e7075
6 changed files with 51 additions and 33 deletions
+32 -18
View File
@@ -109,7 +109,7 @@ trigger date, like sending a reminder 15 minutes before a meeting."),
_order = 'sequence'
def post_action(self, cr, uid, ids, model, context=None):
def post_action(self, cr, uid, ids, model, old_records_states=None, 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.model_id) \
@@ -117,11 +117,11 @@ trigger date, like sending a reminder 15 minutes before a meeting."),
res = cr.fetchall()
# Check if any rule matching with current object
for obj_name, rule_id in res:
obj = self.pool.get(obj_name)
model_pool = self.pool.get(obj_name)
# If the rule doesn't involve a time condition, run it immediately
# Otherwise we let the scheduler run the action
if self.browse(cr, uid, rule_id, context=context).trg_date_type == 'none':
self._action(cr, uid, [rule_id], obj.browse(cr, uid, ids, context=context), context=context)
self._action(cr, uid, [rule_id], model_pool.browse(cr, uid, ids, context=context), old_records_states=old_records_states, context=context)
return True
def _create(self, old_create, model, context=None):
@@ -144,13 +144,23 @@ trigger date, like sending a reminder 15 minutes before a meeting."),
`post_action`, in that order.
"""
def wrapper(cr, uid, ids, vals, context=context):
old_records_states = {}
if context is None:
context = {}
if isinstance(ids, (str, int, long)):
ids = [ids]
model_pool = self.pool.get(model)
# get the old records (before the write)
if model and ids:
for record in model_pool.browse(cr,uid,ids,context=context):
old_records_states[record.id] = record.state
#old_records = model_pool.browse(cr,uid,ids,context=context)
#print old_records[0]
#print old_records[0].state
old_write(cr, uid, ids, vals, context=context)
#print old_records[0].state
if not context.get('action'):
self.post_action(cr, uid, ids, model, context=context)
self.post_action(cr, uid, ids, model, old_records_states=old_records_states, context=context)
return True
return wrapper
@@ -242,7 +252,7 @@ trigger date, like sending a reminder 15 minutes before a meeting."),
def do_check(self, cr, uid, action, obj, context=None):
def do_check(self, cr, uid, action, obj, old_records_states=None, context=None):
""" check Action """
if context is None:
context = {}
@@ -263,14 +273,21 @@ trigger date, like sending a reminder 15 minutes before a meeting."),
(action.trg_partner_categ_id.id in map(lambda x: x.id, obj.partner_id.category_id or []))
)
)
state_to = context.get('state_to', False)
state = getattr(obj, 'state', False)
if state:
ok = ok and (not action.trg_state_from or action.trg_state_from==state)
if state_to:
ok = ok and (not action.trg_state_to or action.trg_state_to==state_to)
elif action.trg_state_to:
ok = False
#state_to = the state after a write or a create
state_to = getattr(obj, 'state', False)
#state_from = nothing or the state of old_records
state_from = ""
if old_records_states != None:
if old_records_states[obj.id]:
state_from = old_records_states[obj.id]
#if we have an action that check the status
if action.trg_state_from:
print action.trg_state_from, " and ", state_to
ok = ok and action.trg_state_from==state_to
if action.trg_state_to:
print action.trg_state_to, " for ", state_from, " and ", state_to
ok = state_from!=state_to
ok = ok and action.trg_state_to==state_to
reg_name = action.regex_name
result_name = True
if reg_name:
@@ -312,19 +329,16 @@ trigger date, like sending a reminder 15 minutes before a meeting."),
model_obj.message_subscribe(cr, uid, [obj.id], new_followers, context=context)
return True
def _action(self, cr, uid, ids, objects, scrit=None, context=None):
def _action(self, cr, uid, ids, objects, scrit=None, old_records_states=None, context=None):
""" Do Action """
if context is None:
context = {}
context.update({'action': True})
if not scrit:
scrit = []
if not isinstance(objects, list):
objects = [objects]
for action in self.browse(cr, uid, ids, context=context):
for obj in objects:
if self.do_check(cr, uid, action, obj, context=context):
if self.do_check(cr, uid, action, obj, old_records_states=old_records_states, context=context):
self.do_action(cr, uid, action, obj, context=context)
context.update({'action': False})
+2 -6
View File
@@ -19,6 +19,7 @@
#
##############################################################################
import pdb
from osv import fields, osv
from tools.translate import _
@@ -280,15 +281,10 @@ class base_stage(object):
# 2. update values
if values_to_update:
self.write(cr, uid, ids, values_to_update, context=context)
# 3. call _action for base action rule
if new_state_name:
self._action(cr, uid, cases, new_state_name, context=context)
elif not (new_stage_id is None):
new_state_name = self.read(cr, uid, ids, ['state'], context=context)[0]['state']
self._action(cr, uid, cases, new_state_name, context=context)
return True
def _action(self, cr, uid, cases, state_to, scrit=None, context=None):
print "--- Base_Stage (_action) ---"
if context is None:
context = {}
context['state_to'] = state_to
+2 -1
View File
@@ -157,13 +157,14 @@ class base_state(object):
:params: update_values: values that will be added with the state
update when writing values to the record.
"""
print "-------------------------BASE STATE--------------------------------"
cases = self.browse(cr, uid, ids, context=context)
cases[0].state # fill browse record cache, for _action having old and new values
if update_values is None:
update_values = {}
update_values['state'] = state_name
self.write(cr, uid, ids, update_values, context=context)
self._action(cr, uid, cases, state_name, context=context)
#self._action(cr, uid, cases, state_name, context=context)
def _action(self, cr, uid, cases, state_to, scrit=None, context=None):
if context is None:
+2 -2
View File
@@ -43,8 +43,8 @@ class base_action_rule(osv.osv):
'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'),
}
def do_check(self, cr, uid, action, obj, context=None):
ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
def do_check(self, cr, uid, action, obj, old_records_states=None, context=None):
ok = super(base_action_rule, self).do_check(cr, uid, action, obj, old_records_states=old_records_states, context=context)
if hasattr(obj, 'section_id'):
ok = ok and (not action.trg_section_id or action.trg_section_id.id == obj.section_id.id)
+9 -4
View File
@@ -29,6 +29,7 @@ from tools.translate import _
from tools import html2plaintext
from base.res.res_partner import format_address
import pdb
CRM_LEAD_PENDING_STATES = (
crm.AVAILABLE_STATES[2][0], # Cancelled
@@ -380,8 +381,10 @@ class crm_lead(base_stage, format_address, osv.osv):
""" Mark the case as lost: state=cancel and probability=0 """
for lead in self.browse(cr, uid, ids):
stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, [('probability', '=', 0.0)], context=context)
if stage_id:
self.case_set(cr, uid, [lead.id], values_to_update={'probability': 0.0}, new_stage_id=stage_id, context=context)
if not stage_id:
return False
else:
self.write(cr, uid, ids, {'probability' : 0.0, 'stage_id' : stage_id}, context=context)
self.case_mark_lost_send_note(cr, uid, ids, context=context)
return True
@@ -389,8 +392,10 @@ class crm_lead(base_stage, format_address, osv.osv):
""" Mark the case as lost: state=done and probability=100 """
for lead in self.browse(cr, uid, ids):
stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, [('probability', '=', 100.0)], context=context)
if stage_id:
self.case_set(cr, uid, [lead.id], values_to_update={'probability': 100.0}, new_stage_id=stage_id, context=context)
if not stage_id:
return False
else:
self.write(cr, uid, ids, {'probability' : 100.0, 'stage_id' : stage_id}, context=context)
self.case_mark_won_send_note(cr, uid, ids, context=context)
return True
+4 -2
View File
@@ -167,8 +167,10 @@ class crm_claim(base_stage, osv.osv):
""" Mark the case as refused: state=done and case_refused=True """
for lead in self.browse(cr, uid, ids):
stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, ['&', ('state', '=', 'done'), ('case_refused', '=', True)], context=context)
if stage_id:
self.case_set(cr, uid, [lead.id], values_to_update={}, new_stage_id=stage_id, context=context)
if not stage_id:
return False
else:
self.write(cr, uid, ids, {'stage_id' : stage_id}, context=context)
return self.case_refuse_send_note(cr, uid, ids, context=context)
def onchange_partner_id(self, cr, uid, ids, part, email=False):