minor fix
[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 Env(dict):
35     
36     def __init__(self, obj, user):
37         self.__obj = obj
38         self.__usr = user
39         
40     def __getitem__(self, name):
41         
42         if name in ('__obj', '__user'):
43             return super(ExprContext, self).__getitem__(name)
44         
45         if name == 'user':
46             return self.__user
47         
48         if name == 'object':
49             return self.__obj
50         
51         return self.__obj[name]
52
53 class process_process(osv.osv):
54     _name = "process.process"
55     _description = "Process"
56     _columns = {
57         'name': fields.char('Name', size=30,required=True, translate=True),
58         'active': fields.boolean('Active'),
59         'note': fields.text('Notes', translate=True),
60         'node_ids': fields.one2many('process.node', 'process_id', 'Nodes')
61     }
62     _defaults = {
63         'active' : lambda *a: True,
64     }
65
66     def graph_get(self, cr, uid, id, res_model, res_id, scale, context):
67         
68         pool = pooler.get_pool(cr.dbname)
69         
70         process = pool.get('process.process').browse(cr, uid, [id])[0]
71         current_object = pool.get(res_model).browse(cr, uid, [res_id], context)[0]
72         current_user = pool.get('res.users').browse(cr, uid, [uid], context)[0]
73         
74         expr_context = Env(current_object, current_user)
75
76         fields = pool.get(res_model).fields_get(cr, uid, context=context)
77
78         def get_resource_info(node):
79             ret = False
80
81             for name, field in fields.items():
82                 if node.model_id and field.get('relation', False) == node.model_id.model:
83                     #TODO: get resource info
84                     pass
85
86             return ret
87         
88         nodes = {}
89         start = []
90         transitions = {}
91
92         for node in process.node_ids:
93             data = {}
94             data['name'] = node.name
95             data['model'] = (node.model_id or None) and node.model_id.model
96             data['kind'] = node.kind
97             data['subflow'] = (node.subflow_id or None) and node.subflow_id.id
98             data['notes'] = node.note
99             data['active'] = False
100             data['gray'] = False
101             data['res'] = get_resource_info(node)
102
103             if node.menu_id:
104                 data['menu'] = {'name': node.menu_id.complete_name, 'id': node.menu_id.id}
105             
106             if node.model_id and node.model_id.model == res_model:
107
108                 data['res'] = resource = {}
109                 resource['name'] = current_object.name_get(context)[0][1]
110                 resource['model'] = res_model
111                 resource['id'] = res_id
112
113                 try:
114                     data['active'] = eval(node.model_states, expr_context)
115                 except Exception, e:
116                     # waring: invalid state expression
117                     pass
118
119             if not data['active']:
120                 try:
121                     gray = True
122                     for cond in node.condition_ids:
123                         if cond.model_id and cond.model_id.model == res_model:
124                             gray = gray and eval(cond.model_states, expr_context)
125                     data['gray'] = not gray
126                 except:
127                     pass
128
129             nodes[node.id] = data
130             if node.flow_start:
131                 start.append(node.id)
132
133             for tr in node.transition_out:
134                 data = {}
135                 data['name'] = tr.name
136                 data['source'] = tr.source_node_id.id
137                 data['target'] = tr.target_node_id.id
138                 data['notes'] = tr.note
139                 data['buttons'] = buttons = []
140                 for b in tr.action_ids:
141                     button = {}
142                     button['name'] = b.name
143                     button['state'] = b.state
144                     button['action'] = b.action
145                     buttons.append(button)
146                 data['roles'] = roles = []
147                 for r in tr.transition_ids:
148                     if r.role_id:
149                         role = {}
150                         role['name'] = r.role_id.name
151                         roles.append(role)
152                 transitions[tr.id] = data
153
154         g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
155         g.process(start)
156         #g.scale(100, 100, 180, 120)
157         g.scale(*scale)
158         graph = g.result_get()
159         miny = -1
160
161         for k,v in nodes.items():
162             x = graph[k]['y']
163             y = graph[k]['x']
164             if miny == -1:
165                 miny = y
166             miny = min(y, miny)
167             v['x'] = x
168             v['y'] = y
169
170         for k, v in nodes.items():
171             y = v['y']
172             v['y'] = min(y - miny + 10, y)
173         return dict(nodes=nodes, transitions=transitions)
174
175 process_process()
176
177 class process_node(osv.osv):
178     _name = 'process.node'
179     _description ='Process Nodes'
180     _columns = {
181         'name': fields.char('Name', size=30,required=True, translate=True),
182         'process_id': fields.many2one('process.process', 'Process', required=True, ondelete='cascade'),
183         'kind': fields.selection([('state','State'), ('subflow','Subflow')], 'Kind of Node', required=True),
184         'menu_id': fields.many2one('ir.ui.menu', 'Related Menu'),
185         'note': fields.text('Notes', translate=True),
186         'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
187         'model_states': fields.char('States Expression', size=128),
188         'subflow_id': fields.many2one('process.process', 'Subflow', ondelete='set null'),
189         'flow_start': fields.boolean('Starting Flow'),
190         'transition_in': fields.one2many('process.transition', 'target_node_id', 'Starting Transitions'),
191         'transition_out': fields.one2many('process.transition', 'source_node_id', 'Ending Transitions'),
192         'condition_ids': fields.one2many('process.condition', 'node_id', 'Conditions')
193     }
194     _defaults = {
195         'kind': lambda *args: 'state',
196         'model_states': lambda *args: False,
197         'flow_start': lambda *args: False,
198     }
199 process_node()
200
201 class process_node_condition(osv.osv):
202     _name = 'process.condition'
203     _description = 'Condition'
204     _columns = {
205         'name': fields.char('Name', size=30, required=True),
206         'node_id': fields.many2one('process.node', 'Node', required=True, ondelete='cascade'),
207         'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
208         'model_states': fields.char('Expression', required=True, size=128)
209     }
210 process_node_condition()
211
212 class process_transition(osv.osv):
213     _name = 'process.transition'
214     _description ='Process Transitions'
215     _columns = {
216         'name': fields.char('Name', size=32, required=True, translate=True),
217         'source_node_id': fields.many2one('process.node', 'Source Node', required=True, ondelete='cascade'),
218         'target_node_id': fields.many2one('process.node', 'Target Node', required=True, ondelete='cascade'),
219         'action_ids': fields.one2many('process.transition.action', 'transition_id', 'Buttons'),
220         'transition_ids': fields.many2many('workflow.transition', 'process_transition_ids', 'ptr_id', 'wtr_id', 'Workflow Transitions'),
221         'note': fields.text('Description', translate=True),
222     }
223 process_transition()
224
225 class process_transition_action(osv.osv):
226     _name = 'process.transition.action'
227     _description ='Process Transitions Actions'
228     _columns = {
229         'name': fields.char('Name', size=32, required=True, translate=True),
230         'state': fields.selection([('dummy','Dummy'),
231                                    ('object','Object Method'),
232                                    ('workflow','Workflow Trigger'),
233                                    ('action','Action')], 'Type', required=True),
234         'action': fields.char('Action ID', size=64, states={
235             'dummy':[('readonly',1)],
236             'object':[('required',1)],
237             'workflow':[('required',1)],
238             'action':[('required',1)],
239         },),
240         'transition_id': fields.many2one('process.transition', 'Transition', required=True, ondelete='cascade')
241     }
242     _defaults = {
243         'state': lambda *args: 'dummy',
244     }
245 process_transition_action()
246