1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 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 ##############################################################################
24 from osv import fields, osv
28 def __init__(self, obj, user):
32 def __getitem__(self, name):
33 if name in ('__obj', '__user'):
34 return super(Env, self).__getitem__(name)
39 return self.__obj[name]
41 class process_process(osv.osv):
42 _name = "process.process"
43 _description = "Process"
45 'name': fields.char('Name', size=30,required=True, translate=True),
46 'active': fields.boolean('Active', help="If the active field is set to true, it will allow you to hide the process without removing it."),
47 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
48 'note': fields.text('Notes', translate=True),
49 'node_ids': fields.one2many('process.node', 'process_id', 'Nodes')
52 'active' : lambda *a: True,
55 def search_by_model(self, cr, uid, res_model, context=None):
56 pool = pooler.get_pool(cr.dbname)
57 model_ids = (res_model or None) and pool.get('ir.model').search(cr, uid, [('model', '=', res_model)])
59 domain = (model_ids or []) and [('model_id', 'in', model_ids)]
62 # search all processes
63 res = pool.get('process.process').search(cr, uid, domain)
65 res = pool.get('process.process').browse(cr, uid, res, context=context)
67 result.append((process.id, process.name))
70 # else search process nodes
71 res = pool.get('process.node').search(cr, uid, domain)
73 res = pool.get('process.node').browse(cr, uid, res, context=context)
75 if (node.process_id.id, node.process_id.name) not in result:
76 result.append((node.process_id.id, node.process_id.name))
80 def graph_get(self, cr, uid, id, res_model, res_id, scale, context=None):
82 pool = pooler.get_pool(cr.dbname)
84 process = pool.get('process.process').browse(cr, uid, [id], context=context)[0]
95 states = dict(pool.get(res_model).fields_get(cr, uid, context=context).get('state', {}).get('selection', {}))
98 current_object = pool.get(res_model).browse(cr, uid, [res_id], context=context)[0]
99 current_user = pool.get('res.users').browse(cr, uid, [uid], context=context)[0]
100 expr_context = Env(current_object, current_user)
101 resource = current_object.name
102 if 'state' in current_object:
103 state = states.get(current_object.state, 'N/A')
104 perm = pool.get(res_model).perm_read(cr, uid, [res_id], context=context)[0]
106 notes = process.note or "N/A"
111 for node in process.node_ids:
113 data['name'] = node.name
114 data['model'] = (node.model_id or None) and node.model_id.model
115 data['kind'] = node.kind
116 data['subflow'] = (node.subflow_id or False) and [node.subflow_id.id, node.subflow_id.name]
117 data['notes'] = node.note
118 data['active'] = False
120 data['url'] = node.help_url
122 # get assosiated workflow
124 wkf_ids = self.pool.get('workflow').search(cr, uid, [('osv', '=', data['model'])])
125 data['workflow'] = (wkf_ids or False) and wkf_ids[0]
127 if 'directory_id' in node and node.directory_id:
128 data['directory_id'] = node.directory_id.id
129 data['directory'] = self.pool.get('document.directory').get_resource_path(cr, uid, data['directory_id'], data['model'], False)
132 data['menu'] = {'name': node.menu_id.complete_name, 'id': node.menu_id.id}
134 if node.model_id and node.model_id.model == res_model:
136 data['active'] = eval(node.model_states, expr_context)
140 if not data['active']:
143 for cond in node.condition_ids:
144 if cond.model_id and cond.model_id.model == res_model:
145 gray = gray and eval(cond.model_states, expr_context)
146 data['gray'] = not gray
150 nodes[node.id] = data
152 start.append(node.id)
154 for tr in node.transition_out:
156 data['name'] = tr.name
157 data['source'] = tr.source_node_id.id
158 data['target'] = tr.target_node_id.id
159 data['notes'] = tr.note
160 data['buttons'] = buttons = []
161 for b in tr.action_ids:
163 button['name'] = b.name
164 button['state'] = b.state
165 button['action'] = b.action
166 buttons.append(button)
167 data['groups'] = groups = []
168 for r in tr.transition_ids:
170 groups.append({'name': r.group_id.name})
171 for r in tr.group_ids:
172 groups.append({'name': r.name})
173 transitions[tr.id] = data
175 # now populate resource information
176 def update_relatives(nid, ref_id, ref_model):
179 for dummy, tr in transitions.items():
180 if tr['source'] == nid:
181 relatives.append(tr['target'])
182 if tr['target'] == nid:
183 relatives.append(tr['source'])
186 nodes[nid]['res'] = False
189 nodes[nid]['res'] = resource = {'id': ref_id, 'model': ref_model}
191 refobj = pool.get(ref_model).browse(cr, uid, [ref_id], context=context)[0]
192 fields = pool.get(ref_model).fields_get(cr, uid, context=context)
194 # check for directory_id from inherited from document module
195 if nodes[nid].get('directory_id', False):
196 resource['directory'] = self.pool.get('document.directory').get_resource_path(cr, uid, nodes[nid]['directory_id'], ref_model, ref_id)
198 resource['name'] = refobj.name_get(context)[0][1]
199 resource['perm'] = pool.get(ref_model).perm_read(cr, uid, [ref_id], context)[0]
203 if 'res' not in node:
204 for n, f in fields.items():
205 if node['model'] == ref_model:
206 update_relatives(r, ref_id, ref_model)
208 elif f.get('relation') == node['model']:
210 if rel and isinstance(rel, list) :
212 try: # XXX: rel has been reported as string (check it)
213 _id = (rel or False) and rel.id
214 _model = node['model']
215 update_relatives(r, _id, _model)
220 for nid, node in nodes.items():
221 if not node['gray'] and (node['active'] or node['model'] == res_model):
222 update_relatives(nid, res_id, res_model)
225 # calculate graph layout
226 g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
228 g.scale(*scale) #g.scale(100, 100, 180, 120)
229 graph = g.result_get()
231 # fix the height problem
233 for k,v in nodes.items():
242 for k, v in nodes.items():
244 v['y'] = min(y - miny + 10, y)
246 return dict(name=name, resource=resource, state=state, perm=perm, notes=notes, nodes=nodes, transitions=transitions)
248 def copy(self, cr, uid, id, default=None, context=None):
249 """ Deep copy the entire process.
255 pool = pooler.get_pool(cr.dbname)
256 process = pool.get('process.process').browse(cr, uid, [id], context=context)[0]
261 # first copy all nodes and and map the new nodes with original for later use in transitions
262 for node in process.node_ids:
263 for t in node.transition_in:
264 tr = transitions.setdefault(t.id, {})
265 tr['target'] = node.id
266 for t in node.transition_out:
267 tr = transitions.setdefault(t.id, {})
268 tr['source'] = node.id
269 nodes[node.id] = pool.get('process.node').copy(cr, uid, node.id, context=context)
271 # then copy transitions with new nodes
272 for tid, tr in transitions.items():
274 'source_node_id': nodes[tr['source']],
275 'target_node_id': nodes[tr['target']]
277 tr = pool.get('process.transition').copy(cr, uid, tid, default=vals, context=context)
279 # and finally copy the process itself with new nodes
282 'node_ids': [(6, 0, nodes.values())]
284 return super(process_process, self).copy(cr, uid, id, default, context)
288 class process_node(osv.osv):
289 _name = 'process.node'
290 _description ='Process Node'
292 'name': fields.char('Name', size=30,required=True, translate=True),
293 'process_id': fields.many2one('process.process', 'Process', required=True, ondelete='cascade'),
294 'kind': fields.selection([('state','State'), ('subflow','Subflow')], 'Kind of Node', required=True),
295 'menu_id': fields.many2one('ir.ui.menu', 'Related Menu'),
296 'note': fields.text('Notes', translate=True),
297 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
298 'model_states': fields.char('States Expression', size=128),
299 'subflow_id': fields.many2one('process.process', 'Subflow', ondelete='set null'),
300 'flow_start': fields.boolean('Starting Flow'),
301 'transition_in': fields.one2many('process.transition', 'target_node_id', 'Starting Transitions'),
302 'transition_out': fields.one2many('process.transition', 'source_node_id', 'Ending Transitions'),
303 'condition_ids': fields.one2many('process.condition', 'node_id', 'Conditions'),
304 'help_url': fields.char('Help URL', size=255)
307 'kind': lambda *args: 'state',
308 'model_states': lambda *args: False,
309 'flow_start': lambda *args: False,
312 def copy_data(self, cr, uid, id, default=None, context=None):
319 return super(process_node, self).copy_data(cr, uid, id, default, context=context)
323 class process_node_condition(osv.osv):
324 _name = 'process.condition'
325 _description = 'Condition'
327 'name': fields.char('Name', size=30, required=True),
328 'node_id': fields.many2one('process.node', 'Node', required=True, ondelete='cascade'),
329 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
330 'model_states': fields.char('Expression', required=True, size=128)
332 process_node_condition()
334 class process_transition(osv.osv):
335 _name = 'process.transition'
336 _description ='Process Transition'
338 'name': fields.char('Name', size=32, required=True, translate=True),
339 'source_node_id': fields.many2one('process.node', 'Source Node', required=True, ondelete='cascade'),
340 'target_node_id': fields.many2one('process.node', 'Target Node', required=True, ondelete='cascade'),
341 'action_ids': fields.one2many('process.transition.action', 'transition_id', 'Buttons'),
342 'transition_ids': fields.many2many('workflow.transition', 'process_transition_ids', 'ptr_id', 'wtr_id', 'Workflow Transitions'),
343 'group_ids': fields.many2many('res.groups', 'process_transition_group_rel', 'tid', 'rid', string='Required Groups'),
344 'note': fields.text('Description', translate=True),
348 class process_transition_action(osv.osv):
349 _name = 'process.transition.action'
350 _description ='Process Transitions Actions'
352 'name': fields.char('Name', size=32, required=True, translate=True),
353 'state': fields.selection([('dummy','Dummy'),
354 ('object','Object Method'),
355 ('workflow','Workflow Trigger'),
356 ('action','Action')], 'Type', required=True),
357 'action': fields.char('Action ID', size=64, states={
358 'dummy':[('readonly',1)],
359 'object':[('required',1)],
360 'workflow':[('required',1)],
361 'action':[('required',1)],
363 'transition_id': fields.many2one('process.transition', 'Transition', required=True, ondelete='cascade')
366 'state': lambda *args: 'dummy',
369 def copy_data(self, cr, uid, id, default=None, context=None):
373 state = self.pool.get('process.transition.action').browse(cr, uid, [id], context=context)[0].state
375 default['state'] = state
377 return super(process_transition_action, self).copy_data(cr, uid, id, default, context)
379 process_transition_action()
381 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: