[FIX] web_kanban: parent can be undefined in some cases
[odoo/odoo.git] / openerp / workflow / instance.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2014 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 import workitem
22 from openerp.workflow.helpers import Session
23 from openerp.workflow.helpers import Record
24 from openerp.workflow.workitem import WorkflowItem
25
26 class WorkflowInstance(object):
27     def __init__(self, session, record, values):
28         assert isinstance(session, Session)
29         assert isinstance(record, Record)
30         self.session = session
31         self.record = record
32
33         if not values:
34             values = {}
35
36         assert isinstance(values, dict)
37         self.instance = values
38
39     @classmethod
40     def create(cls, session, record, workflow_id):
41         assert isinstance(session, Session)
42         assert isinstance(record, Record)
43         assert isinstance(workflow_id, (int, long))
44
45         cr = session.cr
46         cr.execute('insert into wkf_instance (res_type,res_id,uid,wkf_id,state) values (%s,%s,%s,%s,%s) RETURNING id', (record.model, record.id, session.uid, workflow_id, 'active'))
47         instance_id = cr.fetchone()[0]
48
49         cr.execute('select * from wkf_activity where flow_start=True and wkf_id=%s', (workflow_id,))
50         stack = []
51
52         activities = cr.dictfetchall()
53         for activity in activities:
54             WorkflowItem.create(session, record, activity, instance_id, stack)
55
56         cr.execute('SELECT * FROM wkf_instance WHERE id = %s', (instance_id,))
57         values = cr.dictfetchone()
58         wi = WorkflowInstance(session, record, values)
59         wi.update()
60
61         return wi
62
63     def delete(self):
64         self.session.cr.execute('delete from wkf_instance where res_id=%s and res_type=%s', (self.record.id, self.record.model))
65
66     def validate(self, signal, force_running=False):
67         assert isinstance(signal, basestring)
68         assert isinstance(force_running, bool)
69
70         cr = self.session.cr
71         cr.execute("select * from wkf_workitem where inst_id=%s", (self.instance['id'],))
72         stack = []
73         for work_item_values in cr.dictfetchall():
74             wi = WorkflowItem(self.session, self.record, work_item_values)
75             wi.process(signal=signal, force_running=force_running, stack=stack)
76             # An action is returned
77         self._update_end()
78         return stack and stack[0] or False
79
80     def update(self):
81         cr = self.session.cr
82
83         cr.execute("select * from wkf_workitem where inst_id=%s", (self.instance['id'],))
84         for work_item_values in cr.dictfetchall():
85             stack = []
86             WorkflowItem(self.session, self.record, work_item_values).process(stack=stack)
87         return self._update_end()
88
89     def _update_end(self):
90         cr = self.session.cr
91         instance_id = self.instance['id']
92         cr.execute('select wkf_id from wkf_instance where id=%s', (instance_id,))
93         wkf_id = cr.fetchone()[0]
94         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', (instance_id,))
95         ok=True
96         for r in cr.fetchall():
97             if (r[0]<>'complete') or not r[1]:
98                 ok=False
99                 break
100         if ok:
101             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', (instance_id,))
102             act_names = cr.fetchall()
103             cr.execute("update wkf_instance set state='complete' where id=%s", (instance_id,))
104             cr.execute("update wkf_workitem set state='complete' where subflow_id=%s", (instance_id,))
105             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)", (instance_id,))
106             for cur_instance_id, cur_model_name, cur_record_id in cr.fetchall():
107                 cur_record = Record(cur_model_name, cur_record_id)
108                 for act_name in act_names:
109                     WorkflowInstance(self.session, cur_record, {'id':cur_instance_id}).validate('subflow.%s' % act_name[0])
110
111         return ok
112
113
114
115
116
117 def create(session, record, workflow_id):
118     return WorkflowInstance(session, record).create(workflow_id)
119
120 def delete(session, record):
121     return WorkflowInstance(session, record).delete()
122
123 def validate(session, record, instance_id, signal, force_running=False):
124     return WorkflowInstance(session, record).validate(instance_id, signal, force_running)
125
126 def update(session, record, instance_id):
127     return WorkflowInstance(session, record).update(instance_id)
128
129 def _update_end(session, record, instance_id):
130     return WorkflowInstance(session, record)._update_end(instance_id)
131
132
133 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
134