process graph_get returns more info.
[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         current_object = res_model
50         pool = pooler.get_pool(cr.dbname)
51
52         process = pool.get('process.process').browse(cr, uid, [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.name
63             data['model'] = node.model_id.model
64
65             nodes[node.id] = data
66
67             if node.flow_start:
68                 start.append(node.id)
69
70             for tr in node.transition_out:
71                 data = {}
72                 
73                 data['name'] = tr.name
74                 data['source'] = tr.node_from_id.id
75                 data['target'] = tr.node_to_id.id
76
77                 data['buttons'] = buttons = []
78                 for b in tr.action_ids:
79                     button = {}
80                     button['name'] = b.name
81                     buttons.append(button)
82
83                 data['roles'] = roles = []
84                 for r in tr.transition_ids:
85                     role = {}
86                     role['name'] = r.role_id.name
87                     roles.append(role)
88
89                 transitions[tr.id] = data
90
91         g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
92         g.process(start)
93         #g.scale(100, 100, 180, 120)
94         g.scale(*scale)
95
96         graph = g.result_get()
97
98         miny = -1
99
100         for k,v in nodes.items():
101
102             x = graph[k]['y']
103             y = graph[k]['x']
104
105             if miny == -1:
106                 miny = y
107
108             miny = min(y, miny)
109
110             v['x'] = x
111             v['y'] = y
112
113         for k, v in nodes.items():
114             y = v['y']
115             v['y'] = min(y - miny + 10, y)
116
117         return dict(nodes=nodes, transitions=transitions)
118
119 process_process()
120
121 class process_node(osv.osv):
122     _name = 'process.node'
123     _description ='Process Nodes'
124     _columns = {
125         'name': fields.char('Name', size=30,required=True),
126         'process_id': fields.many2one('process.process', 'Process', required=True),
127         'kind': fields.selection([('state','State'),('router','Router'),('subflow','Subflow')],'Kind of Node', required=True),
128         'menu_id': fields.many2one('ir.ui.menu', 'Related Menu'),
129         'note': fields.text('Notes'),
130         'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
131         'model_states': fields.char('States Expression', size=128),
132         'flow_start': fields.boolean('Starting Flow'),
133         'transition_in': fields.one2many('process.transition', 'node_to_id', 'Starting Transitions'),
134         'transition_out': fields.one2many('process.transition', 'node_from_id', 'Ending Transitions'),
135     }
136     _defaults = {
137         'kind': lambda *args: 'state',
138         'model_states': lambda *args: False,
139         'flow_start': lambda *args: False,
140     }
141 process_node()
142
143 class process_transition(osv.osv):
144     _name = 'process.transition'
145     _description ='Process Transitions'
146     _columns = {
147         'name': fields.char('Name', size=32, required=True),
148         'node_from_id': fields.many2one('process.node', 'Origin Node', required=True, ondelete='cascade'),
149         'node_to_id': fields.many2one('process.node', 'Destination Node', required=True, ondelete='cascade'),
150         'transition_ids': fields.many2many('workflow.transition', 'process_transition_ids', 'trans1_id', 'trans2_id', 'Workflow Transitions'),
151         'note': fields.text('Description'),
152         'action_ids': fields.one2many('process.transition.action', 'transition_id', 'Buttons')
153     }
154     _defaults = {
155     }
156 process_transition()
157
158 class process_transition_action(osv.osv):
159     _name = 'process.transition.action'
160     _description ='Process Transitions Actions'
161     _columns = {
162         'name': fields.char('Name', size=32, required=True),
163         'state': fields.selection([('dummy','Dummy'),('method','Object Method'),('workflow','Workflow Trigger'),('action','Action')], 'Type', required=True),
164         'action': fields.char('Action ID', size=64, states={
165             'dummy':[('readonly',1)],
166             'method':[('required',1)],
167             'workflow':[('required',1)],
168             'action':[('required',1)],
169         },),
170         'transition_id': fields.many2one('process.transition', 'Transition', required=True, ondelete='cascade')
171     }
172     _defaults = {
173         'state': lambda *args: 'dummy',
174     }
175 process_transition_action()
176
177
178
179
180 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
181