[IMP] module: when uninstalling a module, do not redirect to a useless screen
[odoo/odoo.git] / openerp / addons / base / module / wizard / base_module_upgrade.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (C) 2004-2012 OpenERP SA (<http://openerp.com>).
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 openerp import pooler
23 from openerp.osv import osv, fields
24 from openerp.tools.translate import _
25
26 class base_module_upgrade(osv.osv_memory):
27     """ Module Upgrade """
28
29     _name = "base.module.upgrade"
30     _description = "Module Upgrade"
31
32     _columns = {
33         'module_info': fields.text('Modules to Update',readonly=True),
34     }
35
36     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
37         res = super(base_module_upgrade, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
38         if view_type != 'form':
39             return res
40
41         context = {} if context is None else context
42         record_id = context and context.get('active_id', False) or False
43         active_model = context.get('active_model')
44         if (not record_id) or (not active_model):
45             return res
46
47         ids = self.get_module_list(cr, uid, context=context)
48         if not ids:
49             res['arch'] = '''<form string="Upgrade Completed" version="7.0">
50                                 <separator string="Upgrade Completed" colspan="4"/>
51                                 <footer>
52                                     <button name="config" string="Start Configuration" type="object" class="oe_highlight"/> or
53                                     <button special="cancel" string="Close" class="oe_link"/>
54                                 </footer>
55                              </form>'''
56
57         return res
58
59     def get_module_list(self, cr, uid, context=None):
60         mod_obj = self.pool.get('ir.module.module')
61         ids = mod_obj.search(cr, uid, [
62             ('state', 'in', ['to upgrade', 'to remove', 'to install'])])
63         return ids
64
65     def default_get(self, cr, uid, fields, context=None):
66         mod_obj = self.pool.get('ir.module.module')
67         ids = self.get_module_list(cr, uid, context=context)
68         res = mod_obj.read(cr, uid, ids, ['name','state'], context)
69         return {'module_info': '\n'.join(map(lambda x: x['name']+' : '+x['state'], res))}
70
71     def upgrade_module_cancel(self, cr, uid, ids, context=None):
72         mod_obj = self.pool.get('ir.module.module')
73         to_installed_ids = mod_obj.search(cr, uid, [
74             ('state', 'in', ['to upgrade', 'to remove'])])
75         if to_installed_ids:
76             mod_obj.write(cr, uid, to_installed_ids, {'state': 'installed'}, context=context)
77
78         to_uninstalled_ids = mod_obj.search(cr, uid, [
79             ('state', '=', 'to install')])
80         if to_uninstalled_ids:
81             mod_obj.write(cr, uid, to_uninstalled_ids, {'state': 'uninstalled'}, context=context)
82
83         return {'type': 'ir.actions.act_window_close'}
84
85     def upgrade_module(self, cr, uid, ids, context=None):
86         ir_module = self.pool.get('ir.module.module')
87
88         # install/upgrade: double-check preconditions
89         ids = ir_module.search(cr, uid, [('state', 'in', ['to upgrade', 'to install'])])
90         if ids:
91             cr.execute("""SELECT d.name FROM ir_module_module m
92                                         JOIN ir_module_module_dependency d ON (m.id = d.module_id)
93                                         LEFT JOIN ir_module_module m2 ON (d.name = m2.name)
94                           WHERE m.id in %s and (m2.state IS NULL or m2.state IN %s)""",
95                       (tuple(ids), ('uninstalled',)))
96             unmet_packages = [x[0] for x in cr.fetchall()]
97             if unmet_packages:
98                 raise osv.except_osv(_('Unmet Dependency!'),
99                                      _('Following modules are not installed or unknown: %s') % ('\n\n' + '\n'.join(unmet_packages)))
100
101             ir_module.download(cr, uid, ids, context=context)
102             cr.commit() # save before re-creating cursor below
103
104         pooler.restart_pool(cr.dbname, update_module=True)
105
106         return {'type': 'ir.actions.act_window_close'}
107
108     def config(self, cr, uid, ids, context=None):
109         return self.pool.get('res.config').next(cr, uid, [], context=context)
110
111
112 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: