1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2005-TODAY TINY SPRL. (http://tiny.be) All Rights Reserved.
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
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.
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.
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.
27 ##############################################################################
32 from osv import fields, osv
36 def __init__(self, obj, user):
40 def __getitem__(self, name):
42 if name in ('__obj', '__user'):
43 return super(ExprContext, self).__getitem__(name)
51 return self.__obj[name]
53 class process_process(osv.osv):
54 _name = "process.process"
55 _description = "Process"
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')
63 'active' : lambda *a: True,
66 def search_by_model(self, cr, uid, res_model, context):
67 pool = pooler.get_pool(cr.dbname)
69 model_ids = pool.get('ir.model').search(cr, uid, [('model', '=', res_model)])
73 nodes = pool.get('process.node').search(cr, uid, [('model_id', 'in', model_ids)])
77 nodes = pool.get('process.node').browse(cr, uid, nodes, context)
83 if node.process_id.id not in unique:
84 result.append((node.process_id.id, node.process_id.name))
85 unique.append(node.process_id.id)
89 def graph_get(self, cr, uid, id, res_model, res_id, scale, context):
91 pool = pooler.get_pool(cr.dbname)
93 process = pool.get('process.process').browse(cr, uid, [id])[0]
94 current_object = pool.get(res_model).browse(cr, uid, [res_id], context)[0]
95 current_user = pool.get('res.users').browse(cr, uid, [uid], context)[0]
97 expr_context = Env(current_object, current_user)
104 states = dict(pool.get(res_model).fields_get(cr, uid, context=context).get('state', {}).get('selection', {}))
105 title = "%s - Resource: %s, State: %s" % (process.name, current_object.name, states.get(getattr(current_object, 'state'), 'N/A'))
107 perm = pool.get(res_model).perm_read(cr, uid, [res_id], context)[0]
109 for node in process.node_ids:
111 data['name'] = node.name
112 data['model'] = (node.model_id or None) and node.model_id.model
113 data['kind'] = node.kind
114 data['subflow'] = (node.subflow_id or False) and [node.subflow_id.id, node.subflow_id.name]
115 data['notes'] = node.note
116 data['active'] = False
118 data['url'] = node.help_url
121 data['menu'] = {'name': node.menu_id.complete_name, 'id': node.menu_id.id}
123 if node.model_id and node.model_id.model == res_model:
125 data['active'] = eval(node.model_states, expr_context)
127 # waring: invalid state expression
130 if not data['active']:
133 for cond in node.condition_ids:
134 if cond.model_id and cond.model_id.model == res_model:
135 gray = gray and eval(cond.model_states, expr_context)
136 data['gray'] = not gray
140 nodes[node.id] = data
142 start.append(node.id)
144 for tr in node.transition_out:
146 data['name'] = tr.name
147 data['source'] = tr.source_node_id.id
148 data['target'] = tr.target_node_id.id
149 data['notes'] = tr.note
150 data['buttons'] = buttons = []
151 for b in tr.action_ids:
153 button['name'] = b.name
154 button['state'] = b.state
155 button['action'] = b.action
156 buttons.append(button)
157 data['roles'] = roles = []
158 for r in tr.transition_ids:
161 role['name'] = r.role_id.name
163 for r in tr.role_ids:
165 role['name'] = r.name
167 transitions[tr.id] = data
169 # now populate resource information
170 def update_relatives(nid, ref_id, ref_model):
173 for tid, tr in transitions.items():
174 if tr['source'] == nid:
175 relatives.append(tr['target'])
176 if tr['target'] == nid:
177 relatives.append(tr['source'])
180 nodes[nid]['res'] = False
183 nodes[nid]['res'] = resource = {'id': ref_id, 'model': ref_model}
185 refobj = pool.get(ref_model).browse(cr, uid, [ref_id], context)[0]
186 fields = pool.get(ref_model).fields_get(cr, uid, context=context)
188 resource['name'] = refobj.name_get(context)[0][1]
189 resource['perm'] = pool.get(ref_model).perm_read(cr, uid, [ref_id], context)[0]
193 if 'res' not in node:
194 for n, f in fields.items():
195 if node['model'] == ref_model:
196 update_relatives(r, ref_id, ref_model)
198 elif f.get('relation') == node['model']:
200 if rel and isinstance(rel, list) :
202 _id = (rel or False) and rel.id
203 _model = node['model']
204 update_relatives(r, _id, _model)
206 for nid, node in nodes.items():
207 if not node['gray'] and (node['active'] or node['model'] == res_model):
208 update_relatives(nid, res_id, res_model)
211 # calculate graph layout
212 g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
214 g.scale(*scale) #g.scale(100, 100, 180, 120)
215 graph = g.result_get()
217 # fix the height problem
219 for k,v in nodes.items():
228 for k, v in nodes.items():
230 v['y'] = min(y - miny + 10, y)
232 return dict(title=title, perm=perm, notes=notes, nodes=nodes, transitions=transitions)
234 def copy(self, cr, uid, id, default=None, context={}):
235 """ Deep copy the entire process.
241 pool = pooler.get_pool(cr.dbname)
242 process = pool.get('process.process').browse(cr, uid, [id], context)[0]
247 # first copy all nodes and and map the new nodes with original for later use in transitions
248 for node in process.node_ids:
249 for t in node.transition_in:
250 tr = transitions.setdefault(t.id, {})
251 tr['target'] = node.id
252 for t in node.transition_out:
253 tr = transitions.setdefault(t.id, {})
254 tr['source'] = node.id
255 nodes[node.id] = pool.get('process.node').copy(cr, uid, node.id, context=context)
257 # then copy transitions with new nodes
258 for tid, tr in transitions.items():
260 'source_node_id': nodes[tr['source']],
261 'target_node_id': nodes[tr['target']]
263 tr = pool.get('process.transition').copy(cr, uid, tid, default=vals, context=context)
265 # and finally copy the process itself with new nodes
268 'node_ids': [(6, 0, nodes.values())]
270 return super(process_process, self).copy(cr, uid, id, default, context)
274 class process_node(osv.osv):
275 _name = 'process.node'
276 _description ='Process Nodes'
278 'name': fields.char('Name', size=30,required=True, translate=True),
279 'process_id': fields.many2one('process.process', 'Process', required=True, ondelete='cascade'),
280 'kind': fields.selection([('state','State'), ('subflow','Subflow')], 'Kind of Node', required=True),
281 'menu_id': fields.many2one('ir.ui.menu', 'Related Menu'),
282 'note': fields.text('Notes', translate=True),
283 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
284 'model_states': fields.char('States Expression', size=128),
285 'subflow_id': fields.many2one('process.process', 'Subflow', ondelete='set null'),
286 'flow_start': fields.boolean('Starting Flow'),
287 'transition_in': fields.one2many('process.transition', 'target_node_id', 'Starting Transitions'),
288 'transition_out': fields.one2many('process.transition', 'source_node_id', 'Ending Transitions'),
289 'condition_ids': fields.one2many('process.condition', 'node_id', 'Conditions'),
290 'help_url': fields.char('Help URL', size=255)
293 'kind': lambda *args: 'state',
294 'model_states': lambda *args: False,
295 'flow_start': lambda *args: False,
298 def copy(self, cr, uid, id, default=None, context={}):
305 return super(process_node, self).copy(cr, uid, id, default, context)
309 class process_node_condition(osv.osv):
310 _name = 'process.condition'
311 _description = 'Condition'
313 'name': fields.char('Name', size=30, required=True),
314 'node_id': fields.many2one('process.node', 'Node', required=True, ondelete='cascade'),
315 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
316 'model_states': fields.char('Expression', required=True, size=128)
318 process_node_condition()
320 class process_transition(osv.osv):
321 _name = 'process.transition'
322 _description ='Process Transitions'
324 'name': fields.char('Name', size=32, required=True, translate=True),
325 'source_node_id': fields.many2one('process.node', 'Source Node', required=True, ondelete='cascade'),
326 'target_node_id': fields.many2one('process.node', 'Target Node', required=True, ondelete='cascade'),
327 'action_ids': fields.one2many('process.transition.action', 'transition_id', 'Buttons'),
328 'transition_ids': fields.many2many('workflow.transition', 'process_transition_ids', 'ptr_id', 'wtr_id', 'Workflow Transitions'),
329 'role_ids': fields.many2many('res.roles', 'process_transition_roles_rel', 'tid', 'rid', 'Roles'),
330 'note': fields.text('Description', translate=True),
334 class process_transition_action(osv.osv):
335 _name = 'process.transition.action'
336 _description ='Process Transitions Actions'
338 'name': fields.char('Name', size=32, required=True, translate=True),
339 'state': fields.selection([('dummy','Dummy'),
340 ('object','Object Method'),
341 ('workflow','Workflow Trigger'),
342 ('action','Action')], 'Type', required=True),
343 'action': fields.char('Action ID', size=64, states={
344 'dummy':[('readonly',1)],
345 'object':[('required',1)],
346 'workflow':[('required',1)],
347 'action':[('required',1)],
349 'transition_id': fields.many2one('process.transition', 'Transition', required=True, ondelete='cascade')
352 'state': lambda *args: 'dummy',
354 process_transition_action()