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