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 ##############################################################################
22 from osv import fields, osv
23 from tools import graph
26 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)
48 def get_active_workitems(self, cr, uid, res, res_id, context={}):
50 cr.execute('select * from wkf where osv=%s limit 1',(res,))
51 wkfinfo = cr.dictfetchone()
55 cr.execute('SELECT id FROM wkf_instance \
56 WHERE res_id=%s AND wkf_id=%s \
57 ORDER BY state LIMIT 1',
58 (res_id, wkfinfo['id']))
59 inst_id = cr.fetchone()
61 cr.execute('select act_id,count(*) from wkf_workitem where inst_id=%s group by act_id', (inst_id,))
62 workitems = dict(cr.fetchall())
64 return {'wkf': wkfinfo, 'workitems': workitems}
68 # scale = (vertical-distance, horizontal-distance, min-node-width(optional), min-node-height(optional), margin(default=20))
72 # def graph_get(self, cr, uid, id, scale, context={}):
80 # workflow = self.browse(cr, uid, id, context)
81 # for a in workflow.activities:
82 # nodes_name.append((a.id,a.name))
87 # if not a.in_transitions:
88 # no_ancester.append(a.id)
90 # for t in a.out_transitions:
91 # transitions.append((a.id, t.act_to.id))
92 # tres[t.id] = (a.id, t.act_to.id)
95 # g = graph(nodes, transitions, no_ancester)
98 # result = g.result_get()
101 # for node in nodes_name:
102 # results[str(node[0])] = result[node[0]]
103 # results[str(node[0])]['name'] = node[1]
105 # return {'nodes': results, 'transitions': tres}
108 def create(self, cr, user, vals, context=None):
111 wf_service = netsvc.LocalService("workflow")
112 wf_service.clear_cache(cr, user)
113 return super(workflow, self).create(cr, user, vals, context=context)
116 class wkf_activity(osv.osv):
117 _name = "workflow.activity"
118 _table = "wkf_activity"
120 # _log_access = False
122 'name': fields.char('Name', size=64, required=True),
123 'wkf_id': fields.many2one('workflow', 'Workflow', required=True, select=True, ondelete='cascade'),
124 'split_mode': fields.selection([('XOR', 'Xor'), ('OR','Or'), ('AND','And')], 'Split Mode', size=3, required=True),
125 'join_mode': fields.selection([('XOR', 'Xor'), ('AND', 'And')], 'Join Mode', size=3, required=True),
126 'kind': fields.selection([('dummy', 'Dummy'), ('function', 'Function'), ('subflow', 'Subflow'), ('stopall', 'Stop All')], 'Kind', size=64, required=True),
127 'action': fields.text('Python Action'),
128 'action_id': fields.many2one('ir.actions.server', 'Server Action', ondelete='set null'),
129 'flow_start': fields.boolean('Flow Start'),
130 'flow_stop': fields.boolean('Flow Stop'),
131 'subflow_id': fields.many2one('workflow', 'Subflow'),
132 'signal_send': fields.char('Signal (subflow.*)', size=32),
133 'out_transitions': fields.one2many('workflow.transition', 'act_from', 'Outgoing Transitions'),
134 'in_transitions': fields.one2many('workflow.transition', 'act_to', 'Incoming Transitions'),
137 'kind': lambda *a: 'dummy',
138 'join_mode': lambda *a: 'XOR',
139 'split_mode': lambda *a: 'XOR',
143 class wkf_transition(osv.osv):
144 _table = "wkf_transition"
145 _name = "workflow.transition"
146 # _log_access = False
149 'trigger_model': fields.char('Trigger Object', size=128),
150 'trigger_expr_id': fields.char('Trigger Expression', size=128),
151 'signal': fields.char('Signal (button Name)', size=64,
152 help="When the operation of transition comes from a button pressed in the client form, "\
153 "signal tests the name of the pressed button. If signal is NULL, no button is necessary to validate this transition."),
154 'group_id': fields.many2one('res.groups', 'Group Required',
155 help="The group that a user must have to be authorized to validate this transition."),
156 'condition': fields.char('Condition', size=128,
157 help="Expression to be satisfied if we want the transition done."),
158 'act_from': fields.many2one('workflow.activity', 'Source Activity', required=True, select=True, ondelete='cascade',
159 help="Source activity. When this activity is over, the condition is tested to determine if we can start the ACT_TO activity."),
160 'act_to': fields.many2one('workflow.activity', 'Destination Activity', required=True, select=True, ondelete='cascade',
161 help="The destination activity."),
162 'wkf_id': fields.related('act_from','wkf_id', type='many2one', relation='workflow', string='Workflow', select=True),
167 class wkf_instance(osv.osv):
168 _table = "wkf_instance"
169 _name = "workflow.instance"
170 _rec_name = 'res_type'
173 'wkf_id': fields.many2one('workflow', 'Workflow', ondelete='cascade', select=True),
174 'res_id': fields.integer('Resource ID'),
175 'res_type': fields.char('Resource Object', size=64),
176 'state': fields.char('State', size=32),
178 def _auto_init(self, cr, context=None):
179 super(wkf_instance, self)._auto_init(cr, context)
180 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_type_res_id_state_index\'')
181 if not cr.fetchone():
182 cr.execute('CREATE INDEX wkf_instance_res_type_res_id_state_index ON wkf_instance (res_type, res_id, state)')
183 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_id_wkf_id_index\'')
184 if not cr.fetchone():
185 cr.execute('CREATE INDEX wkf_instance_res_id_wkf_id_index ON wkf_instance (res_id, wkf_id)')
189 class wkf_workitem(osv.osv):
190 _table = "wkf_workitem"
191 _name = "workflow.workitem"
195 'act_id': fields.many2one('workflow.activity', 'Activity', required=True, ondelete="restrict", select=True),
196 'wkf_id': fields.related('act_id','wkf_id', type='many2one', relation='workflow', string='Workflow'),
197 'subflow_id': fields.many2one('workflow.instance', 'Subflow', ondelete="cascade", select=True),
198 'inst_id': fields.many2one('workflow.instance', 'Instance', required=True, ondelete="cascade", select=True),
199 'state': fields.char('State', size=64, select=True),
203 class wkf_triggers(osv.osv):
204 _table = "wkf_triggers"
205 _name = "workflow.triggers"
208 'res_id': fields.integer('Resource ID', size=128),
209 'model': fields.char('Object', size=128),
210 'instance_id': fields.many2one('workflow.instance', 'Destination Instance', ondelete="cascade"),
211 'workitem_id': fields.many2one('workflow.workitem', 'Workitem', required=True, ondelete="cascade"),
213 def _auto_init(self, cr, context={}):
214 super(wkf_triggers, self)._auto_init(cr, context)
215 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_triggers_res_id_model_index\'')
216 if not cr.fetchone():
217 cr.execute('CREATE INDEX wkf_triggers_res_id_model_index ON wkf_triggers (res_id, model)')
221 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: