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 ##############################################################################
22 import openerp.netsvc as netsvc
24 class workflow_service(netsvc.Service):
26 Sometimes you might want to fire a signal or re-evaluate the current state
27 of a workflow using the service's API. You can access the workflow services
31 >>> wf_service = netsvc.LocalService("workflow")
35 def __init__(self, name='workflow'):
36 netsvc.Service.__init__(self, name)
37 self.wkf_on_create_cache={}
39 def clear_cache(self, cr, uid):
40 self.wkf_on_create_cache[cr.dbname]={}
42 def trg_write(self, uid, res_type, res_id, cr):
44 Reevaluates the specified workflow instance. Thus if any condition for
45 a transition have been changed in the backend, then running ``trg_write``
46 will move the workflow over that transition.
48 :param res_type: the model name
49 :param res_id: the model instance id the workflow belongs to
50 :param cr: a database cursor
52 ident = (uid,res_type,res_id)
53 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'))
54 for (id,) in cr.fetchall():
55 instance.update(cr, id, ident)
57 def trg_trigger(self, uid, res_type, res_id, cr):
61 If a workflow instance is waiting for a trigger from another model, then this
62 trigger can be activated if its conditions are met.
64 :param res_type: the model name
65 :param res_id: the model instance id the workflow belongs to
66 :param cr: a database cursor
68 cr.execute('select instance_id from wkf_triggers where res_id=%s and model=%s', (res_id,res_type))
70 for (instance_id,) in res:
71 cr.execute('select %s,res_type,res_id from wkf_instance where id=%s', (uid, instance_id,))
73 instance.update(cr, instance_id, ident)
75 def trg_delete(self, uid, res_type, res_id, cr):
77 Delete a workflow instance
79 :param res_type: the model name
80 :param res_id: the model instance id the workflow belongs to
81 :param cr: a database cursor
83 ident = (uid,res_type,res_id)
84 instance.delete(cr, ident)
86 def trg_create(self, uid, res_type, res_id, cr):
88 Create a new workflow instance
90 :param res_type: the model name
91 :param res_id: the model instance id to own the created worfklow instance
92 :param cr: a database cursor
94 ident = (uid,res_type,res_id)
95 self.wkf_on_create_cache.setdefault(cr.dbname, {})
96 if res_type in self.wkf_on_create_cache[cr.dbname]:
97 wkf_ids = self.wkf_on_create_cache[cr.dbname][res_type]
99 cr.execute('select id from wkf where osv=%s and on_create=True', (res_type,))
100 wkf_ids = cr.fetchall()
101 self.wkf_on_create_cache[cr.dbname][res_type] = wkf_ids
102 for (wkf_id,) in wkf_ids:
103 instance.create(cr, ident, wkf_id)
105 def trg_validate(self, uid, res_type, res_id, signal, cr):
107 Fire a signal on a given workflow instance
109 :param res_type: the model name
110 :param res_id: the model instance id the workflow belongs to
111 :signal: the signal name to be fired
112 :param cr: a database cursor
115 ident = (uid,res_type,res_id)
116 # ids of all active workflow instances for a corresponding resource (id, model_nam)
117 cr.execute('select id from wkf_instance where res_id=%s and res_type=%s and state=%s', (res_id, res_type, 'active'))
118 for (id,) in cr.fetchall():
119 res2 = instance.validate(cr, id, ident, signal)
120 result = result or res2
123 def trg_redirect(self, uid, res_type, res_id, new_rid, cr):
125 Re-bind a workflow instance to another instance of the same model.
127 Make all workitems which are waiting for a (subflow) workflow instance
128 for the old resource point to the (first active) workflow instance for
131 :param res_type: the model name
132 :param res_id: the model instance id the workflow belongs to
133 :param new_rid: the model instance id to own the worfklow instance
134 :param cr: a database cursor
136 # get ids of wkf instances for the old resource (res_id)
137 #CHECKME: shouldn't we get only active instances?
138 cr.execute('select id, wkf_id from wkf_instance where res_id=%s and res_type=%s', (res_id, res_type))
139 for old_inst_id, wkf_id in cr.fetchall():
140 # first active instance for new resource (new_rid), using same wkf
143 'FROM wkf_instance '\
144 'WHERE res_id=%s AND res_type=%s AND wkf_id=%s AND state=%s',
145 (new_rid, res_type, wkf_id, 'active'))
146 new_id = cr.fetchone()
148 # select all workitems which "wait" for the old instance
149 cr.execute('select id from wkf_workitem where subflow_id=%s', (old_inst_id,))
150 for (item_id,) in cr.fetchall():
151 # redirect all those workitems to the wkf instance of the new resource
152 cr.execute('update wkf_workitem set subflow_id=%s where id=%s', (new_id[0], item_id))
155 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: