1 # -*- coding: utf-8 -*-
2 ##############################################################################
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>).
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.
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.
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/>.
21 ##############################################################################
23 import openerp.modules
26 """ Initialize a database with for the ORM.
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.
33 f = openerp.modules.get_module_resource('base', 'base.sql')
34 base_sql_file = openerp.tools.misc.file_open(f)
36 cr.execute(base_sql_file.read())
41 for i in openerp.modules.get_modules():
42 mod_path = openerp.modules.get_module_path(i)
46 # This will raise an exception if no/unreadable descriptor file.
47 info = openerp.modules.load_information_from_description_file(i)
51 categories = info['category'].split('/')
52 category_id = create_categories(cr, categories)
54 if info['installable']:
60 state = 'uninstallable'
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', (
67 info['website'], i, info['name'],
68 info['description'], category_id, state, info['certificate'],
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))
81 def create_categories(cr, categories):
82 """ Create the ir_module_category entries for some categories.
84 categories is a list of strings forming a single category with its
85 parent categories, like ['Grand Parent', 'Parent', 'Child'].
87 Return the database id of the (last) category.
93 cr.execute('SELECT id \
94 FROM ir_module_category \
95 WHERE name=%s AND parent_id=%s', (categories[0], p_id))
97 cr.execute('SELECT id \
98 FROM ir_module_category \
99 WHERE name=%s AND parent_id IS NULL', (categories[0],))
102 cr.execute('INSERT INTO ir_module_category \
104 VALUES (%s, %s) RETURNING id', (categories[0], p_id))
105 c_id = cr.fetchone()[0]
109 categories = categories[1:]
112 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: