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