[IMP] use the openerp namespace.
[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(self, cr, uid, ids, context=None):
72         ir_module = self.pool.get('ir.module.module')
73
74         # install/upgrade: double-check preconditions
75         ids = ir_module.search(cr, uid, [('state', 'in', ['to upgrade', 'to install'])])
76         if ids:
77             cr.execute("""SELECT d.name FROM ir_module_module m
78                                         JOIN ir_module_module_dependency d ON (m.id = d.module_id)
79                                         LEFT JOIN ir_module_module m2 ON (d.name = m2.name)
80                           WHERE m.id in %s and (m2.state IS NULL or m2.state IN %s)""",
81                       (tuple(ids), ('uninstalled',)))
82             unmet_packages = [x[0] for x in cr.fetchall()]
83             if unmet_packages:
84                 raise osv.except_osv(_('Unmet dependency !'),
85                                      _('Following modules are not installed or unknown: %s') % ('\n\n' + '\n'.join(unmet_packages)))
86
87             ir_module.download(cr, uid, ids, context=context)
88             cr.commit() # save before re-creating cursor below
89
90         pooler.restart_pool(cr.dbname, update_module=True)
91
92         ir_model_data = self.pool.get('ir.model.data')
93         __, res_id = ir_model_data.get_object_reference(cr, uid, 'base', 'view_base_module_upgrade_install')
94         return {
95                 'view_type': 'form',
96                 'view_mode': 'form',
97                 'res_model': 'base.module.upgrade',
98                 'views': [(res_id, 'form')],
99                 'view_id': False,
100                 'type': 'ir.actions.act_window',
101                 'target': 'new',
102             }
103
104     def config(self, cr, uid, ids, context=None):
105         return self.pool.get('res.config').next(cr, uid, [], context=context)
106
107
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: