diff --git a/openerp/addons/base/module/module.py b/openerp/addons/base/module/module.py index 42b57b7c661..dbb8d6a3f40 100644 --- a/openerp/addons/base/module/module.py +++ b/openerp/addons/base/module/module.py @@ -31,6 +31,7 @@ import urllib import zipimport from openerp import modules, pooler, release, tools, addons +from openerp.modules.db import create_categories from openerp.tools.parse_version import parse_version from openerp.tools.translate import _ from openerp.osv import fields, osv, orm @@ -638,21 +639,8 @@ class module(osv.osv): categs = category.split('/') if categs != current_category_path: - p_id = None - while categs: - if p_id is not None: - cr.execute('SELECT id FROM ir_module_category WHERE name=%s AND parent_id=%s', (categs[0], p_id)) - else: - cr.execute('SELECT id FROM ir_module_category WHERE name=%s AND parent_id is NULL', (categs[0],)) - c_id = cr.fetchone() - if not c_id: - cr.execute('INSERT INTO ir_module_category (name, parent_id) VALUES (%s, %s) RETURNING id', (categs[0], p_id)) - c_id = cr.fetchone()[0] - else: - c_id = c_id[0] - p_id = c_id - categs = categs[1:] - self.write(cr, uid, [mod_browse.id], {'category_id': p_id}) + cat_id = create_categories(cr, categs) + mod_browse.write({'category_id': cat_id}) def update_translations(self, cr, uid, ids, filter_lang=None, context=None): if not filter_lang: diff --git a/openerp/modules/db.py b/openerp/modules/db.py index 6318bd954f3..f13708a8a9b 100644 --- a/openerp/modules/db.py +++ b/openerp/modules/db.py @@ -3,7 +3,7 @@ # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2009 Tiny SPRL (). -# Copyright (C) 2010 OpenERP s.a. (). +# Copyright (C) 2010-2012 OpenERP s.a. (). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -119,21 +119,17 @@ def create_categories(cr, categories): category = [] while categories: category.append(categories[0]) - if p_id is not None: - cr.execute('SELECT id \ - FROM ir_module_category \ - WHERE name=%s AND parent_id=%s', (categories[0], p_id)) - else: - cr.execute('SELECT id \ - FROM ir_module_category \ - WHERE name=%s AND parent_id IS NULL', (categories[0],)) + xml_id = 'module_category_' + ('_'.join(map(lambda x: x.lower(), category))).replace('&', 'and').replace(' ', '_') + # search via xml_id (because some categories are renamed) + cr.execute("SELECT res_id FROM ir_model_data WHERE name=%s AND module=%s AND model=%s", + (xml_id, "base", "ir.module.category")) + c_id = cr.fetchone() if not c_id: cr.execute('INSERT INTO ir_module_category \ (name, parent_id) \ VALUES (%s, %s) RETURNING id', (categories[0], p_id)) c_id = cr.fetchone()[0] - xml_id = 'module_category_' + ('_'.join(map(lambda x: x.lower(), category))).replace('&', 'and').replace(' ', '_') cr.execute('INSERT INTO ir_model_data (module, name, res_id, model) \ VALUES (%s, %s, %s, %s)', ('base', xml_id, c_id, 'ir.module.category')) else: