odoo-python-development

Setting up a Development Environment for Odoo 17.x

Introduction

For one of the projects I am involved it, I was looking into SAML integration with Odoo 17.x. SAML is not supported by Odoo out of the box. The Odoo Community Association (OCA), an organization that has been developing modules for the Odoo platform for many years, has a collection of modules dedicated to authentication called server-auth. The modules that can be found on their Github repository extends the authentication functionality of Odoo, such as OAuth, SAML, LDAP and OIDC integrations.

For instance, the auth_saml module, adds SAML integration to Odoo. Although, it was available for Odoo 16.x, it was not available on Odoo 17.x in July 2024. Since I needed the upgrade, I was monitoring the repository and I noticed that progress for Odoo 17.x support has stalled. When I looked deeper into the matter, I figured that there was a pull request for auth_saml for Odoo 17.x was actually ready. The only thing remaining of the checklist was that to increase test code coverage by few percentage points beyond a threshold set by the repository maintainer.

Since it is in essence a Python open source project, an environment that I am pretty familiar with, I offered to help with improving the test code coverage. My goal was to add tests to the auth_saml module, ensure that the coverage have crossed the threshold line, and submit the code with the hope to finally get auth-saml on Odoo 17.x.

Because I didn’t have any prior experience with setting up the development environment for Odoo 17 or for Odoo 17 modules. it took me several precious hours until I figured it out and I could start making progress on the task at hand. Hence the motivation behind this blog post. I am writing this HowTo entry, with the hope that it will shorten the setup time for anyone who is willing to donate time and effort to Odoo, including myself.

In the following, I will describe the steps I took to setup a development environment for Odoo 17.x on an Ubuntu 22.04 LTS machine.

The Steps

The first step in setting up a development environment for Odoo 17.x on Ubuntu 22.04 LTS, is to get all software packages necessary for Odoo and for running tests in Odoo.

$ sudo apt-get update
$ sudo apt-get install -y build-essential npm python3-pip python3-dev python3-venv \
libxml2-dev libxslt1-dev zlib1g-dev libsasl2-dev libldap2-dev libssl-dev libblas-dev \
libatlas-base-dev libffi-dev libmysqlclient-dev libjpeg-dev libpq-dev libjpeg8-dev liblcms2-dev \
git wget postgresql postgresql-client

That will install packages necessary for running python, and for installing python packages and will ensure that git, and wget are already installed because we will need them to clone the Odoo repository and to get any additional assets from the internet. It will also install the postgresql database that Odoo needs to store data.and the client necessary to communicate with that Database. If you are have an SQL server running or you have an alternative to the PostgreSQL server, you could simply bypass installing postgresql postgresql-client.

Odoo 17 needs wkhtmltopdf for generating PDFs. wkhtmltopdf is available in the Ubuntu aptitude repository, however, the Odoo 17 requires a version that is greater than 0.12.2, like version 0.12.5, which is not possible via the Ubuntu 22.04 LTS repositories. Therefore we have to download it and install it “manually”:

$ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb
$ sudo dpkg -i wkhtmltox_0.12.5-1.bionic_amd64.deb
$ sudo apt install -f
$ rm wkhtmltox_0.12.5-1.bionic_amd64.deb

Now let’s install the web dependencies:

$ sudo ln -s /usr/bin/nodejs /usr/bin/node
$ sudo npm install -g less less-plugin-clean-css
$ sudo apt-get install -y node-less

Now we need to prepare our workspace. Let’s create a folder called workspace, it will contain Odoo code, and any other repositories we will clone, and any other files related the project, such as coverage details, log files, configuration files etc.

$ mkdir ~/workspace

Now we are ready to clone Odoo. Let’s clone the repository and switch to branch 17.0 or just clone branch 17.0.

$ cd ~/workspace
$ git clone https://www.github.com/odoo/odoo --branch 17.0

Let’s now create a Python virtual environment, to install all Python dependencies in an isolated way:

$ cd ~/workspace/odoo
$ python3 -m venv venv
$ source venv/bin/activate
(venv)$ pip install -r requirements.txt

We are almost there, before our development environment is ready is run, we need to create a database dedicated for our project in the designated database. In the case that PostgreSQL database that is running on localhost is being used. We will create a db user called `odoo`, a database called odoo and make sure the user odoo has all privileges on db odoo

(venv)$ sudo su - postgres
$ createuser --createdb --username postgres --no-createrole --no-superuser --pwprompt odoo
$ createdb odoo --owner odoo
$ psql
> CREATE ROLE odoo WITH PASSWORD 'password';
> ALTER ROLE odoo WITH SUPERUSER;
> GRANT ALL PRIVILEGES ON DATABASE odoo TO odoo;

Now let's exit psql

> \q

Let's test:

$ psql -U odoo -d odoo -h localhost
> \du
> \q

Now let's exit the session of the user postgres

$ exit

The last piece of the puzzle is the Odoo configuration file. Let’s create one that is called odoo.conf. It could be located in the Odoo folder, but it could be placed anywhere. The odoo.conf file tells Odoo how to connect to the SQL database, what the HTTP port is among other things. Here is a simple odoo.conf that is practically enough for our use-case. Here we assume we are using the postgresql database server that is running on localhost:

[options]
admin_passwd = admin_password
db_host = localhost
db_port = 5432
db_user = odoo
db_password = password
http_port = 8080
addons_path = ${HOME}/workspace/odoo/addons
data_dir = ${HOME}/workspace/odoo/data

Running Tests

To test the development environment, we will run ./odoo-bin which is the Odoo CLI that can run tests:

(venv)$ ./odoo-bin -c odoo.conf --db-filter=^odoo$ --test-enable --stop-after-init --log-level=test -i base

This command will run the ./odoo-bin script using the configurations in odoo.conf using the database odoo and run tests (--test-enable) with the base modules installed.

(venv)$ ./odoo-bin -c odoo.conf --db-filter=^odoo$ --test-enable --stop-after-init --log-level=test -i base

and to check test code coverage:

(venv)$ coverage run --source ${HOME}/workspace/odoo ./odoo-bin -c odoo.conf --db-filter=^odoo$ --test-enable --stop-after-init --log-level=test -i base
(venv)$ coverage html

3rd Party Modules

With the development environment ready, we can now think about 3rd party modules such as the server-auth modules.

Let’s clone the server-auth repository and see how to run 3rd party modules tests and how to analyze test code coverage. I will use the OCA/server-auth repository. Actually for my specific use-case, I clone the forked repository and checkout the branch that was related to the pull request I wanted to improve upon. But for simplicity, let’s clone the original repository and the branch 17.0

(venv)$ cd ~/workspace/
(venv)$ git clone https://github.com/OCA/server-auth.git
(venv)$ git checkout 17.0

Now we want to run the tests in the module auth_saml. To do so, we will run odoo-bin, with the module auth_saml installed

(venv)$ cd ~/workspace/odoo
(venv)$ ./odoo-bin -c odoo.conf --db-filter=^odoo$ --test-enable --stop-after-init --log-level=test -i auth_saml

For code coverage:

(venv)$ coverage run --source ${HOME}/workspace/server-auth/auth_saml ./odoo-bin -c odoo.conf --db-filter=^odoo$ --test-enable --stop-after-init --log-level=test -i auth_saml
(venv)$ coverage html

Now you are all set, to edit the code of Odoo, or a module and to run tests from the command line.

Developing, Debugging using VSCode

Coming soon…

Conclusion

In this blog post, I described the steps necessary to prepare the development environment for Odoo 17.x specifically. I showed how to run automated tests for Odoo, and how to run the tests for 3rd party modules. I also discussed commands for the generation of code coverage reports.

I hope will motivate you to contribute to this great Open Source project by making it much easier to setup the development environment.