~openupgrade-committers/openupgrade-server/5.0

« back to all changes in this revision

Viewing changes to bin/addons/openupgrade_records/model/generate_records_wizard.py

  • Committer: Stefan Rijnhart
  • Date: 2012-05-27 12:24:43 UTC
  • mfrom: (2175.1.3 openupgrade-server)
  • Revision ID: stefan@therp.nl-20120527122443-7lh1cvrm26wt8u6e
[MRG] Add openupgrade development module for easy analysis file generation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    This module Copyright (C) 2012 OpenUpgrade community
 
6
#    https://launchpad.net/~openupgrade-committers
 
7
#
 
8
#    Contributors:
 
9
#    Therp BV <http://therp.nl>
 
10
#
 
11
#    This program is free software: you can redistribute it and/or modify
 
12
#    it under the terms of the GNU Affero General Public License as
 
13
#    published by the Free Software Foundation, either version 3 of the
 
14
#    License, or (at your option) any later version.
 
15
#
 
16
#    This program is distributed in the hope that it will be useful,
 
17
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
#    GNU Affero General Public License for more details.
 
20
#
 
21
#    You should have received a copy of the GNU Affero General Public License
 
22
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
23
#
 
24
##############################################################################
 
25
 
 
26
import os
 
27
from osv import osv, fields
 
28
import pooler
 
29
try:
 
30
    from openerp.openupgrade import openupgrade_tools
 
31
except ImportError:
 
32
    from openupgrade import openupgrade_tools
 
33
 
 
34
class generate_records_wizard(osv.osv_memory):
 
35
    _name = 'openupgrade.generate.records.wizard'
 
36
    _description = 'OpenUpgrade Generate Records Wizard'
 
37
    _columns = {
 
38
        'state': fields.selection([('init', 'init'), ('ready', 'ready')], 'State'),
 
39
        }
 
40
    _defaults = {
 
41
        'state': lambda *a: 'init',
 
42
        }
 
43
 
 
44
    def generate(self, cr, uid, ids, context=None):
 
45
        """
 
46
        Main wizard step. Make sure that all modules are up-to-date,
 
47
        then reinitialize all installed modules.
 
48
        Equivalent of running the server with '-d <database> --init all'
 
49
 
 
50
        The goal of this is to fill the records table.
 
51
 
 
52
        TODO: update module list and versions, then update all modules?
 
53
        """
 
54
        # Truncate the records table
 
55
        if (openupgrade_tools.table_exists(cr, 'openupgrade_attribute') and
 
56
            openupgrade_tools.table_exists(cr, 'openupgrade_record')):
 
57
            cr.execute(
 
58
                'TRUNCATE openupgrade_attribute, openupgrade_record;'
 
59
                )
 
60
 
 
61
        # Need to get all modules in state 'installed'
 
62
        module_obj = self.pool.get('ir.module.module')
 
63
        module_ids = module_obj.search(
 
64
            cr, uid, [('state', 'in', ['to install', 'to upgrade'])])
 
65
        if module_ids:
 
66
            cr.commit()
 
67
            _db, pool = pooler.restart_pool(cr.dbname, update_module=True)
 
68
        # Did we succeed above?
 
69
        module_ids = module_obj.search(
 
70
            cr, uid, [('state', 'in', ['to install', 'to upgrade'])])
 
71
        if module_ids:
 
72
            modules = module_obj.read(
 
73
                cr, uid, module_ids, ['name'], context=context)
 
74
            raise except_osv(
 
75
                "Cannot reliably generate records", 
 
76
                ("Cannot seem to install or upgrade modules " +
 
77
                 ', '.join([x['name'] for x in modules])))
 
78
        # Now reinitialize all installed modules
 
79
        module_ids = module_obj.search(
 
80
            cr, uid, [('state', '=', 'installed')])
 
81
        module_obj.write(
 
82
            cr, uid, module_ids, {'state': 'to install'})
 
83
        cr.commit()
 
84
        _db, pool = pooler.restart_pool(cr.dbname, update_module=True)
 
85
        self.write(cr, uid, ids, {'state': 'ready'})
 
86
        # and we are done
 
87
        return True
 
88
 
 
89
generate_records_wizard()
 
90