[IMP]Added highlight button flow for server side wizards
[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" class="oe_highlight"/>
52                                 </header>
53                                 <group>
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                                 </group>
58                              </form>'''
59
60         return res
61
62     def get_module_list(self, cr, uid, context=None):
63         mod_obj = self.pool.get('ir.module.module')
64         ids = mod_obj.search(cr, uid, [
65             ('state', 'in', ['to upgrade', 'to remove', 'to install'])])
66         return ids
67
68     def default_get(self, cr, uid, fields, context=None):
69         mod_obj = self.pool.get('ir.module.module')
70         ids = self.get_module_list(cr, uid, context=context)
71         res = mod_obj.read(cr, uid, ids, ['name','state'], context)
72         return {'module_info': '\n'.join(map(lambda x: x['name']+' : '+x['state'], res))}
73
74     def upgrade_module(self, cr, uid, ids, context=None):
75         ir_module = self.pool.get('ir.module.module')
76
77         # install/upgrade: double-check preconditions
78         ids = ir_module.search(cr, uid, [('state', 'in', ['to upgrade', 'to install'])])
79         if ids:
80             cr.execute("""SELECT d.name FROM ir_module_module m
81                                         JOIN ir_module_module_dependency d ON (m.id = d.module_id)
82                                         LEFT JOIN ir_module_module m2 ON (d.name = m2.name)
83                           WHERE m.id in %s and (m2.state IS NULL or m2.state IN %s)""",
84                       (tuple(ids), ('uninstalled',)))
85             unmet_packages = [x[0] for x in cr.fetchall()]
86             if unmet_packages:
87                 raise osv.except_osv(_('Unmet dependency !'),
88                                      _('Following modules are not installed or unknown: %s') % ('\n\n' + '\n'.join(unmet_packages)))
89
90             ir_module.download(cr, uid, ids, context=context)
91             cr.commit() # save before re-creating cursor below
92
93         pooler.restart_pool(cr.dbname, update_module=True)
94
95         ir_model_data = self.pool.get('ir.model.data')
96         __, res_id = ir_model_data.get_object_reference(cr, uid, 'base', 'view_base_module_upgrade_install')
97         return {
98                 'view_type': 'form',
99                 'view_mode': 'form',
100                 'res_model': 'base.module.upgrade',
101                 'views': [(res_id, 'form')],
102                 'view_id': False,
103                 'type': 'ir.actions.act_window',
104                 'target': 'new',
105             }
106
107     def config(self, cr, uid, ids, context=None):
108         return self.pool.get('res.config').next(cr, uid, [], context=context)
109
110
111 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: