1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from osv import fields, osv
24 from tools import graph
27 class workflow(osv.osv):
32 'name': fields.char('Name', size=64, required=True),
33 'osv': fields.char('Resource Object', size=64, required=True,select=True),
34 'on_create': fields.boolean('On Create', select=True),
35 'activities': fields.one2many('workflow.activity', 'wkf_id', 'Activities'),
38 'on_create': lambda *a: True
41 def write(self, cr, user, ids, vals, context=None):
44 wf_service = netsvc.LocalService("workflow")
45 wf_service.clear_cache(cr, user)
46 return super(workflow, self).write(cr, user, ids, vals, context=context)
49 # scale = (vertical-distance, horizontal-distance, min-node-width(optional), min-node-height(optional), margin(default=20))
53 def graph_get(self, cr, uid, id, scale, context={}):
61 workflow = self.browse(cr, uid, id, context)
62 for a in workflow.activities:
63 nodes_name.append((a.id,a.name))
68 if not a.in_transitions:
69 no_ancester.append(a.id)
71 for t in a.out_transitions:
72 transitions.append((a.id, t.act_to.id))
73 tres[t.id] = (a.id, t.act_to.id)
76 g = graph(nodes, transitions, no_ancester)
79 result = g.result_get()
82 for node in nodes_name:
83 results[str(node[0])] = result[node[0]]
84 results[str(node[0])]['name'] = node[1]
86 return {'nodes': results, 'transitions': tres}
89 def create(self, cr, user, vals, context=None):
92 wf_service = netsvc.LocalService("workflow")
93 wf_service.clear_cache(cr, user)
94 return super(workflow, self).create(cr, user, vals, context=context)
97 class wkf_activity(osv.osv):
98 _name = "workflow.activity"
99 _table = "wkf_activity"
100 # _log_access = False
102 'name': fields.char('Name', size=64, required=True),
103 'wkf_id': fields.many2one('workflow', 'Workflow', required=True, select=True, ondelete='cascade'),
104 'split_mode': fields.selection([('XOR', 'Xor'), ('OR','Or'), ('AND','And')], 'Split Mode', size=3, required=True),
105 'join_mode': fields.selection([('XOR', 'Xor'), ('AND', 'And')], 'Join Mode', size=3, required=True),
106 'kind': fields.selection([('dummy', 'Dummy'), ('function', 'Function'), ('subflow', 'Subflow'), ('stopall', 'Stop All')], 'Kind', size=64, required=True),
107 'action': fields.text('Python Action'),
108 'action_id': fields.many2one('ir.actions.server', 'Server Action', ondelete='set null'),
109 'flow_start': fields.boolean('Flow Start'),
110 'flow_stop': fields.boolean('Flow Stop'),
111 'subflow_id': fields.many2one('workflow', 'Subflow'),
112 'signal_send': fields.char('Signal (subflow.*)', size=32),
113 'out_transitions': fields.one2many('workflow.transition', 'act_from', 'Outgoing Transitions'),
114 'in_transitions': fields.one2many('workflow.transition', 'act_to', 'Incoming Transitions'),
117 'kind': lambda *a: 'dummy',
118 'join_mode': lambda *a: 'XOR',
119 'split_mode': lambda *a: 'XOR',
123 class wkf_transition(osv.osv):
124 _table = "wkf_transition"
125 _name = "workflow.transition"
126 # _log_access = False
129 'trigger_model': fields.char('Trigger Object', size=128),
130 'trigger_expr_id': fields.char('Trigger Expression', size=128),
131 'signal': fields.char('Signal (button Name)', size=64),
132 'role_id': fields.many2one('res.roles', 'Role Required'),
133 'condition': fields.char('Condition', required=True, size=128),
134 'act_from': fields.many2one('workflow.activity', 'Source Activity', required=True, select=True, ondelete='cascade'),
135 'act_to': fields.many2one('workflow.activity', 'Destination Activity', required=True, select=True, ondelete='cascade'),
138 'condition': lambda *a: 'True',
142 class wkf_instance(osv.osv):
143 _table = "wkf_instance"
144 _name = "workflow.instance"
145 _rec_name = 'res_type'
148 'wkf_id': fields.many2one('workflow', 'Workflow', ondelete='cascade', select=True),
149 'uid': fields.integer('User ID'),
150 'res_id': fields.integer('Resource ID', select=True),
151 'res_type': fields.char('Resource Object', size=64, select=True),
152 'state': fields.char('State', size=32, select=True),
154 def _auto_init(self, cr, context={}):
155 super(wkf_instance, self)._auto_init(cr, context)
156 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_id_res_type_state_index\'')
157 if not cr.fetchone():
158 cr.execute('CREATE INDEX wkf_instance_res_id_res_type_state_index ON wkf_instance (res_id, res_type, state)')
160 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_id_wkf_id_index\'')
161 if not cr.fetchone():
162 cr.execute('CREATE INDEX wkf_instance_res_id_wkf_id_index ON wkf_instance (res_id, wkf_id)')
167 class wkf_workitem(osv.osv):
168 _table = "wkf_workitem"
169 _name = "workflow.workitem"
173 'act_id': fields.many2one('workflow.activity', 'Activity', required=True, ondelete="cascade", select=True),
174 'subflow_id': fields.many2one('workflow.instance', 'Subflow', ondelete="cascade", select=True),
175 'inst_id': fields.many2one('workflow.instance', 'Instance', required=True, ondelete="cascade", select=True),
176 'state': fields.char('State', size=64, select=True),
180 class wkf_triggers(osv.osv):
181 _table = "wkf_triggers"
182 _name = "workflow.triggers"
185 'res_id': fields.integer('Resource ID', size=128),
186 'model': fields.char('Object', size=128),
187 'instance_id': fields.many2one('workflow.instance', 'Destination Instance', ondelete="cascade"),
188 'workitem_id': fields.many2one('workflow.workitem', 'Workitem', required=True, ondelete="cascade"),
190 def _auto_init(self, cr, context={}):
191 super(wkf_triggers, self)._auto_init(cr, context)
192 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_triggers_res_id_model_index\'')
193 if not cr.fetchone():
194 cr.execute('CREATE INDEX wkf_triggers_res_id_model_index ON wkf_triggers (res_id, model)')
199 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: