Book Image

Odoo Development Cookbook

By : Holger Brunn, Alexandre Fayolle, Daniel Reis
Book Image

Odoo Development Cookbook

By: Holger Brunn, Alexandre Fayolle, Daniel Reis

Overview of this book

Odoo is a full-featured open source ERP with a focus on extensibility. The flexibility and sustainability of open source is also a key selling point of Odoo. It is built on a powerful framework for rapid application development, both for back-end applications and front-end websites. The book starts by covering Odoo installation and administration, and provides a gentle introduction to application development. It then dives deep into several of the areas that an experienced developer will need to use. You’ll learn implement business logic, adapt the UI, and extend existing features.
Table of Contents (23 chapters)
Odoo Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Easy installation of Odoo from source


For Odoo deployment, it is recommended to use a GNU/Linux environment. You may be more at ease using Microsoft Windows or Mac OS X, but the fact is that most of the Odoo developers are using GNU/Linux and you are much more likely to get support from the community for OS-level issues happening on GNU/Linux than on Windows.

It is also recommended to develop using the same environment (same distribution, same version) as the one which will be used in production. This will avoid nasty surprises such as discovering on the day of deployment that some library has a different version than expected, with a slightly different and incompatible behavior. If your workstation is using a different OS, a good approach is to set up a virtual machine on your workstation and to install a GNU/Linux distribution in the VM.

Tip

To avoid copying files between your workstation where you are running your development environment and the virtual machine which runs Odoo, you can configure a SAMBA share inside the virtual machine and store the source code there. You can then mount the share on your workstation in order to edit the files easily.

This book assumes you are running Debian GNU/Linux as its stable version (Jessie at the time of writing). Ubuntu is another popular choice, and since it is built on top of Debian, most of the examples in this book should work unchanged. Whatever Linux distribution you choose, you should have some notion of how to use it from the command line, and having a few ideas about system administration will certainly not cause any harm.

Getting ready

We assume that Linux is up and running and that you have an account with root access, either because you know the root password or because sudo has been configured. In the following pages, we will be using $(whoami) whenever the login of your work user is required in a command line. This is a shell command which will substitute your login in the command you are typing.

Some operations will definitely be easier if you have a GitHub account. Go to https://github.com and create one if you don't have one already.

How to do it...

To install Odoo from source, you need to follow these steps:

  1. Run the following commands to install the main dependencies:

    $ sudo apt-get install git python2.7 postgresql nano \
    python-virtualenv
    
  2. Download and install wkhtmltopdf:

    $ wget http://nightly.odoo.com/extra/wkhtmltox-0.12.1.2_linux-jessie-amd64.deb
    $ sudo dpkg -i wkhtmltox-0.12.1.2_linux-jessie-amd64.deb
    

    Note

    Caution!

    This is a package provided by the Odoo maintainer for Debian Jessie. If you are using another distribution, browse to http://download.gna.org/wkhtmltopdf/0.12/0.12.1/ and download the package for your operating system.

  3. Now, use this to install the build dependencies:

    $ sudo apt-get install gcc python2.7-dev libxml2-dev \
    libxslt1-dev libevent-dev libsasl2-dev libldap2-dev libpq-dev \
    libpng12-dev libjpeg-dev
    
  4. Configure PostgreSQL:

    $ sudo -u postgres createuser --createdb $(whoami)
    $ createdb $(whoami)
    
  5. Configure git:

    $ git config --global user.name "Your Name"
    $ git config --global user.email [email protected]
    
  6. Clone the Odoo code base:

    $ mkdir ~/odoo-dev
    $ cd ~/odoo-dev
    $ git clone -b 9.0 --single-branch https://github.com/odoo/odoo.git
    $ cd odoo
    
  7. Create an odoo-9.0 virtual environment and activate it:

    $ virtualenv ~/odoo-9.0
    $ source ~/odoo-9.0/bin/activate
    
  8. Install the Python dependencies of Odoo in virtualenv:

    $ pip install -r requirements.txt
    
  9. Create and start your first Odoo instances:

    $ createdb odoo-test
    $ python odoo.py -d odoo-test --addons-path=addons \
    --dbfilter=odoo-test$
    
  10. Point your browser to http://localhost:8069 and authenticate using the admin account and admin as password.

Tip

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  • Log in or register to our website using your e-mail address and password

  • Hover the mouse pointer on the SUPPORT tab at the top

  • Click on Code Downloads & Errata

  • Enter the name of the book in the Search box

  • Select the book for which you're looking to download the code files

  • Choose from the drop-down menu where you purchased this book from

  • Click on Code Download

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

How it works...

Dependencies come from various sources. First, you have the core dependencies of Odoo, the Python interpreter that is used to run the source code, and the PostgreSQL database server used to store the instance data. Git is used for source code versioning and getting the source code of Odoo itself.

Since we will need to edit some files as root or as postgres (the PostgreSQL administrative user) on our server, we need to install a console-based text editor. We suggest nano as it is very simple to use, but feel free to choose any editor with which you feel at ease as long as it works on the console, such as vim, e3, or emacs-nox.

Wkhtmltopdf is a runtime dependency of Odoo used to produce PDF reports. The version required by Odoo 9.0 is 0.12.1, which is not included in current GNU/Linux distributions. Fortunately for us, the maintainers of wkhtmltopdf provide prebuilt packages for various distributions on http://wkhtmltopdf.org/downloads.html (in the archive section). However, Debian Jessie is not there, so the Odoo maintainers provide their own version of the package on http://nightly.odoo.com/extra/.

There are lots of other runtime dependencies that are Python modules, which we can install using pip in a virtual environment. However, some of these Python modules can feature some dependencies on native C libraries for which the Python bindings need to be compiled. We therefore install the development packages for these C libraries as well as the Python development package and a C compiler. Once these build dependencies are installed, we can use pip -r requirements.txt (a file which comes from the Odoo source code distribution) to download, compile, and install the Python modules.

Virtual environments

Python virtual environments, or virtualenv for short, are isolated Python workspaces. They are very useful to Python developers because they allow different workspaces with different versions of various Python libraries installed, possibly on different Python interpreter versions.

You can create as many environments as you wish using the command virtualenv path/to/newenv. This will create a newenv directory in the specified location, containing a bin/ subdirectory and a lib/python2.7 subdirectory.

In bin/ you will find several scripts:

  • activate: The script is not executed, it is sourced using the built-in source shell. This will activate the environment by adjusting the PATH environment variable to include the bin/ directory of the virtualenv. It also installs a shell function called deactivate, which you can run to exit the virtualenv, and changes the shell prompt to let you know which virtualenv is currently activated

  • pip: This is a special version of the pip command which acts inside the virtualenv only.

  • python: This is a wrapper around your system Python interpreter which uses the packages installed in the virtualenv.

Tip

The built-in source shell is also available (as a single dot, followed by a space, and the path to the file to source). The shortcut form is perfectly fine, but we will stick to source in this book for readability.

There are two main ways of using a virtualenv. You may activate it as we show in the recipe (and call deactivate when you're done) or you may use the scripts in the bin/ directory of the environment explicitly by calling them with their full path, in which case you don't need to activate the virtualenv. This is mainly a matter of taste, so you should experiment and find out which style suits you better for which case.

You may have executable Python scripts with the first line looking like the following:

#! /usr/bin/env python

These will be easier to use with an activated virtualenv. This is the case with the odoo.py script, which you can therefore call in the following way:

$ ./odoo.py -d odoo-test --addons-path=addons --db-filter=odoo-test$

PostgreSQL configuration

On a GNU/Linux system, Odoo works very well with the default values of psycopg2, the Python module used to access a PostgreSQL database:

  • Passwordless authentication if the database user has the same name as the current user on local connections

  • Local connection uses Unix domain sockets

  • The database server listens on port 5432

In that case, there is nothing special to do: we use the postgres administrative user to create a database user which shares our login name and give it the right to create new databases. We then create a new database with the same name as the new user, which will be used as a default database when using the psql command.

When on a development server, it is OK to give the PostgreSQL user more rights and to use the --superuser command-line option rather than just --createdb. The net effect is that this user can then also create other users and globally manage the database instance. If you feel --superuser is too much, you may still want to use --createrole in addition to --createdb when creating your database user. Avoid doing this on production servers as it would give additional leverage to an attacker exploiting a vulnerability in some part of the deployed code (see Chapter 16, Server Deployment).

If you want to use a database user with a different login, you will need to provide a password for the user. This is done by passing the --pwprompt flag on the command line when creating the user, in which case the command will prompt you for the password.

If the user has already been created and you want to set a password (or modify a forgotten password) you can use the following command:

$ psql -c "alter role $(whoami) with password 'newpassword'"

Note

If this command fails with an error message saying that the database does not exist, it is because you did not create a database named after your login name in step 3. That's fine; just add the --dbname option with an existing database name such as --dbname template1.

Git configuration

At some point in the book, you will need to use git commit. This will fail unless some basic configuration is performed; you need to provide Git with your name and email address. Git will remind you to do this with a nice error message, but you may as well do it now.

Note

This is also something to keep in mind if you are using a service such as Travis for continuous integration, and your test scripts need to perform some git merges: you have to provide a dummy name and e-mail for the merging to succeed.

Downloading the Odoo source code

Downloading the Odoo code base is done by performing a git clone operation. Be patient as this will take some time. The options --branch 9.0 --single-branch avoid downloading other branches and save a little time. The --depth option can also be used to avoid downloading the whole repository history, but the downside of that option is that you will not be able to explore that history when looking for issues.

The Odoo developers also propose nightly builds, which are available as tarballs and distribution packages. The main advantage of using a git clone is that you will be able to update your repository when new bug fixes are committed in the source tree. You will also be able to easily test any proposed fixes and track regressions, so you can make your bug reports more precise and helpful for the developers.

Starting the instance

Now comes the moment you've been waiting for. To start our first instance, we first create a new empty database and then use the odoo.py script with the following command-line arguments:

  • -d database_name: Use that database by default.

  • --db-filter=database_name$: Only try to connect to databases matching the supplied regular expression. One Odoo installation can serve multiple instances living in separate databases and this argument limits the available databases. The trailing $ is important as the regular expression is used in match mode; this avoids selecting names starting with the specified string.

  • --addons-path=directory1,directory2,...: This is a comma separated list of directories in which Odoo will look for addons. This list is scanned at the instance creation time to populate the list of available add-on modules in the instance.

If you are using a database user with a database login different from your Linux login, you need to pass the following additional arguments:

  • --db_host=localhost: use a TCP connection to the database server

  • --db_user=database_username: use the specified database login

  • --db_password=database_password: the password to use to authenticate against the PostgreSQL server

To get an overview of all the available options, use the --help argument. We will see much more about the odoo.py script in this chapter as well as in Chapter 2, Managing Odoo Server Instances.

When Odoo is started on an empty database, it will first create the database structure needed to support its operations. It will also scan the addons path to find the available addon modules, and insert some the initial records in the database. This includes the admin user with the default password admin which you will use to authenticate with.

Odoo includes an HTTP server. By default, it listens on all local network interfaces on TCP port 8069 so pointing your web browser to http://localhost:8069/ leads you to your newly created instance.

There is more…

In the recipe, we downloaded the latest stable version of Odoo using the following command:

$ git clone -b 9.0 --single-branch https://github.com/odoo/odoo.git

This uses the official branch maintained by Odoo. One issue with this branch is that bug fixes contributed by the community are not always merged in a timely fashion. The Odoo Community Association (OCA) maintains a parallel branch in which fixes and improvements are peer-reviewed by the community and tend to be merged faster than on the official branch. It is not a fork of Odoo, and the latest version of Odoo is merged back into that branch daily. You may want to use it for your developments and deployments, in which case you need to clone Odoo like this:

$ git clone -b 9.0 --single-branch https://github.com/OCA/OCB.git odoo