[FIX] bin/workflow: 'stack' variable was not initialized when sql query returned...
[odoo/odoo.git] / bin / workflow / instance.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 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 wkf_logs
24 import workitem
25
26 import netsvc
27 import pooler
28
29 def create(cr, ident, wkf_id):
30     (uid,res_type,res_id) = ident
31     cr.execute("select nextval('wkf_instance_id_seq')")
32     id_new = cr.fetchone()[0]
33     cr.execute('insert into wkf_instance (id,res_type,res_id,uid,wkf_id) values (%s,%s,%s,%s,%s)', (id_new,res_type,res_id,uid,wkf_id))
34     cr.execute('select * from wkf_activity where flow_start=True and wkf_id=%s', (wkf_id,))
35     res = cr.dictfetchall()
36     stack = []
37     workitem.create(cr, res, id_new, ident, stack=stack)
38     update(cr, id_new, ident)
39     return id_new
40
41 def delete(cr, ident):
42     (uid,res_type,res_id) = ident
43     cr.execute('delete from wkf_instance where res_id=%s and res_type=%s', (res_id,res_type))
44
45 def validate(cr, inst_id, ident, signal, force_running=False):
46     cr.execute("select * from wkf_workitem where inst_id=%s", (inst_id,))
47     stack = []
48     for witem in cr.dictfetchall():
49         stack = []
50         workitem.process(cr, witem, ident, signal, force_running, stack=stack)
51         # An action is returned
52     _update_end(cr, inst_id, ident)
53     return stack and stack[0] or False
54
55 def update(cr, inst_id, ident):
56     cr.execute("select * from wkf_workitem where inst_id=%s", (inst_id,))
57     for witem in cr.dictfetchall():
58         stack = []
59         workitem.process(cr, witem, ident, stack=stack)
60     return _update_end(cr, inst_id, ident)
61
62 def _update_end(cr, inst_id, ident):
63     cr.execute('select wkf_id from wkf_instance where id=%s', (inst_id,))
64     wkf_id = cr.fetchone()[0]
65     cr.execute('select state,flow_stop from wkf_workitem w left join wkf_activity a on (a.id=w.act_id) where w.inst_id=%s', (inst_id,))
66     ok=True
67     for r in cr.fetchall():
68         if (r[0]<>'complete') or not r[1]:
69             ok=False
70             break
71     if ok:
72         cr.execute('select distinct a.name from wkf_activity a left join wkf_workitem w on (a.id=w.act_id) where w.inst_id=%s', (inst_id,))
73         act_names = cr.fetchall()
74         cr.execute("update wkf_instance set state='complete' where id=%s", (inst_id,))
75         cr.execute("update wkf_workitem set state='complete' where subflow_id=%s", (inst_id,))
76         cr.execute("select i.id,w.osv,i.res_id from wkf_instance i left join wkf w on (i.wkf_id=w.id) where i.id in (select inst_id from wkf_workitem where subflow_id=%s)", (inst_id,))
77         for i in cr.fetchall():
78             for act_name in act_names:
79                 validate(cr, i[0], (ident[0],i[1],i[2]), 'subflow.'+act_name[0])
80     return ok
81
82
83 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
84