[REF] simplified init_db/load_modules:
[odoo/odoo.git] / openerp / modules / db.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #    Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import openerp.modules
24
25 def is_initialized(cr):
26     """ Check if a database has been initialized for the ORM.
27
28     The database can be initialized with the 'initialize' function below.
29
30     """
31     cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'")
32     return len(cr.fetchall()) > 0
33
34 def initialize(cr):
35     """ Initialize a database with for the ORM.
36
37     This executes base/base.sql, creates the ir_module_categories (taken
38     from each module descriptor file), and creates the ir_module_module
39     and ir_model_data entries.
40
41     """
42     f = openerp.modules.get_module_resource('base', 'base.sql')
43     base_sql_file = openerp.tools.misc.file_open(f)
44     try:
45         cr.execute(base_sql_file.read())
46         cr.commit()
47     finally:
48         base_sql_file.close()
49
50     for i in openerp.modules.get_modules():
51         mod_path = openerp.modules.get_module_path(i)
52         if not mod_path:
53             continue
54
55         # This will raise an exception if no/unreadable descriptor file.
56         info = openerp.modules.load_information_from_description_file(i)
57
58         if not info:
59             continue
60         categories = info['category'].split('/')
61         category_id = create_categories(cr, categories)
62
63         if info['installable']:
64             if info['active']:
65                 state = 'to install'
66             else:
67                 state = 'uninstalled'
68         else:
69             state = 'uninstallable'
70
71         cr.execute('INSERT INTO ir_module_module \
72                 (author, website, name, shortdesc, description, \
73                     category_id, state, certificate, web, license) \
74                 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id', (
75             info['author'],
76             info['website'], i, info['name'],
77             info['description'], category_id, state, info['certificate'],
78             info['web'],
79             info['license']))
80         id = cr.fetchone()[0]
81         cr.execute('INSERT INTO ir_model_data \
82             (name,model,module, res_id, noupdate) VALUES (%s,%s,%s,%s,%s)', (
83                 'module_meta_information', 'ir.module.module', i, id, True))
84         dependencies = info['depends']
85         for d in dependencies:
86             cr.execute('INSERT INTO ir_module_module_dependency \
87                     (module_id,name) VALUES (%s, %s)', (id, d))
88         cr.commit()
89
90 def create_categories(cr, categories):
91     """ Create the ir_module_category entries for some categories.
92
93     categories is a list of strings forming a single category with its
94     parent categories, like ['Grand Parent', 'Parent', 'Child'].
95
96     Return the database id of the (last) category.
97
98     """
99     p_id = None
100     while categories:
101         if p_id is not None:
102             cr.execute('SELECT id \
103                        FROM ir_module_category \
104                        WHERE name=%s AND parent_id=%s', (categories[0], p_id))
105         else:
106             cr.execute('SELECT id \
107                        FROM ir_module_category \
108                        WHERE name=%s AND parent_id IS NULL', (categories[0],))
109         c_id = cr.fetchone()
110         if not c_id:
111             cr.execute('INSERT INTO ir_module_category \
112                     (name, parent_id) \
113                     VALUES (%s, %s) RETURNING id', (categories[0], p_id))
114             c_id = cr.fetchone()[0]
115         else:
116             c_id = c_id[0]
117         p_id = c_id
118         categories = categories[1:]
119     return p_id
120
121 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: