1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
26 import openerp.netsvc as netsvc
27 import openerp.pooler as pooler
29 class workflow_service(netsvc.Service):
31 Sometimes you might want to fire a signal or re-evaluate the current state
32 of a workflow using the service's API. You can access the workflow services
36 >>> wf_service = netsvc.LocalService("workflow")
39 def __init__(self, name='workflow', audience='*'):
40 netsvc.Service.__init__(self, name, audience)
41 self.exportMethod(self.trg_write)
42 self.exportMethod(self.trg_delete)
43 self.exportMethod(self.trg_create)
44 self.exportMethod(self.trg_validate)
45 self.exportMethod(self.trg_redirect)
46 self.exportMethod(self.trg_trigger)
47 self.exportMethod(self.clear_cache)
48 self.wkf_on_create_cache={}
50 def clear_cache(self, cr, uid):
51 self.wkf_on_create_cache[cr.dbname]={}
53 def trg_write(self, uid, res_type, res_id, cr):
55 Reevaluates the specified workflow instance. Thus if any condition for
56 a transition have been changed in the backend, then running ``trg_write``
57 will move the workflow over that transition.
59 :param res_type: the model name
60 :param res_id: the model instance id the workflow belongs to
61 :param cr: a database cursor
63 ident = (uid,res_type,res_id)
64 cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id or None,res_type or None, 'active'))
65 for (id,) in cr.fetchall():
66 instance.update(cr, id, ident)
68 def trg_trigger(self, uid, res_type, res_id, cr):
72 If a workflow instance is waiting for a trigger from another model, then this
73 trigger can be activated if its conditions are met.
75 :param res_type: the model name
76 :param res_id: the model instance id the workflow belongs to
77 :param cr: a database cursor
79 cr.execute('select instance_id from wkf_triggers where res_id=%s and model=%s', (res_id,res_type))
81 for (instance_id,) in res:
82 cr.execute('select %s,res_type,res_id from wkf_instance where id=%s', (uid, instance_id,))
84 instance.update(cr, instance_id, ident)
86 def trg_delete(self, uid, res_type, res_id, cr):
88 Delete a workflow instance
90 :param res_type: the model name
91 :param res_id: the model instance id the workflow belongs to
92 :param cr: a database cursor
94 ident = (uid,res_type,res_id)
95 instance.delete(cr, ident)
97 def trg_create(self, uid, res_type, res_id, cr):
99 Create a new workflow instance
101 :param res_type: the model name
102 :param res_id: the model instance id to own the created worfklow instance
103 :param cr: a database cursor
105 ident = (uid,res_type,res_id)
106 self.wkf_on_create_cache.setdefault(cr.dbname, {})
107 if res_type in self.wkf_on_create_cache[cr.dbname]:
108 wkf_ids = self.wkf_on_create_cache[cr.dbname][res_type]
110 cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
111 wkf_ids = cr.fetchall()
112 self.wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
113 for (wkf_id,) in wkf_ids:
114 instance.create(cr, ident, wkf_id)
116 def trg_validate(self, uid, res_type, res_id, signal, cr):
118 Fire a signal on a given workflow instance
120 :param res_type: the model name
121 :param res_id: the model instance id the workflow belongs to
122 :signal: the signal name to be fired
123 :param cr: a database cursor
126 ident = (uid,res_type,res_id)
127 # ids of all active workflow instances for a corresponding resource (id, model_nam)
128 cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id, res_type, 'active'))
129 for (id,) in cr.fetchall():
130 res2 = instance.validate(cr, id, ident, signal)
131 result = result or res2
134 def trg_redirect(self, uid, res_type, res_id, new_rid, cr):
136 Re-bind a workflow instance to another instance of the same model.
138 Make all workitems which are waiting for a (subflow) workflow instance
139 for the old resource point to the (first active) workflow instance for
142 :param res_type: the model name
143 :param res_id: the model instance id the workflow belongs to
144 :param new_rid: the model instance id to own the worfklow instance
145 :param cr: a database cursor
147 # get ids of wkf instances for the old resource (res_id)
148 #CHECKME: shouldn't we get only active instances?
149 cr.execute('select id, wkf_id from wkf_instance where res_id=%s and res_type=%s', (res_id, res_type))
150 for old_inst_id, wkf_id in cr.fetchall():
151 # first active instance for new resource (new_rid), using same wkf
154 'FROM wkf_instance '\
155 'WHERE res_id=%s AND res_type=%s AND wkf_id=%s AND state=%s',
156 (new_rid, res_type, wkf_id, 'active'))
157 new_id = cr.fetchone()
159 # select all workitems which "wait" for the old instance
160 cr.execute('select id from wkf_workitem where subflow_id=%s', (old_inst_id,))
161 for (item_id,) in cr.fetchall():
162 # redirect all those workitems to the wkf instance of the new resource
163 cr.execute('update wkf_workitem set subflow_id=%s where id=%s', (new_id[0], item_id))
166 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: