第二章:开发环境设置

根据预期的使用情况,有多种安装Odoo的方法。对于Odoo社区的开发人员和Odoo员工来说,首选的方法是执行源代码安装(从源代码运行Odoo)。

准备环境

首先,按照 环境设置 部分的贡献指南来准备您的环境。

By now, you should have downloaded the source code into two local repositories, one for odoo/odoo and one for odoo/enterprise. These repositories are set up to push changes to pre-defined forks on GitHub. This will prove to be convenient when you start contributing to the codebase, but for the scope of this tutorial, we want to avoid polluting them with training material. Let’s then develop your own module in a third repository odoo/tutorials. Like the first two repositories, it will be part of the addons-path that references all directories containing Odoo modules. In this repository, we will create our first module!

注解

This repository also already contains some bare modules that will be used in other tutorials.

  1. Following the same process as with the odoo/odoo and odoo/enterprise repositories, clone the odoo/tutorials repository on your machine with:

    $ git clone git@github.com:odoo/tutorials.git
    
  2. Configure your fork and Git to push changes to your fork rather than to the main codebase. If you work at Odoo, configure Git to push changes to the shared fork created on the account odoo-dev.

    1. Visit github.com/odoo/tutorials and click the Fork button to create a fork of the repository on your account.

    2. In the command below, replace <your_github_account> with the name of the GitHub account on which you created the fork.

      $ cd /TutorialsPath
      $ git remote add dev git@github.com:<your_github_account>/tutorials.git
      

就是这样!您的环境现在已经准备好从源代码运行Odoo,并且您已经成功创建了一个用作插件目录的存储库。这将允许您将您的工作推送到GitHub。

重要

For Odoo employees only:

  1. Make sure to read very carefully 进行你的第一次贡献. In particular, your branch name must follow our conventions.

  2. Once you have pushed your first change to the shared fork on odoo-dev, create a PR. Please put your quadrigram in the PR title (e.g., “abcd - Technical Training”).

    This will enable you to share your upcoming work and receive feedback from your coaches. To ensure a continuous feedback loop, we recommend pushing a new commit as soon as you complete a chapter of the tutorial. Note that the PR is automatically updated with commits you push to odoo-dev, you don’t need to open multiple PRs.

  3. At Odoo we use Runbot extensively for our CI tests. When you push your changes to odoo-dev, Runbot creates a new build and test your code. Once logged in, you will be able to see your branches Tutorials project.

注解

The specific location of the repositories on your file system is not crucial. However, for the sake of simplicity, we will assume that you have cloned all the repositories under the same directory. If this is not the case, make sure to adjust the following commands accordingly, providing the appropriate relative path from the odoo/odoo repository to the odoo/tutorials repository.

运行服务器

使用 odoo-bin 启动

一旦所有依赖项都设置好,可以通过运行 odoo-bin 来启动 Odoo,这是服务器的命令行界面。

$ cd $HOME/src/odoo/
$ ./odoo-bin --addons-path="addons/,../enterprise/,../tutorials" -d rd-demo

有多个 命令行参数 可用于运行服务器。在本培训中,您只需要其中一些。

-d <database>

将要使用的数据库。

--addons-path <directories>

存储模块的目录列表,用逗号分隔。这些目录将被扫描以查找模块。

--limit-time-cpu <limit>

限制每个请求的工作进程使用不超过<limit> CPU秒。

--limit-time-real <limit>

防止工作进程在处理请求时超过<limit>秒。

小技巧

  • The --limit-time-cpu and --limit-time-real arguments can be used to prevent the worker from being killed when debugging the source code.

  • 您可能会遇到类似的错误 AttributeError: module '<MODULE_NAME>' has no attribute '<$ATTRIBUTE'>。在这种情况下,您可能需要使用 $ pip install --upgrade --force-reinstall <MODULE_NAME> 重新安装模块。
    如果此错误发生在多个模块上,您可能需要使用 $ pip install --upgrade --force-reinstall -r requirements.txt 重新安装所有的要求。
    您也可以清除Python缓存来解决此问题:
    $ cd $HOME/.local/lib/python3.8/site-packages/
    $ find -name '*.pyc' -type f -delete
    
  • 其他常用的参数包括:

    • -i: Install some modules before running the server (comma-separated list). This is equivalent to going to Apps in the user interface, and installing the module from there.

    • -u: Update some modules before running the server (comma-separated list). This is equivalent to going to Apps in the user interface, selecting a module, and upgrading it from there.

注解

For now you cannot add ../technical-training-sandbox to your addons-path as it is empty and will result into an invalid addons-path folder error, but you will have to add it back later on !

登录Odoo

打开 http://localhost:8069/ 在你的浏览器上。我们推荐使用 Chrome <https://www.google.com/intl/en/chrome/> _, Firefox <https://www.mozilla.org/firefox/new/> _,或者任何其他带有开发工具的浏览器。

要登录管理员用户,请使用以下凭据:

  • email: admin

  • 密码: admin

启用开发者模式

开发者或调试模式对于培训非常有用,因为它提供了额外的(高级)工具。在接下来的章节中, 我们将始终假设您已启用开发者模式

Enable the developer mode now. Choose the method that you prefer; they are all equivalent.

注解

只有至少安装了一个应用程序,才能访问设置屏幕的主页面。在下一章节中,您将被引导安装自己的应用程序。

额外工具

有用的 Git 命令

以下是一些日常工作中有用的 Git 命令。

  • 切换分支:
    当您切换分支时,两个仓库(odoo 和 enterprise)必须同步,即两者都需要在同一个分支中。
    $ cd $HOME/src/odoo
    $ git switch 16.0
    
    $ cd $HOME/src/enterprise
    $ git switch 16.0
    
  • 获取并变基:

    $ cd $HOME/src/odoo
    $ git fetch --all --prune
    $ git rebase --autostash odoo/16.0
    
    $ cd $HOME/src/enterprise
    $ git fetch --all --prune
    $ git rebase --autostash enterprise/16.0
    

代码编辑器

如果你在Odoo工作,你的许多同事都在使用 VSCode <https://code.visualstudio.com> _, VSCodium <https://vscodium.com> _(开源的等效品), PyCharm <https://www.jetbrains.com/pycharm/download/#section=linux> _,或者 Sublime Text <https://www.sublimetext.com> _。然而,你可以自由选择你喜欢的编辑器。

配置您的 Linter 是非常重要的。使用 Linter 可以帮助您显示语法和语义警告或错误。Odoo 源代码尽力遵守 Python 和 JavaScript 的标准,但有些标准可以忽略。

对于Python,我们使用PEP8,忽略这些选项:

  • E501: line too long

  • E301: expected 1 blank line, found 0

  • E302: expected 2 blank lines, found 1

对于JavaScript,我们使用ESLint,你可以在这里找到一个 配置文件示例

PostgreSQL的管理员工具

你可以使用命令行来管理你的PostgreSQL数据库,就像之前演示的那样,或者使用图形用户界面应用程序,比如 pgAdmin 或者 DBeaver

我们建议您使用Unix套接字连接来连接GUI应用程序和数据库。

  • 主机名/地址: /var/run/postgresql

  • 端口: 5432

  • 用户名: $USER

Python调试

当面对错误或者试图理解代码运行时,简单地打印输出可以解决很多问题,但是一个合适的调试器可以节省很多时间。

你可以使用经典的Python库调试器(pdbpudbipdb),或者你可以使用你的编辑器的调试器。

在下面的例子中,我们使用ipdb,但使用其他库的过程类似。

  1. 安装库:

    pip install ipdb
    
  2. 放置触发器(断点):

    import ipdb; ipdb.set_trace()
    

    Example

    def copy(self, default=None):
        import ipdb; ipdb.set_trace()
        self.ensure_one()
        chosen_name = default.get('name') if default else ''
        new_name = chosen_name or _('%s (copy)') % self.name
        default = dict(default or {}, name=new_name)
        return super(Partner, self).copy(default)
    

这是一份命令列表:

h(elp) [command]

如果没有提供参数,则打印可用命令的列表。如果提供了命令作为参数,则打印有关该命令的帮助信息。

pp expression

使用 pprint 模块对 expression 的值进行漂亮的打印。

w(here)

打印堆栈跟踪,最近的帧在底部。

d(own)

将当前帧在堆栈跟踪中向下移动一级(到一个较新的帧)。

u(p)

将当前帧在堆栈跟踪中向上移动一级(到旧帧)。

n(ext)

继续执行,直到当前函数中的下一行被执行或函数返回。

c(ontinue)

继续执行,只有在遇到断点时才停止。

s(tep)

执行当前行。在可能的第一个机会停止(在被调用的函数中或在当前函数的下一行)。

q(uit)

退出调试器。正在执行的程序被中止。

现在您的服务器已经运行起来了,是时候开始 编写您自己的应用程序!