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