convert tabs to 4 spaces
[odoo/odoo.git] / bin / workflow / wkf_expr.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 ###############################################################################
28
29 import sys
30 import netsvc
31 import osv as base
32 import pooler
33
34
35 class EnvCall(object):
36
37     def __init__(self,wf_service,d_arg):
38         self.wf_service=wf_service
39         self.d_arg=d_arg
40
41     def __call__(self,*args):
42         arg=self.d_arg+args
43         return self.wf_service.execute_cr(*arg)
44
45
46 class Env(dict):
47
48     def __init__(self, wf_service, cr, uid, model, ids):
49         self.wf_service = wf_service
50         self.cr = cr
51         self.uid = uid
52         self.model = model
53         self.ids = ids
54         self.obj = pooler.get_pool(cr.dbname).get(model)
55         self.columns = self.obj._columns.keys() + self.obj._inherit_fields.keys()
56
57     def __getitem__(self, key):
58         if (key in self.columns) or (key in dir(self.obj)):
59             res = self.obj.browse(self.cr, self.uid, self.ids[0])
60             return res[key]
61             #res=self.wf_service.execute_cr(self.cr, self.uid, self.model, 'read',\
62             #       self.ids, [key])[0][key]
63             #super(Env, self).__setitem__(key, res)
64             #return res
65         #elif key in dir(self.obj):
66         #   return EnvCall(self.wf_service, (self.cr, self.uid, self.model, key,\
67         #           self.ids))
68         else:
69             return super(Env, self).__getitem__(key)
70
71 def _eval_expr(cr, ident, workitem, action):
72     ret=False
73     assert action, 'You used a NULL action in a workflow, use dummy node instead.'
74     for line in action.split('\n'):
75         uid=ident[0]
76         model=ident[1]
77         ids=[ident[2]]
78         if line =='True':
79             ret=True
80         elif line =='False':
81             ret=False
82         else:
83             wf_service = netsvc.LocalService("object_proxy")
84             env = Env(wf_service, cr, uid, model, ids)
85             ret = eval(line, env)
86     return ret
87
88 def execute_action(cr, ident, workitem, activity):
89     wf_service = netsvc.LocalService("object_proxy")
90     obj = pooler.get_pool(cr.dbname).get('ir.actions.server')
91     ctx = {'active_id':ident[2], 'active_ids':[ident[2]]}
92     result = obj.run(cr, ident[0], [activity['action_id']], ctx)
93     return result
94
95 def execute(cr, ident, workitem, activity):
96     return _eval_expr(cr, ident, workitem, activity['action'])
97
98 def check(cr, workitem, ident, transition, signal):
99     ok = True
100     if transition['signal']:
101         ok = (signal==transition['signal'])
102
103     if transition['role_id']:
104         uid = ident[0]
105         serv = netsvc.LocalService('object_proxy')
106         user_roles = serv.execute_cr(cr, uid, 'res.users', 'read', [uid], ['roles_id'])[0]['roles_id']
107         ok = ok and serv.execute_cr(cr, uid, 'res.roles', 'check', user_roles, transition['role_id'])
108     ok = ok and _eval_expr(cr, ident, workitem, transition['condition'])
109     return ok
110