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