[FIX] Fixed error of non-ascii charcter in label, remove by default condition True...
[odoo/odoo.git] / openerp / addons / base / ir / workflow / workflow.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 from tools import graph
24 import netsvc
25
26 class workflow(osv.osv):
27     _name = "workflow"
28     _table = "wkf"
29     _order = "name"
30   #  _log_access = False
31     _columns = {
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'),
36     }
37     _defaults = {
38         'on_create': lambda *a: True
39     }
40
41     def write(self, cr, user, ids, vals, context=None):
42         if not context:
43             context={}
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)
47
48     def get_active_workitems(self, cr, uid, res, res_id, context={}):
49
50         cr.execute('select * from wkf where osv=%s limit 1',(res,))
51         wkfinfo = cr.dictfetchone()
52         workitems = []
53
54         if wkfinfo:
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()
60
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())
63
64         return {'wkf': wkfinfo, 'workitems':  workitems}
65
66
67     #
68     # scale =  (vertical-distance, horizontal-distance, min-node-width(optional), min-node-height(optional), margin(default=20))
69     #
70
71
72 #    def graph_get(self, cr, uid, id, scale, context={}):
73 #
74 #        nodes= []
75 #        nodes_name = []
76 #        transitions = []
77 #        start = []
78 #        tres = {}
79 #        no_ancester = []
80 #        workflow = self.browse(cr, uid, id, context)
81 #        for a in workflow.activities:
82 #            nodes_name.append((a.id,a.name))
83 #            nodes.append(a.id)
84 #            if a.flow_start:
85 #                start.append(a.id)
86 #            else:
87 #                if not a.in_transitions:
88 #                    no_ancester.append(a.id)
89 #
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)
93 #
94 #
95 #        g  = graph(nodes, transitions, no_ancester)
96 #        g.process(start)
97 #        g.scale(*scale)
98 #        result = g.result_get()
99 #        results = {}
100 #
101 #        for node in nodes_name:
102 #            results[str(node[0])] = result[node[0]]
103 #            results[str(node[0])]['name'] = node[1]
104 #
105 #        return {'nodes': results, 'transitions': tres}
106
107
108     def create(self, cr, user, vals, context=None):
109         if not context:
110             context={}
111         wf_service = netsvc.LocalService("workflow")
112         wf_service.clear_cache(cr, user)
113         return super(workflow, self).create(cr, user, vals, context=context)
114 workflow()
115
116 class wkf_activity(osv.osv):
117     _name = "workflow.activity"
118     _table = "wkf_activity"
119     _order = "name"
120    # _log_access = False
121     _columns = {
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'),
135     }
136     _defaults = {
137         'kind': lambda *a: 'dummy',
138         'join_mode': lambda *a: 'XOR',
139         'split_mode': lambda *a: 'XOR',
140     }
141 wkf_activity()
142
143 class wkf_transition(osv.osv):
144     _table = "wkf_transition"
145     _name = "workflow.transition"
146    # _log_access = False
147     _rec_name = 'signal'
148     _columns = {
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),
163     }
164
165 wkf_transition()
166
167 class wkf_instance(osv.osv):
168     _table = "wkf_instance"
169     _name = "workflow.instance"
170     _rec_name = 'res_type'
171     _log_access = False
172     _columns = {
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),
177     }
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)')
186
187 wkf_instance()
188
189 class wkf_workitem(osv.osv):
190     _table = "wkf_workitem"
191     _name = "workflow.workitem"
192     _log_access = False
193     _rec_name = 'state'
194     _columns = {
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),
200     }
201 wkf_workitem()
202
203 class wkf_triggers(osv.osv):
204     _table = "wkf_triggers"
205     _name = "workflow.triggers"
206     _log_access = False
207     _columns = {
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"),
212     }
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)')
218 wkf_triggers()
219
220
221 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
222