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