[translation] : placeholders are missed out for Translation (Case:585261)
[odoo/odoo.git] / openerp / workflow / wkf_service.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 import instance
22 import openerp.netsvc as netsvc
23
24 class workflow_service(netsvc.Service):
25     """
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
28     using:
29
30     >>> import netsvc
31     >>> wf_service = netsvc.LocalService("workflow")
32
33     """
34
35     def __init__(self, name='workflow'):
36         netsvc.Service.__init__(self, name)
37         self.wkf_on_create_cache={}
38
39     def clear_cache(self, cr, uid):
40         self.wkf_on_create_cache[cr.dbname]={}
41
42     def trg_write(self, uid, res_type, res_id, cr):
43         """
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.
47
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
51         """
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)
56
57     def trg_trigger(self, uid, res_type, res_id, cr):
58         """
59         Activate a trigger.
60
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.
63
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
67         """
68         cr.execute('select instance_id from wkf_triggers where res_id=%s and model=%s', (res_id,res_type))
69         res = cr.fetchall()
70         for (instance_id,) in res:
71             cr.execute('select %s,res_type,res_id from wkf_instance where id=%s', (uid, instance_id,))
72             ident = cr.fetchone()
73             instance.update(cr, instance_id, ident)
74
75     def trg_delete(self, uid, res_type, res_id, cr):
76         """
77         Delete a workflow instance
78
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
82         """
83         ident = (uid,res_type,res_id)
84         instance.delete(cr, ident)
85
86     def trg_create(self, uid, res_type, res_id, cr):
87         """
88         Create a new workflow instance
89
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
93         """
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]
98         else:
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)
104
105     def trg_validate(self, uid, res_type, res_id, signal, cr):
106         """
107         Fire a signal on a given workflow instance
108
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
113         """
114         result = False
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
121         return result
122
123     def trg_redirect(self, uid, res_type, res_id, new_rid, cr):
124         """
125         Re-bind a workflow instance to another instance of the same model.
126
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
129         the new resource.
130
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
135         """
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
141             cr.execute(
142                 'SELECT id '\
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()
147             if new_id:
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))
153 workflow_service()
154
155 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
156