Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

代码阅读db.py



  • -- coding: utf-8 --<br />##############################################################################<br />#<br />#    OpenERP, Open Source Management Solution<br />#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).<br />#    Copyright (C) 2010-2012 OpenERP s.a. (<http://openerp.com>).<br />#<br />#    This program is free software: you can redistribute it and/or modify<br />#    it under the terms of the GNU Affero General Public License as<br />#    published by the Free Software Foundation, either version 3 of the<br />#    License, or (at your option) any later version.<br />#<br />#    This program is distributed in the hope that it will be useful,<br />#    but WITHOUT ANY WARRANTY; without even the implied warranty of<br />#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br />#    GNU Affero General Public License for more details.<br />#<br />#    You should have received a copy of the GNU Affero General Public License<br />#    along with this program.  If not, see <http://www.gnu.org/licenses/>.<br />#<br />##############################################################################<br />import openerp.modules<br />import logging<br />logger = logging.getLogger(name)<br />def is_initialized(cr):<br />    """ Check if a database has been initialized for the ORM.<br />    The database can be initialized with the 'initialize' function below.<br />    """<br />    [color=red]'''<br />    pg_class是postgresql记载表和几乎所有有字段或者是那些类似表的东西,OE里所有的对象都存储在ir_module_module,我们利用它来判断<br />    某个数据库是否已初始化<br />    '''[/color]<br />    cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'")<br />    return len(cr.fetchall()) > 0<br /><br />[color=red]'''<br />初始化数据库<br />'''[/color]<br />def initialize(cr):<br />    """ Initialize a database with for the ORM.<br />    This executes base/base.sql, creates the ir_module_categories (taken<br />    from each module descriptor file), and creates the ir_module_module<br />    and ir_model_data entries.<br />    """<br />    f = openerp.modules.get_module_resource('base', 'base.sql')  [color=red]//获取base模块下的base.sql文件[/color]<br />    if not f:<br />        m = "File not found: 'base.sql' (provided by module 'base')."<br />        logger.critical(m)<br />        raise IOError(m)<br />    base_sql_file = openerp.tools.misc.file_open(f)    [color=red]//打开获得的文件[/color]<br />    try:<br />        cr.execute(base_sql_file.read())<br />        cr.commit()<br />    finally:<br />        base_sql_file.close()<br />    for i in openerp.modules.get_modules():<br />        mod_path = openerp.modules.get_module_path(i)<br />        if not mod_path:<br />            continue<br />        # This will raise an exception if no/unreadable descriptor file.<br />        info = openerp.modules.load_information_from_description_file(i)    [color=red]//获取模块下的_openerp.py文件[/color]<br />        if not info:<br />            continue<br />        categories = info['category'].split('/')<br />        category_id = create_categories(cr, categories)    [color=red]//创建目录[/color]<br />        if info['installable']:    [color=red]//如果installable为true的话,设置state为uninstalled[/color]<br />            state = 'uninstalled'<br />        else:<br />            state = 'uninstallable'<br />        cr.execute('INSERT INTO ir_module_module <br />                (author, website, name, shortdesc, description, <br />                    category_id, auto_install, state, web, license, application, icon, sequence, summary) <br />                VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id', (<br />            info['author'],<br />            info['website'], i, info['name'],<br />            info['description'], category_id,<br />            info['auto_install'], state,<br />            info['web'],<br />            info['license'],<br />            info['application'], info['icon'],<br />            info['sequence'], info['summary']))    [color=red]//将所有对象的_openerp.py文件中的信息存入到ir_module_module中[/color]<br />        id = cr.fetchone()[0]<br />        cr.execute('INSERT INTO ir_model_data <br />            (name,model,module, res_id, noupdate) VALUES (%s,%s,%s,%s,%s)', (<br />                'module_'+i, 'ir.module.module', 'base', id, True))<br />        dependencies = info['depends']<br />        for d in dependencies:<br />            cr.execute('INSERT INTO ir_module_module_dependency <br />                    (module_id,name) VALUES (%s, %s)', (id, d))<br />    # Install recursively all auto-installing modules<br />    [color=red]'''<br />    递归的安装_openerp_.py文件中所有auto_install为true的模块,这也是为什么刚初始化的数据库会安装了一些模块的原因,因为其<br />    auto_install被设置了true。我们在新建模块的时候一般设成false,不让其初始化数据库时就安装<br />    '''[/color]<br />    while True:<br />        cr.execute("""SELECT m.name FROM ir_module_module m WHERE m.auto_install AND state != 'to install'<br />                      AND NOT EXISTS (<br />                          SELECT 1 FROM ir_module_module_dependency d JOIN ir_module_module mdep ON (d.name = mdep.name)<br />                                  WHERE d.module_id = m.id AND mdep.state != 'to install'<br />                      )""")<br />        to_auto_install = [x[0] for x in cr.fetchall()]<br />        if not to_auto_install: break<br />        cr.execute("""UPDATE ir_module_module SET state='to install' WHERE name in %s""", (tuple(to_auto_install),))<br />    cr.commit()<br /><br />[color=red]'''<br />创建目录,目录用于对模块,组等等进行分门别类,信息来自_openerp_.py中的category项<br />'''[/color]<br />def create_categories(cr, categories):<br />    """ Create the ir_module_category entries for some categories.<br />    categories is a list of strings forming a single category with its<br />    parent categories, like ['Grand Parent', 'Parent', 'Child'].<br />    Return the database id of the (last) category.<br />    """<br />    p_id = None<br />    category = []<br />    while categories:<br />        category.append(categories[0])<br />        [color=red]//对于传进来的目录名进行拼接,组成module_category_xxxxxx,其中xxxxxx就是_openerp_.py里的目录名[/color]<br />        xml_id = 'module_category_' + (''.join(map(lambda x: x.lower(), category))).replace('&', 'and').replace(' ', '')<br />        # search via xml_id (because some categories are renamed)<br />        cr.execute("SELECT res_id FROM ir_model_data WHERE name=%s AND module=%s AND model=%s",<br />                  (xml_id, "base", "ir.module.category"))<br />        c_id = cr.fetchone()<br />        if not c_id:<br />            [color=red]//如果ir_module_data中没有相关目录信息,则新建立,并插入新的数据到ir_module_data中[/color]<br />            cr.execute('INSERT INTO ir_module_category <br />                    (name, parent_id) <br />                    VALUES (%s, %s) RETURNING id', (categories[0], p_id))<br />            c_id = cr.fetchone()[0]<br />            cr.execute('INSERT INTO ir_model_data (module, name, res_id, model) <br />                      VALUES (%s, %s, %s, %s)', ('base', xml_id, c_id, 'ir.module.category'))<br />        else:<br />            c_id = c_id[0]<br />        p_id = c_id<br />        categories = categories[1:]<br />    return p_id<br /><br />[color=red]'''<br />检测数据库是否有unaccent函数,unaccent函数是用来移除给定字符串上的读音符号<br />'''[/color]<br />def has_unaccent(cr):<br />    """ Test if the database has an unaccent function.<br />    The unaccent is supposed to be provided by the PostgreSQL unaccent contrib<br />    module but any similar function will be picked by OpenERP.<br />    """<br />    cr.execute("SELECT proname FROM pg_proc WHERE proname='unaccent'")<br />    return len(cr.fetchall()) > 0<br /># vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:



  • -- coding: utf-8 --<br />##############################################################################<br />#<br />#    OpenERP, Open Source Management Solution<br />#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).<br />#    Copyright (C) 2010-2012 OpenERP s.a. (<http://openerp.com>).<br />#<br />#    This program is free software: you can redistribute it and/or modify<br />#    it under the terms of the GNU Affero General Public License as<br />#    published by the Free Software Foundation, either version 3 of the<br />#    License, or (at your option) any later version.<br />#<br />#    This program is distributed in the hope that it will be useful,<br />#    but WITHOUT ANY WARRANTY; without even the implied warranty of<br />#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the<br />#    GNU Affero General Public License for more details.<br />#<br />#    You should have received a copy of the GNU Affero General Public License<br />#    along with this program.  If not, see <http://www.gnu.org/licenses/>.<br />#<br />##############################################################################<br />import openerp.modules<br />import logging<br />logger = logging.getLogger(name)<br />def is_initialized(cr):<br />    """ Check if a database has been initialized for the ORM.<br />    The database can be initialized with the 'initialize' function below.<br />    """<br />    [color=red]'''<br />    pg_class是postgresql记载表和几乎所有有字段或者是那些类似表的东西,OE里所有的对象都存储在ir_module_module,我们利用它来判断<br />    某个数据库是否已初始化<br />    '''[/color]<br />    cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'")<br />    return len(cr.fetchall()) > 0<br /><br />[color=red]'''<br />初始化数据库<br />'''[/color]<br />def initialize(cr):<br />    """ Initialize a database with for the ORM.<br />    This executes base/base.sql, creates the ir_module_categories (taken<br />    from each module descriptor file), and creates the ir_module_module<br />    and ir_model_data entries.<br />    """<br />    f = openerp.modules.get_module_resource('base', 'base.sql')  [color=red]//获取base模块下的base.sql文件[/color]<br />    if not f:<br />        m = "File not found: 'base.sql' (provided by module 'base')."<br />        logger.critical(m)<br />        raise IOError(m)<br />    base_sql_file = openerp.tools.misc.file_open(f)    [color=red]//打开获得的文件[/color]<br />    try:<br />        cr.execute(base_sql_file.read())<br />        cr.commit()<br />    finally:<br />        base_sql_file.close()<br />    for i in openerp.modules.get_modules():<br />        mod_path = openerp.modules.get_module_path(i)<br />        if not mod_path:<br />            continue<br />        # This will raise an exception if no/unreadable descriptor file.<br />        info = openerp.modules.load_information_from_description_file(i)    [color=red]//获取模块下的_openerp.py文件[/color]<br />        if not info:<br />            continue<br />        categories = info['category'].split('/')<br />        category_id = create_categories(cr, categories)    [color=red]//创建目录[/color]<br />        if info['installable']:    [color=red]//如果installable为true的话,设置state为uninstalled[/color]<br />            state = 'uninstalled'<br />        else:<br />            state = 'uninstallable'<br />        cr.execute('INSERT INTO ir_module_module <br />                (author, website, name, shortdesc, description, <br />                    category_id, auto_install, state, web, license, application, icon, sequence, summary) <br />                VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id', (<br />            info['author'],<br />            info['website'], i, info['name'],<br />            info['description'], category_id,<br />            info['auto_install'], state,<br />            info['web'],<br />            info['license'],<br />            info['application'], info['icon'],<br />            info['sequence'], info['summary']))    [color=red]//将所有对象的_openerp.py文件中的信息存入到ir_module_module中[/color]<br />        id = cr.fetchone()[0]<br />        cr.execute('INSERT INTO ir_model_data <br />            (name,model,module, res_id, noupdate) VALUES (%s,%s,%s,%s,%s)', (<br />                'module_'+i, 'ir.module.module', 'base', id, True))<br />        dependencies = info['depends']<br />        for d in dependencies:<br />            cr.execute('INSERT INTO ir_module_module_dependency <br />                    (module_id,name) VALUES (%s, %s)', (id, d))<br />    # Install recursively all auto-installing modules<br />    [color=red]'''<br />    递归的安装_openerp_.py文件中所有auto_install为true的模块,这也是为什么刚初始化的数据库会安装了一些模块的原因,因为其<br />    auto_install被设置了true。我们在新建模块的时候一般设成false,不让其初始化数据库时就安装<br />    '''[/color]<br />    while True:<br />        cr.execute("""SELECT m.name FROM ir_module_module m WHERE m.auto_install AND state != 'to install'<br />                      AND NOT EXISTS (<br />                          SELECT 1 FROM ir_module_module_dependency d JOIN ir_module_module mdep ON (d.name = mdep.name)<br />                                  WHERE d.module_id = m.id AND mdep.state != 'to install'<br />                      )""")<br />        to_auto_install = [x[0] for x in cr.fetchall()]<br />        if not to_auto_install: break<br />        cr.execute("""UPDATE ir_module_module SET state='to install' WHERE name in %s""", (tuple(to_auto_install),))<br />    cr.commit()<br /><br />[color=red]'''<br />创建目录,目录用于对模块,组等等进行分门别类,信息来自_openerp_.py中的category项<br />'''[/color]<br />def create_categories(cr, categories):<br />    """ Create the ir_module_category entries for some categories.<br />    categories is a list of strings forming a single category with its<br />    parent categories, like ['Grand Parent', 'Parent', 'Child'].<br />    Return the database id of the (last) category.<br />    """<br />    p_id = None<br />    category = []<br />    while categories:<br />        category.append(categories[0])<br />        [color=red]//对于传进来的目录名进行拼接,组成module_category_xxxxxx,其中xxxxxx就是_openerp_.py里的目录名[/color]<br />        xml_id = 'module_category_' + (''.join(map(lambda x: x.lower(), category))).replace('&', 'and').replace(' ', '')<br />        # search via xml_id (because some categories are renamed)<br />        cr.execute("SELECT res_id FROM ir_model_data WHERE name=%s AND module=%s AND model=%s",<br />                  (xml_id, "base", "ir.module.category"))<br />        c_id = cr.fetchone()<br />        if not c_id:<br />            [color=red]//如果ir_module_data中没有相关目录信息,则新建立,并插入新的数据到ir_module_data中[/color]<br />            cr.execute('INSERT INTO ir_module_category <br />                    (name, parent_id) <br />                    VALUES (%s, %s) RETURNING id', (categories[0], p_id))<br />            c_id = cr.fetchone()[0]<br />            cr.execute('INSERT INTO ir_model_data (module, name, res_id, model) <br />                      VALUES (%s, %s, %s, %s)', ('base', xml_id, c_id, 'ir.module.category'))<br />        else:<br />            c_id = c_id[0]<br />        p_id = c_id<br />        categories = categories[1:]<br />    return p_id<br /><br />[color=red]'''<br />检测数据库是否有unaccent函数,unaccent函数是用来移除给定字符串上的读音符号<br />'''[/color]<br />def has_unaccent(cr):<br />    """ Test if the database has an unaccent function.<br />    The unaccent is supposed to be provided by the PostgreSQL unaccent contrib<br />    module but any similar function will be picked by OpenERP.<br />    """<br />    cr.execute("SELECT proname FROM pg_proc WHERE proname='unaccent'")<br />    return len(cr.fetchall()) > 0<br /># vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待