implement the loop action on the server action

now every action can be iterative with the multiple records

bzr revid: mga@tinyerp.com-20081217114021-8wpumtr5j50n4mxv
This commit is contained in:
Mantavya Gajjar 2008-12-17 17:10:21 +05:30
parent f677df6884
commit 5587073cc6
3 changed files with 60 additions and 27 deletions

View File

@ -1189,7 +1189,7 @@
<page string="Create / Write" attrs="{'invisible':[('state','!=','object_create'),('state','!=','object_write')]}">
<separator colspan="4" string="Fields Mapping"/>
<field name="srcmodel_id" select="2" attrs="{'required':[('state','!=','sms'), ('state','!=','code'), ('state','!=','trigger'), ('state','!=','client_action'), ('state','!=','email'), ('state','!=','sms'), ('state','!=','other')]}"/>
<field name="srcmodel_id" select="2" attrs="{'required':[('state','!=','sms'), ('state','!=','code'), ('state','!=','loop'),('state','!=','trigger'), ('state','!=','client_action'), ('state','!=','email'), ('state','!=','sms'), ('state','!=','other')]}"/>
<field name="fields_lines" nolabel="1" select="2" colspan="4">
<tree string="Field Mappings" editable="top">
<field name="col1" domain="[('model_id','=',parent.srcmodel_id or parent.model_id)]"/>
@ -1202,10 +1202,18 @@
<field name="value" colspan="4"/>
</form>
</field>
<field name="record_id" attrs="{'invisible':[('state','!=','object_create')]}" domain="[('model_id','in',[model_id])]"/>
<field name="record_id" attrs="{'readonly':[('state','!=','object_create')]}" domain="[('model_id','in',[model_id])]"/>
<field name="write_id" attrs="{'readonly':[('state','!=','object_write')]}"/>
<label colspan="4" string="If you use a formula type, use a python expression using the variable 'object'." align="0.0"/>
</page>
<page string="Iteration Actions" attrs="{'invisible':[('state','!=','loop')]}">
<separator colspan="4" string="Iteration Action Configuration"/>
<field name="loop_action" domain="[('state','!=','loop')]"/>
<field name="expression" attrs="{'required':[('state','=','loop')]}"/>
<label colspan="4" string="Define the expression that generate a list" align="0.0"/>
</page>
<page string="Multi Actions" attrs="{'invisible':[('state','!=','other')]}">
<separator colspan="4" string="Other Actions Configuration"/>
<field name="child_ids" nolabel="1" colspan="4"/>

View File

@ -364,12 +364,7 @@ server_object_lines()
# Actions that are run on the server side
#
class actions_server(osv.osv):
def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False):
res = super(actions_server, self).fields_view_get(cr, user, view_id, view_type, context, toolbar)
#print 'RES : ',res
return res
def _select_signals(self, cr, uid, context={}):
cr.execute("select distinct t.signal as key, t.signal || ' - [ ' || w.osv || ' ] ' as val from wkf w, wkf_activity a, wkf_transition t "\
" where w.id = a.wkf_id " \
@ -388,9 +383,11 @@ class actions_server(osv.osv):
_columns = {
'name': fields.char('Action Name', required=True, size=64),
'condition' : fields.char('Condition', size=256),
'sub_condition' : fields.char('Condition', size=256),
'state': fields.selection([
('client_action','Client Action'),
('dummy','Dummy'),
('loop','Iteration'),
('code','Python Code'),
('trigger','Trigger'),
('email','Email'),
@ -398,7 +395,7 @@ class actions_server(osv.osv):
('object_create','Create Object'),
('object_write','Write Object'),
('other','Multi Actions'),
], 'Action State', required=True, size=32),
], 'Action Type', required=True, size=32),
'code':fields.text('Python Code'),
'sequence': fields.integer('Sequence'),
'model_id': fields.many2one('ir.model', 'Object', required=True),
@ -406,21 +403,25 @@ class actions_server(osv.osv):
'trigger_name': fields.selection(_select_signals, string='Trigger Name', size=128),
'wkf_model_id': fields.many2one('ir.model', 'Workflow on'),
'trigger_obj_id': fields.many2one('ir.model.fields','Trigger On'),
'email': fields.many2one('ir.model.fields', 'Contact'),
'email': fields.char('Email Address', size=512),
'subject': fields.char('Subject', size=1024, translate=True),
'message': fields.text('Message', translate=True),
'mobile': fields.many2one('ir.model.fields', 'Contact'),
'mobile': fields.char('Mobile No', size=512),
'sms': fields.char('SMS', size=160, translate=True),
'child_ids': fields.many2many('ir.actions.server', 'rel_server_actions', 'server_id', 'action_id', 'Others Actions'),
'usage': fields.char('Action Usage', size=32),
'type': fields.char('Action Type', size=32, required=True),
'srcmodel_id': fields.many2one('ir.model', 'Model', help="In which object you want to create / write the object if its empty refer to the Object field"),
'fields_lines': fields.one2many('ir.server.object.lines', 'server_id', 'Fields Mapping'),
'record_id':fields.many2one('ir.model.fields', 'Record Id', help="privide the field name from where the record id refers, if its empty it will refer to the active id of the object")
'record_id':fields.many2one('ir.model.fields', 'Create Id', help="Provide the field name from where the record id stores after the create operations, if its empty, you can not track the new record"),
'write_id':fields.char('Write Id', size=256, help="Provide the field name from where the record id refer for the write operation, if its empty it will refer to the active id of the object"),
'loop_action':fields.many2one('ir.actions.server', 'Loop Action'),
'expression':fields.char('Loop Expression', size=512),
}
_defaults = {
'state': lambda *a: 'dummy',
'condition': lambda *a: 'True',
'sub_condition': lambda *a: 'True',
'type': lambda *a: 'ir.actions.server',
'sequence': lambda *a: 5,
'code': lambda *a: """# You can use the following variables
@ -434,7 +435,6 @@ class actions_server(osv.osv):
""",
}
def get_email(self, cr, uid, action, context):
logger = netsvc.Logger()
obj_pool = self.pool.get(action.model_id.model)
@ -456,7 +456,6 @@ class actions_server(osv.osv):
return obj
def get_mobile(self, cr, uid, action, context):
logger = netsvc.Logger()
obj_pool = self.pool.get(action.model_id.model)
@ -501,6 +500,7 @@ class actions_server(osv.osv):
# OUT:
# False : Finnished correctly
# ACTION_ID : Action to launch
def run(self, cr, uid, ids, context={}):
logger = netsvc.Logger()
@ -540,7 +540,12 @@ class actions_server(osv.osv):
if action.state == 'email':
user = config['email_from']
address = self.get_email(cr, uid, action, context)
address = str(action.email)
try:
address = eval(str(action.email), cxt)
except:
pass
if not address:
raise osv.except_osv(_('Error'), _("Please specify the Partner Email address !"))
if not user:
@ -582,6 +587,23 @@ class actions_server(osv.osv):
res.append(result)
return res
if action.state == 'loop':
obj_pool = self.pool.get(action.model_id.model)
obj = obj_pool.browse(cr, uid, context['active_id'], context=context)
cxt = {
'context':context,
'object': obj,
'time':time,
'cr': cr,
'pool' : self.pool,
'uid' : uid
}
expr = eval(str(action.expression), cxt)
context['object'] = obj
for i in expr:
context['active_id'] = i.id
result = self.run(cr, uid, [action.loop_action.id], context)
if action.state == 'object_write':
res = {}
for exp in action.fields_lines:
@ -589,29 +611,30 @@ class actions_server(osv.osv):
if exp.type == 'equation':
obj_pool = self.pool.get(action.model_id.model)
obj = obj_pool.browse(cr, uid, context['active_id'], context=context)
expr = eval(euq, {'context':context, 'object': obj, 'time':time})
cxt = {'context':context, 'object': obj, 'time':time}
expr = eval(euq, cxt)
else:
expr = exp.value
res[exp.col1.name] = expr
if not action.record_id:
if not action.write_id:
if not action.srcmodel_id:
obj_pool = self.pool.get(action.model_id.model)
obj_pool.write(cr, uid, [context.get('active_id')], res)
else:
write_id = context.get('active_id')
obj_pool = self.pool.get(action.srcmodel_id.model)
obj_pool.write(cr, uid, [context.get('active_id')], res)
else:
obj_pool.write(cr, uid, [write_id], res)
elif action.write_id:
obj_pool = self.pool.get(action.srcmodel_id.model)
id = self.pool.get(action.model_id.model).read(cr, uid, [context.get('active_id')], [action.record_id.name])
write_id = False
if action.record_id.ttype in ('char', 'int', 'float'):
write_id = int(id[0][action.record_id.name])
elif action.record_id.ttype in ('many2one'):
write_id = int(id[0][action.record_id.name][0])
rec = self.pool.get(action.model_id.model).browse(cr, uid, context.get('active_id'))
id = eval(action.write_id, {'object': rec})
if type(id) != type(1):
raise osv.except_osv(_('Error'), _("Problem in configuration `Record Id` in Server Action!"))
write_id = id
obj_pool.write(cr, uid, [write_id], res)
if action.state == 'object_create':
res = {}
for exp in action.fields_lines:
@ -632,6 +655,7 @@ class actions_server(osv.osv):
self.pool.get(action.model_id.model).write(cr, uid, [context.get('active_id')], {action.record_id.name:res_id})
return False
actions_server()
class act_window_close(osv.osv):

View File

@ -134,6 +134,7 @@
<rng:ref name="button"/>
<rng:ref name="form" />
<rng:ref name="page" />
<rng:ref name="notebook"/>
</rng:choice>
</rng:zeroOrMore>
</rng:element>