[imp] implement a complete next() method and use it in action_next and action_skip...
[odoo/odoo.git] / bin / addons / base / res / res_config.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 osv, fields
23 import netsvc
24
25 class res_config_configurable(osv.osv_memory):
26     ''' Base classes for new-style configuration items
27
28     Configuration items should inherit from this class, implement
29     the execute method (and optionally the cancel one) and have
30     their view inherit from the related res_config_view_base view.
31     '''
32     _name = 'res.config'
33     logger = netsvc.Logger()
34
35     def _progress(self, cr, uid, context=None):
36         total = self.pool.get('ir.actions.todo')\
37             .search_count(cr, uid, [], context)
38         open = self.pool.get('ir.actions.todo')\
39             .search_count(cr, uid, [('active','=',True),
40                                     ('state','<>','open')],
41                           context)
42         if total:
43             return round(open*100./total)
44         return 100.
45
46     _columns = dict(
47         progress=fields.float('Configuration Progress', readonly=True),
48         )
49     _defaults = dict(
50         progress=_progress
51         )
52
53     def _next_action(self, cr, uid):
54         todos = self.pool.get('ir.actions.todo')
55         self.logger.notifyChannel('actions', netsvc.LOG_INFO,
56                                   'getting next %s' % todos)
57         active_todos = todos.search(cr, uid, [('state','=','open'),
58                                               ('active','=',True)],
59                                     limit=1, context=None)
60         if active_todos:
61             return todos.browse(cr, uid, active_todos[0], context=None)
62         return None
63
64     def _next(self, cr, uid):
65         self.logger.notifyChannel('actions', netsvc.LOG_INFO,
66                                   'getting next operation')
67         next = self._next_action(cr, uid)
68         self.logger.notifyChannel('actions', netsvc.LOG_INFO,
69                                   'next action is %s' % next)
70         if next:
71             self.pool.get('ir.actions.todo').write(cr, uid, next.id, {
72                     'state':'done',
73                     }, context=None)
74             action = next.action_id
75             return {
76                 'view_mode': action.view_mode,
77                 'view_type': action.view_type,
78                 'view_id': action.view_id and [action.view_id.id] or False,
79                 'res_model': action.res_model,
80                 'type': action.type,
81                 'target': action.target,
82                 }
83         self.logger.notifyChannel(
84             'actions', netsvc.LOG_INFO,
85             'all configuration actions have been executed')
86         return {'type': 'ir.actions.act_window_close'}
87     def next(self, cr, uid, ids, context=None):
88         return self._next(cr, uid)
89
90     def execute(self, cr, uid, ids, context=None):
91         raise NotImplementedError(
92             'Configuration items need to implement execute')
93     def cancel(self, cr, uid, ids, context=None):
94         pass
95
96     def action_next(self, cr, uid, ids, context=None):
97         next = self.execute(cr, uid, ids, context=None)
98         if next: return next
99         return self.next(cr, uid, ids, context=context)
100
101     def action_skip(self, cr, uid, ids, context=None):
102         next = self.cancel(cr, uid, ids, context=None)
103         if next: return next
104         return self.next(cr, uid, ids, context=context)
105 res_config_configurable()
106
107 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: