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 graph_get(self, cr, uid, id, res_model, res_id, scale, context):
68 pool = pooler.get_pool(cr.dbname)
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]
74 expr_context = Env(current_object, current_user)
76 fields = pool.get(res_model).fields_get(cr, uid, context=context)
78 def get_resource_info(node):
81 for name, field in fields.items():
82 if node.model_id and field.get('relation', False) == node.model_id.model:
83 #TODO: get resource info
91 for node in process.node_ids:
93 data['name'] = node.name
94 data['model'] = (node.model_id or None) and node.model_id.model
95 data['kind'] = node.kind
96 data['subflow'] = (node.subflow_id or None) and node.subflow_id.id
97 data['notes'] = node.note
98 data['active'] = False
100 data['res'] = get_resource_info(node)
103 data['menu'] = {'name': node.menu_id.complete_name, 'id': node.menu_id.id}
105 if node.model_id and node.model_id.model == res_model:
107 data['res'] = resource = {}
108 resource['name'] = current_object.name_get(context)[0][1]
109 resource['model'] = res_model
110 resource['id'] = res_id
113 data['active'] = eval(node.model_states, expr_context)
115 # waring: invalid state expression
118 if not data['active']:
121 for cond in node.condition_ids:
122 if cond.model_id and cond.model_id.model == res_model:
123 gray = gray and eval(cond.model_states, expr_context)
124 data['gray'] = not gray
128 nodes[node.id] = data
130 start.append(node.id)
132 for tr in node.transition_out:
134 data['name'] = tr.name
135 data['source'] = tr.source_node_id.id
136 data['target'] = tr.target_node_id.id
137 data['notes'] = tr.note
138 data['buttons'] = buttons = []
139 for b in tr.action_ids:
141 button['name'] = b.name
142 button['state'] = b.state
143 button['action'] = b.action
144 buttons.append(button)
145 data['roles'] = roles = []
146 for r in tr.transition_ids:
149 role['name'] = r.role_id.name
151 transitions[tr.id] = data
153 g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
155 #g.scale(100, 100, 180, 120)
157 graph = g.result_get()
160 for k,v in nodes.items():
169 for k, v in nodes.items():
171 v['y'] = min(y - miny + 10, y)
172 return dict(nodes=nodes, transitions=transitions)
176 class process_node(osv.osv):
177 _name = 'process.node'
178 _description ='Process Nodes'
180 'name': fields.char('Name', size=30,required=True, translate=True),
181 'process_id': fields.many2one('process.process', 'Process', required=True, ondelete='cascade'),
182 'kind': fields.selection([('state','State'), ('subflow','Subflow')], 'Kind of Node', required=True),
183 'menu_id': fields.many2one('ir.ui.menu', 'Related Menu'),
184 'note': fields.text('Notes', translate=True),
185 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
186 'model_states': fields.char('States Expression', size=128),
187 'subflow_id': fields.many2one('process.process', 'Subflow', ondelete='set null'),
188 'flow_start': fields.boolean('Starting Flow'),
189 'transition_in': fields.one2many('process.transition', 'target_node_id', 'Starting Transitions'),
190 'transition_out': fields.one2many('process.transition', 'source_node_id', 'Ending Transitions'),
191 'condition_ids': fields.one2many('process.condition', 'node_id', 'Conditions')
194 'kind': lambda *args: 'state',
195 'model_states': lambda *args: False,
196 'flow_start': lambda *args: False,
200 class process_node_condition(osv.osv):
201 _name = 'process.condition'
202 _description = 'Condition'
204 'name': fields.char('Name', size=30, required=True),
205 'node_id': fields.many2one('process.node', 'Node', required=True, ondelete='cascade'),
206 'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
207 'model_states': fields.char('Expression', required=True, size=128)
209 process_node_condition()
211 class process_transition(osv.osv):
212 _name = 'process.transition'
213 _description ='Process Transitions'
215 'name': fields.char('Name', size=32, required=True, translate=True),
216 'source_node_id': fields.many2one('process.node', 'Source Node', required=True, ondelete='cascade'),
217 'target_node_id': fields.many2one('process.node', 'Target Node', required=True, ondelete='cascade'),
218 'action_ids': fields.one2many('process.transition.action', 'transition_id', 'Buttons'),
219 'transition_ids': fields.many2many('workflow.transition', 'process_transition_ids', 'ptr_id', 'wtr_id', 'Workflow Transitions'),
220 'note': fields.text('Description', translate=True),
224 class process_transition_action(osv.osv):
225 _name = 'process.transition.action'
226 _description ='Process Transitions Actions'
228 'name': fields.char('Name', size=32, required=True, translate=True),
229 'state': fields.selection([('dummy','Dummy'),
230 ('object','Object Method'),
231 ('workflow','Workflow Trigger'),
232 ('action','Action')], 'Type', required=True),
233 'action': fields.char('Action ID', size=64, states={
234 'dummy':[('readonly',1)],
235 'object':[('required',1)],
236 'workflow':[('required',1)],
237 'action':[('required',1)],
239 'transition_id': fields.many2one('process.transition', 'Transition', required=True, ondelete='cascade')
242 'state': lambda *args: 'dummy',
244 process_transition_action()