261b39ffb318d9e36caa521aada65d23d004b52e
[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 initialize(cr):
26     """ Initialize a database with for the ORM.
27
28     This executes base/base.sql, creates the ir_module_categories (taken
29     from each module descriptor file), and creates the ir_module_module
30     and ir_model_data entries.
31
32     """
33     f = openerp.modules.get_module_resource('base', 'base.sql')
34     base_sql_file = openerp.tools.misc.file_open(f)
35     try:
36         cr.execute(base_sql_file.read())
37         cr.commit()
38     finally:
39         base_sql_file.close()
40
41     for i in openerp.modules.get_modules():
42         mod_path = openerp.modules.get_module_path(i)
43         if not mod_path:
44             continue
45
46         # This will raise an exception if no/unreadable descriptor file.
47         info = openerp.modules.load_information_from_description_file(i)
48
49         if not info:
50             continue
51         categories = info['category'].split('/')
52         category_id = create_categories(cr, categories)
53
54         if info['installable']:
55             if info['active']:
56                 state = 'to install'
57             else:
58                 state = 'uninstalled'
59         else:
60             state = 'uninstallable'
61
62         cr.execute('INSERT INTO ir_module_module \
63                 (author, website, name, shortdesc, description, \
64                     category_id, state, certificate, web, license) \
65                 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id', (
66             info['author'],
67             info['website'], i, info['name'],
68             info['description'], category_id, state, info['certificate'],
69             info['web'],
70             info['license']))
71         id = cr.fetchone()[0]
72         cr.execute('INSERT INTO ir_model_data \
73             (name,model,module, res_id, noupdate) VALUES (%s,%s,%s,%s,%s)', (
74                 'module_meta_information', 'ir.module.module', i, id, True))
75         dependencies = info['depends']
76         for d in dependencies:
77             cr.execute('INSERT INTO ir_module_module_dependency \
78                     (module_id,name) VALUES (%s, %s)', (id, d))
79         cr.commit()
80
81 def create_categories(cr, categories):
82     """ Create the ir_module_category entries for some categories.
83
84     categories is a list of strings forming a single category with its
85     parent categories, like ['Grand Parent', 'Parent', 'Child'].
86
87     Return the database id of the (last) category.
88
89     """
90     p_id = None
91     while categories:
92         if p_id is not None:
93             cr.execute('SELECT id \
94                        FROM ir_module_category \
95                        WHERE name=%s AND parent_id=%s', (categories[0], p_id))
96         else:
97             cr.execute('SELECT id \
98                        FROM ir_module_category \
99                        WHERE name=%s AND parent_id IS NULL', (categories[0],))
100         c_id = cr.fetchone()
101         if not c_id:
102             cr.execute('INSERT INTO ir_module_category \
103                     (name, parent_id) \
104                     VALUES (%s, %s) RETURNING id', (categories[0], p_id))
105             c_id = cr.fetchone()[0]
106         else:
107             c_id = c_id[0]
108         p_id = c_id
109         categories = categories[1:]
110     return p_id
111
112 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: