Improved `Process` module.
[odoo/odoo.git] / addons / process / process.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2005-TODAY TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 import netsvc
30 import pooler, tools
31
32 from osv import fields, osv
33
34 class process_process(osv.osv):
35     _name = "process.process"
36     _description = "Process"
37     _columns = {
38         'name': fields.char('Name', size=30,required=True),
39         'active': fields.boolean('Active'),
40         'note': fields.text('Notes'),
41         'node_ids': fields.one2many('process.node', 'process_id', 'Nodes')
42     }
43     _defaults = {
44         'active' : lambda *a: True,
45     }
46
47     def graph_get(self, cr, uid, id, res_model, res_id, scale, context):
48         
49         pool = pooler.get_pool(cr.dbname)
50
51         process = pool.get('process.process').browse(cr, uid, [id])[0]
52         current_object = pool.get(res_model).browse(cr, uid, [res_id])[0]
53
54         nodes = {}
55         start = []
56         transitions = {}
57         for node in process.node_ids:
58
59             data = {}
60
61             data['name'] = node.name
62             data['menu'] = (node.menu_id or None) and node.menu_id.name
63             data['model'] = (node.model_id or None) and node.model_id.model
64             data['kind'] = node.kind
65             data['active'] = 0
66
67             if node.kind == "state" and node.model_id and node.model_id.model == res_model:
68                 states = node.model_states
69                 states = (states or []) and states.split(',')
70                 data['active'] = (states and current_object.state in states) or not states
71
72             elif node.kind == "router":
73                 #TODO:
74                 pass
75
76             elif node.kind == "subflow":
77                 #TODO: subflow
78                 pass
79
80             nodes[node.id] = data
81
82             if node.flow_start:
83                 start.append(node.id)
84
85             for tr in node.transition_ids:
86                 data = {}
87                 
88                 data['name'] = tr.name
89                 data['source'] = tr.source_node_id.id
90                 data['target'] = tr.target_node_id.id
91
92                 data['buttons'] = buttons = []
93                 for b in tr.action_ids:
94                     button = {}
95                     button['name'] = b.name
96                     buttons.append(button)
97
98                 data['roles'] = roles = []
99                 for r in tr.role_ids:
100                     role = {}
101                     role['name'] = r.name
102                     roles.append(role)
103                     
104                 transitions[tr.id] = data
105
106         g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
107         g.process(start)
108         #g.scale(100, 100, 180, 120)
109         g.scale(*scale)
110
111         graph = g.result_get()
112
113         miny = -1
114
115         for k,v in nodes.items():
116
117             x = graph[k]['y']
118             y = graph[k]['x']
119
120             if miny == -1:
121                 miny = y
122
123             miny = min(y, miny)
124
125             v['x'] = x
126             v['y'] = y
127
128         for k, v in nodes.items():
129             y = v['y']
130             v['y'] = min(y - miny + 10, y)
131
132         return dict(nodes=nodes, transitions=transitions)
133
134 process_process()
135
136 class process_node(osv.osv):
137     _name = 'process.node'
138     _description ='Process Nodes'
139     _columns = {
140         'name': fields.char('Name', size=30,required=True),
141         'process_id': fields.many2one('process.process', 'Process', required=True),
142         'kind': fields.selection([('state','State'),('router','Router'),('subflow','Subflow')],'Kind of Node', required=True),
143         'menu_id': fields.many2one('ir.ui.menu', 'Related Menu'),
144         'note': fields.text('Notes'),
145         'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
146         'model_states': fields.char('States Expression', size=128),
147         'flow_start': fields.boolean('Starting Flow'),
148         'transition_ids': fields.one2many('process.transition', 'target_node_id', 'Transitions'),
149     }
150     _defaults = {
151         'kind': lambda *args: 'state',
152         'model_states': lambda *args: False,
153         'flow_start': lambda *args: False,
154     }
155 process_node()
156
157 class process_transition(osv.osv):
158     _name = 'process.transition'
159     _description ='Process Transitions'
160     _columns = {
161         'name': fields.char('Name', size=32, required=True),
162         'source_node_id': fields.many2one('process.node', 'Source Node', required=True, ondelete='cascade'),
163         'target_node_id': fields.many2one('process.node', 'Target Node', required=True, ondelete='cascade'),        
164         'action_ids': fields.one2many('process.transition.action', 'transition_id', 'Buttons'),
165         'role_ids': fields.many2many('res.roles', 'process_transition_roles_rel', 'process_transition_id', 'role_id', 'Roles Required'),
166         'note': fields.text('Description'),
167     }
168     _defaults = {
169     }
170 process_transition()
171
172 class process_transition_action(osv.osv):
173     _name = 'process.transition.action'
174     _description ='Process Transitions Actions'
175     _columns = {
176         'name': fields.char('Name', size=32, required=True),
177         'state': fields.selection([('dummy','Dummy'),
178                                    ('method','Object Method'),
179                                    ('workflow','Workflow Trigger'),
180                                    ('action','Action')], 'Type', required=True),
181         'action': fields.char('Action ID', size=64, states={
182             'dummy':[('readonly',1)],
183             'method':[('required',1)],
184             'workflow':[('required',1)],
185             'action':[('required',1)],
186         },),
187         'transition_id': fields.many2one('process.transition', 'Transition', required=True, ondelete='cascade')
188     }
189     _defaults = {
190         'state': lambda *args: 'dummy',
191     }
192 process_transition_action()
193
194
195
196
197 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
198