[FIX] res.partner.category: the relation table name was manually chosen, which breaks...
[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="Apply Scheduled Upgrades" version="7.0">
50                                 <header>
51                                     <button name="config" string="Start configuration" type="object" icon="gtk-ok"/>
52                                     <button special="cancel" string="Close" icon="gtk-cancel"/>
53                                 </header>
54                                 <separator string="System update completed" colspan="4"/>
55                                 <label align="0.0" string="The selected modules have been updated / installed !" colspan="4"/>
56                                 <label align="0.0" string="We suggest to reload the menu tab to see the new menus (Ctrl+T then Ctrl+R)." colspan="4"/>
57                              </form>'''
58
59         return res
60
61     def get_module_list(self, cr, uid, context=None):
62         mod_obj = self.pool.get('ir.module.module')
63         ids = mod_obj.search(cr, uid, [
64             ('state', 'in', ['to upgrade', 'to remove', 'to install'])])
65         return ids
66
67     def default_get(self, cr, uid, fields, context=None):
68         mod_obj = self.pool.get('ir.module.module')
69         ids = self.get_module_list(cr, uid, context=context)
70         res = mod_obj.read(cr, uid, ids, ['name','state'], context)
71         return {'module_info': '\n'.join(map(lambda x: x['name']+' : '+x['state'], res))}
72
73     def upgrade_module(self, cr, uid, ids, context=None):
74         ir_module = self.pool.get('ir.module.module')
75
76         # install/upgrade: double-check preconditions
77         ids = ir_module.search(cr, uid, [('state', 'in', ['to upgrade', 'to install'])])
78         if ids:
79             cr.execute("""SELECT d.name FROM ir_module_module m
80                                         JOIN ir_module_module_dependency d ON (m.id = d.module_id)
81                                         LEFT JOIN ir_module_module m2 ON (d.name = m2.name)
82                           WHERE m.id in %s and (m2.state IS NULL or m2.state IN %s)""",
83                       (tuple(ids), ('uninstalled',))) 
84             unmet_packages = [x[0] for x in cr.fetchall()]
85             if unmet_packages:
86                 raise osv.except_osv(_('Unmet dependency !'),
87                                      _('Following modules are not installed or unknown: %s') % ('\n\n' + '\n'.join(unmet_packages)))
88
89             ir_module.download(cr, uid, ids, context=context)
90             cr.commit() # save before re-creating cursor below 
91
92         pooler.restart_pool(cr.dbname, update_module=True)
93
94         ir_model_data = self.pool.get('ir.model.data')
95         __, res_id = ir_model_data.get_object_reference(cr, uid, 'base', 'view_base_module_upgrade_install')
96         return {
97                 'view_type': 'form',
98                 'view_mode': 'form',
99                 'res_model': 'base.module.upgrade',
100                 'views': [(res_id, 'form')],
101                 'view_id': False,
102                 'type': 'ir.actions.act_window',
103                 'target': 'new',
104             }
105
106     def config(self, cr, uid, ids, context=None):
107         return self.pool.get('res.config').next(cr, uid, [], context=context)
108
109
110 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: