1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
25 from osv import fields, osv
29 def __init__(self, obj, user):
33 def __getitem__(self, name):
35 if name in ('__obj', '__user'):
36 return super(ExprContext, self).__getitem__(name)
44 return self.__obj[name]
46 class process_process(osv.osv):
47 _name = "process.process"
48 _description = "Process"
50 'name': fields.char('Name', size=30,required=True, translate=True),
51 'active': fields.boolean('Active'),
52 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
53 'note': fields.text('Notes', translate=True),
54 'node_ids': fields.one2many('process.node', 'process_id', 'Nodes')
57 'active' : lambda *a: True,
60 def search_by_model(self, cr, uid, res_model, context):
61 pool = pooler.get_pool(cr.dbname)
62 model_ids = (res_model or None) and pool.get('ir.model').search(cr, uid, [('model', '=', res_model)])
64 domain = (model_ids or []) and [('model_id', 'in', model_ids)]
67 # search all processes
68 res = pool.get('process.process').search(cr, uid, domain)
70 res = pool.get('process.process').browse(cr, uid, res, context)
72 result.append((process.id, process.name))
75 # else search process nodes
76 res = pool.get('process.node').search(cr, uid, domain)
78 res = pool.get('process.node').browse(cr, uid, res, context)
80 if (node.process_id.id, node.process_id.name) not in result:
81 result.append((node.process_id.id, node.process_id.name))
85 def graph_get(self, cr, uid, id, res_model, res_id, scale, context):
87 pool = pooler.get_pool(cr.dbname)
89 process = pool.get('process.process').browse(cr, uid, [id], context)[0]
100 states = dict(pool.get(res_model).fields_get(cr, uid, context=context).get('state', {}).get('selection', {}))
103 current_object = pool.get(res_model).browse(cr, uid, [res_id], context)[0]
104 current_user = pool.get('res.users').browse(cr, uid, [uid], context)[0]
105 expr_context = Env(current_object, current_user)
106 resource = current_object.name
107 if 'state' in current_object:
108 state = states.get(current_object.state, 'N/A')
109 perm = pool.get(res_model).perm_read(cr, uid, [res_id], context)[0]
111 notes = process.note or "N/A"
116 for node in process.node_ids:
118 data['name'] = node.name
119 data['model'] = (node.model_id or None) and node.model_id.model
120 data['kind'] = node.kind
121 data['subflow'] = (node.subflow_id or False) and [node.subflow_id.id, node.subflow_id.name]
122 data['notes'] = node.note
123 data['active'] = False
125 data['url'] = node.help_url
127 # get assosiated workflow
129 wkf_ids = self.pool.get('workflow').search(cr, uid, [('osv', '=', data['model'])])
130 data['workflow'] = (wkf_ids or False) and wkf_ids[0]
132 if 'directory_id' in node and node.directory_id:
133 data['directory_id'] = node.directory_id.id
134 data['directory'] = self.pool.get('document.directory').get_resource_path(cr, uid, data['directory_id'], data['model'], False)
137 data['menu'] = {'name': node.menu_id.complete_name, 'id': node.menu_id.id}
139 if node.model_id and node.model_id.model == res_model:
141 data['active'] = eval(node.model_states, expr_context)
145 if not data['active']:
148 for cond in node.condition_ids:
149 if cond.model_id and cond.model_id.model == res_model:
150 gray = gray and eval(cond.model_states, expr_context)
151 data['gray'] = not gray
155 nodes[node.id] = data
157 start.append(node.id)
159 for tr in node.transition_out:
161 data['name'] = tr.name
162 data['source'] = tr.source_node_id.id
163 data['target'] = tr.target_node_id.id
164 data['notes'] = tr.note
165 data['buttons'] = buttons = []
166 for b in tr.action_ids:
168 button['name'] = b.name
169 button['state'] = b.state
170 button['action'] = b.action
171 buttons.append(button)
172 data['roles'] = roles = []
173 for r in tr.transition_ids:
176 role['name'] = r.role_id.name
178 for r in tr.role_ids:
180 role['name'] = r.name
182 transitions[tr.id] = data
184 # now populate resource information
185 def update_relatives(nid, ref_id, ref_model):
188 for tid, tr in transitions.items():
189 if tr['source'] == nid:
190 relatives.append(tr['target'])
191 if tr['target'] == nid:
192 relatives.append(tr['source'])
195 nodes[nid]['res'] = False
198 nodes[nid]['res'] = resource = {'id': ref_id, 'model': ref_model}
200 refobj = pool.get(ref_model).browse(cr, uid, [ref_id], context)[0]
201 fields = pool.get(ref_model).fields_get(cr, uid, context=context)
203 # check for directory_id from inherited from document module
204 if nodes[nid].get('directory_id', False):
205 resource['directory'] = self.pool.get('document.directory').get_resource_path(cr, uid, nodes[nid]['directory_id'], ref_model, ref_id)
207 resource['name'] = refobj.name_get(context)[0][1]
208 resource['perm'] = pool.get(ref_model).perm_read(cr, uid, [ref_id], context)[0]
212 if 'res' not in node:
213 for n, f in fields.items():
214 if node['model'] == ref_model:
215 update_relatives(r, ref_id, ref_model)
217 elif f.get('relation') == node['model']:
219 if rel and isinstance(rel, list) :
221 try: # XXX: rel has been reported as string (check it)
222 _id = (rel or False) and rel.id
223 _model = node['model']
224 update_relatives(r, _id, _model)
229 for nid, node in nodes.items():
230 if not node['gray'] and (node['active'] or node['model'] == res_model):
231 update_relatives(nid, res_id, res_model)
234 # calculate graph layout
235 g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
237 g.scale(*scale) #g.scale(100, 100, 180, 120)
238 graph = g.result_get()
240 # fix the height problem
242 for k,v in nodes.items():
251 for k, v in nodes.items():
253 v['y'] = min(y - miny + 10, y)
255 return dict(name=name, resource=resource, state=state, perm=perm, notes=notes, nodes=nodes, transitions=transitions)
257 def copy(self, cr, uid, id, default=None, context={}):
258 """ Deep copy the entire process.
264 pool = pooler.get_pool(cr.dbname)
265 process = pool.get('process.process').browse(cr, uid, [id], context)[0]
270 # first copy all nodes and and map the new nodes with original for later use in transitions
271 for node in process.node_ids:
272 for t in node.transition_in:
273 tr = transitions.setdefault(t.id, {})
274 tr['target'] = node.id
275 for t in node.transition_out:
276 tr = transitions.setdefault(t.id, {})
277 tr['source'] = node.id
278 nodes[node.id] = pool.get('process.node').copy(cr, uid, node.id, context=context)
280 # then copy transitions with new nodes
281 for tid, tr in transitions.items():
283 'source_node_id': nodes[tr['source']],
284 'target_node_id': nodes[tr['target']]
286 tr = pool.get('process.transition').copy(cr, uid, tid, default=vals, context=context)
288 # and finally copy the process itself with new nodes
291 'node_ids': [(6, 0, nodes.values())]
293 return super(process_process, self).copy(cr, uid, id, default, context)
297 class process_node(osv.osv):
298 _name = 'process.node'
299 _description ='Process Nodes'
301 'name': fields.char('Name', size=30,required=True, translate=True),
302 'process_id': fields.many2one('process.process', 'Process', required=True, ondelete='cascade'),
303 'kind': fields.selection([('state','State'), ('subflow','Subflow')], 'Kind of Node', required=True),
304 'menu_id': fields.many2one('ir.ui.menu', 'Related Menu'),
305 'note': fields.text('Notes', translate=True),
306 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
307 'model_states': fields.char('States Expression', size=128),
308 'subflow_id': fields.many2one('process.process', 'Subflow', ondelete='set null'),
309 'flow_start': fields.boolean('Starting Flow'),
310 'transition_in': fields.one2many('process.transition', 'target_node_id', 'Starting Transitions'),
311 'transition_out': fields.one2many('process.transition', 'source_node_id', 'Ending Transitions'),
312 'condition_ids': fields.one2many('process.condition', 'node_id', 'Conditions'),
313 'help_url': fields.char('Help URL', size=255)
316 'kind': lambda *args: 'state',
317 'model_states': lambda *args: False,
318 'flow_start': lambda *args: False,
321 def copy_data(self, cr, uid, id, default=None, context={}):
328 return super(process_node, self).copy_data(cr, uid, id, default, context)
332 class process_node_condition(osv.osv):
333 _name = 'process.condition'
334 _description = 'Condition'
336 'name': fields.char('Name', size=30, required=True),
337 'node_id': fields.many2one('process.node', 'Node', required=True, ondelete='cascade'),
338 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
339 'model_states': fields.char('Expression', required=True, size=128)
341 process_node_condition()
343 class process_transition(osv.osv):
344 _name = 'process.transition'
345 _description ='Process Transitions'
347 'name': fields.char('Name', size=32, required=True, translate=True),
348 'source_node_id': fields.many2one('process.node', 'Source Node', required=True, ondelete='cascade'),
349 'target_node_id': fields.many2one('process.node', 'Target Node', required=True, ondelete='cascade'),
350 'action_ids': fields.one2many('process.transition.action', 'transition_id', 'Buttons'),
351 'transition_ids': fields.many2many('workflow.transition', 'process_transition_ids', 'ptr_id', 'wtr_id', 'Workflow Transitions'),
352 'role_ids': fields.many2many('res.roles', 'process_transition_roles_rel', 'tid', 'rid', 'Roles'),
353 'note': fields.text('Description', translate=True),
357 class process_transition_action(osv.osv):
358 _name = 'process.transition.action'
359 _description ='Process Transitions Actions'
361 'name': fields.char('Name', size=32, required=True, translate=True),
362 'state': fields.selection([('dummy','Dummy'),
363 ('object','Object Method'),
364 ('workflow','Workflow Trigger'),
365 ('action','Action')], 'Type', required=True),
366 'action': fields.char('Action ID', size=64, states={
367 'dummy':[('readonly',1)],
368 'object':[('required',1)],
369 'workflow':[('required',1)],
370 'action':[('required',1)],
372 'transition_id': fields.many2one('process.transition', 'Transition', required=True, ondelete='cascade')
375 'state': lambda *args: 'dummy',
378 def copy_data(self, cr, uid, id, default=None, context={}):
382 state = self.pool.get('process.transition.action').browse(cr, uid, [id], context)[0].state
384 default['state'] = state
386 return super(process_transition_action, self).copy_data(cr, uid, id, default, context)
388 process_transition_action()
390 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: