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