Set of label improvements to Open ERP Server.
[odoo/odoo.git] / bin / addons / base / ir / workflow / workflow.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 from osv import fields, osv
24 from tools import graph
25 import netsvc
26
27 class workflow(osv.osv):
28     _name = "workflow"
29     _table = "wkf"
30   #  _log_access = False
31     _columns = {
32         'name': fields.char('Name', size=64, required=True),
33         'osv': fields.char('Resource Object', size=64, required=True,select=True),
34         'on_create': fields.boolean('On Create', select=True),
35         'activities': fields.one2many('workflow.activity', 'wkf_id', 'Activities'),
36     }
37     _defaults = {
38         'on_create': lambda *a: True
39     }
40
41     def write(self, cr, user, ids, vals, context=None):
42         if not context:
43             context={}
44         wf_service = netsvc.LocalService("workflow")
45         wf_service.clear_cache(cr, user)
46         return super(workflow, self).write(cr, user, ids, vals, context=context)
47
48     #
49     # scale =  (vertical-distance, horizontal-distance, min-node-width(optional), min-node-height(optional), margin(default=20))
50     #
51
52
53     def graph_get(self, cr, uid, id, scale, context={}):
54
55         nodes= []
56         nodes_name = []
57         transitions = []
58         start = []
59         tres = {}
60         no_ancester = []
61         workflow = self.browse(cr, uid, id, context)
62         for a in workflow.activities:
63             nodes_name.append((a.id,a.name))
64             nodes.append(a.id)
65             if a.flow_start:
66                 start.append(a.id)
67             else:
68                 if not a.in_transitions:
69                     no_ancester.append(a.id)
70
71             for t in a.out_transitions:
72                 transitions.append((a.id, t.act_to.id))
73                 tres[t.id] = (a.id, t.act_to.id)
74
75
76         g  = graph(nodes, transitions, no_ancester)
77         g.process(start)
78         g.scale(*scale)
79         result = g.result_get()
80         results = {}
81
82         for node in nodes_name:
83             results[str(node[0])] = result[node[0]]
84             results[str(node[0])]['name'] = node[1]
85
86         return {'nodes': results, 'transitions': tres}
87
88
89     def create(self, cr, user, vals, context=None):
90         if not context:
91             context={}
92         wf_service = netsvc.LocalService("workflow")
93         wf_service.clear_cache(cr, user)
94         return super(workflow, self).create(cr, user, vals, context=context)
95 workflow()
96
97 class wkf_activity(osv.osv):
98     _name = "workflow.activity"
99     _table = "wkf_activity"
100    # _log_access = False
101     _columns = {
102         'name': fields.char('Name', size=64, required=True),
103         'wkf_id': fields.many2one('workflow', 'Workflow', required=True, select=True, ondelete='cascade'),
104         'split_mode': fields.selection([('XOR', 'Xor'), ('OR','Or'), ('AND','And')], 'Split Mode', size=3, required=True),
105         'join_mode': fields.selection([('XOR', 'Xor'), ('AND', 'And')], 'Join Mode', size=3, required=True),
106         'kind': fields.selection([('dummy', 'Dummy'), ('function', 'Function'), ('subflow', 'Subflow'), ('stopall', 'Stop All')], 'Kind', size=64, required=True),
107         'action': fields.text('Python Action'),
108         'action_id': fields.many2one('ir.actions.server', 'Server Action', ondelete='set null'),
109         'flow_start': fields.boolean('Flow Start'),
110         'flow_stop': fields.boolean('Flow Stop'),
111         'subflow_id': fields.many2one('workflow', 'Subflow'),
112         'signal_send': fields.char('Signal (subflow.*)', size=32),
113         'out_transitions': fields.one2many('workflow.transition', 'act_from', 'Outgoing Transitions'),
114         'in_transitions': fields.one2many('workflow.transition', 'act_to', 'Incoming Transitions'),
115     }
116     _defaults = {
117         'kind': lambda *a: 'dummy',
118         'join_mode': lambda *a: 'XOR',
119         'split_mode': lambda *a: 'XOR',
120     }
121 wkf_activity()
122
123 class wkf_transition(osv.osv):
124     _table = "wkf_transition"
125     _name = "workflow.transition"
126    # _log_access = False
127     _rec_name = 'signal'
128     _columns = {
129         'trigger_model': fields.char('Trigger Object', size=128),
130         'trigger_expr_id': fields.char('Trigger Expression', size=128),
131         'signal': fields.char('Signal (button Name)', size=64),
132         'role_id': fields.many2one('res.roles', 'Role Required'),
133         'condition': fields.char('Condition', required=True, size=128),
134         'act_from': fields.many2one('workflow.activity', 'Source Activity', required=True, select=True, ondelete='cascade'),
135         'act_to': fields.many2one('workflow.activity', 'Destination Activity', required=True, select=True, ondelete='cascade'),
136     }
137     _defaults = {
138         'condition': lambda *a: 'True',
139     }
140 wkf_transition()
141
142 class wkf_instance(osv.osv):
143     _table = "wkf_instance"
144     _name = "workflow.instance"
145     _rec_name = 'res_type'
146     _log_access = False
147     _columns = {
148         'wkf_id': fields.many2one('workflow', 'Workflow', ondelete='cascade', select=True),
149         'uid': fields.integer('User ID'),
150         'res_id': fields.integer('Resource ID', select=True),
151         'res_type': fields.char('Resource Object', size=64, select=True),
152         'state': fields.char('State', size=32, select=True),
153     }
154     def _auto_init(self, cr, context={}):
155         super(wkf_instance, self)._auto_init(cr, context)
156         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_id_res_type_state_index\'')
157         if not cr.fetchone():
158             cr.execute('CREATE INDEX wkf_instance_res_id_res_type_state_index ON wkf_instance (res_id, res_type, state)')
159             cr.commit()
160         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_id_wkf_id_index\'')
161         if not cr.fetchone():
162             cr.execute('CREATE INDEX wkf_instance_res_id_wkf_id_index ON wkf_instance (res_id, wkf_id)')
163             cr.commit()
164
165 wkf_instance()
166
167 class wkf_workitem(osv.osv):
168     _table = "wkf_workitem"
169     _name = "workflow.workitem"
170     _log_access = False
171     _rec_name = 'state'
172     _columns = {
173         'act_id': fields.many2one('workflow.activity', 'Activity', required=True, ondelete="cascade", select=True),
174         'subflow_id': fields.many2one('workflow.instance', 'Subflow', ondelete="cascade", select=True),
175         'inst_id': fields.many2one('workflow.instance', 'Instance', required=True, ondelete="cascade", select=True),
176         'state': fields.char('State', size=64, select=True),
177     }
178 wkf_workitem()
179
180 class wkf_triggers(osv.osv):
181     _table = "wkf_triggers"
182     _name = "workflow.triggers"
183     _log_access = False
184     _columns = {
185         'res_id': fields.integer('Resource ID', size=128),
186         'model': fields.char('Object', size=128),
187         'instance_id': fields.many2one('workflow.instance', 'Destination Instance', ondelete="cascade"),
188         'workitem_id': fields.many2one('workflow.workitem', 'Workitem', required=True, ondelete="cascade"),
189     }
190     def _auto_init(self, cr, context={}):
191         super(wkf_triggers, self)._auto_init(cr, context)
192         cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_triggers_res_id_model_index\'')
193         if not cr.fetchone():
194             cr.execute('CREATE INDEX wkf_triggers_res_id_model_index ON wkf_triggers (res_id, model)')
195             cr.commit()
196 wkf_triggers()
197
198
199 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
200