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