Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Odoo 14 Development Cookbook

You're reading from   Odoo 14 Development Cookbook Rapidly build, customize, and manage secure and efficient business apps using Odoo's latest features

Arrow left icon
Product type Paperback
Published in Dec 2020
Publisher Packt
ISBN-13 9781800200319
Length 784 pages
Edition 4th Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
 Gajjar Gajjar
Author Profile Icon Gajjar
Gajjar
 Fayolle Fayolle
Author Profile Icon Fayolle
Fayolle
Holger Brunn Holger Brunn
Author Profile Icon Holger Brunn
Holger Brunn
Daniel Reis Daniel Reis
Author Profile Icon Daniel Reis
Daniel Reis
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Preface 1. Chapter 1: Installing the Odoo Development Environment 2. Chapter 2: Managing Odoo Server Instances FREE CHAPTER 3. Chapter 3: Creating Odoo Add-On Modules 4. Chapter 4: Application Models 5. Chapter 5: Basic Server-Side Development 6. Chapter 6: Managing Module Data 7. Chapter 7: Debugging Modules 8. Chapter 8: Advanced Server-Side Development Techniques 9. Chapter 9: Backend Views 10. Chapter 10: Security Access 11. Chapter 11: Internationalization 12. Chapter 12: Automation, Workflows, Emails, and Printing 13. Chapter 13: Web Server Development 14. Chapter 14: CMS Website Development 15. Chapter 15: Web Client Development 16. Chapter 16: The Odoo Web Library (OWL) 17. Chapter 17: In-App Purchasing with Odoo 18. Chapter 18: Automated Test Cases 19. Chapter 19: Managing, Deploying, and Testing with Odoo.sh 20. Chapter 20: Remote Procedure Calls in Odoo 21. Chapter 21: Performance Optimization 22. Chapter 22: Point of Sale 23. Chapter 23: Managing Emails in Odoo 24. Chapter 24: Managing the IoT Box 25. Other Books You May Enjoy

Easy installation of Odoo from source

It is highly recommended to use the Linux Ubuntu operating system for the installation of Odoo, since this is the operating system that Odoo uses for all its tests, debugging, and installations of Odoo Enterprise, in addition to the fact that most developers of Odoo also use GNU/Linux distributions, and is much more likely to get support from the Odoo community for OS-level issues that occur in GNU/Linux than Windows or macOS.

It is also recommended to develop Odoo add-on modules using the same environment (the same distribution and the same version) as the one that will be used in production. This will avoid nasty surprises, such as discovering on the day of deployment that a 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 (VM) on your workstation and install a GNU/Linux distribution in the VM.

Note

Ubuntu is available as an app in Microsoft Store so you can use that too, if you do not want to switch to Ubuntu OS.

For this book, we will be using Ubuntu Server 18.04 LTS, but you can use any another Debian GNU/Linux OS. Whatever Linux distribution you choose, you should have some notion of how to use it from the command line, and having knowledge of system administration will certainly not do any harm.

Getting ready

We are assuming that you have Ubuntu 18.04 up and running and that you have an account with root access or that sudo has been configured. In the following sections, we will install Odoo's dependencies and download Odoo's source code from GitHub.

Note

Some of the configurations require a system login username, so we will use $(whoami) whenever a login username is required in a command line. This is a shell command that will substitute your login in the command you are typing.

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

How to do it...

To install Odoo from source, perform the following steps:

  1. Run the following commands to install the main dependencies:
    $ sudo apt-get update
    $ sudo apt install git python3-pip build-essential wget python3-dev python3-venv python3-wheel libxslt-dev libzip-dev libldap2-dev libsasl2-dev python3-setuptools libpng12-0 libjpeg-dev gdebi -y
  2. Download and install wkhtmltopdf:
    $ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.trusty_amd64.deb
    $ sudo dpkg -i wkhtmltox_0.12.5-1.trusty_amd64.deb

    If you find errors in a previous command, force install the dependencies with the following command:

    $ sudo apt-get install -f
  3. Now, install the PostgreSQL database:
    $ sudo apt install postgresql -y
  4. Configure PostgreSQL:
    $ sudo -u postgres createuser --superuser $(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 14.0 --single-branch --depth 1 https://github.com/odoo/odoo.git
  7. Create an odoo-14.0 virtual environment and activate it:
    $ python3 -m venv ~/venv-odoo-14.0
    $ source ~/venv-odoo-14.0/bin/activate
  8. Install the Python dependencies of Odoo in venv:
    $ cd ~/odoo-dev/odoo/
    $ pip3 install -r requirements.txt
  9. Create and start your first Odoo instances:
    $ createdb odoo-test
    $ python3 odoo-bin -d odoo-test –i base --addons-path=addons --db-filter=odoo-test$
  10. Point your browser to http://localhost:8069 and authenticate it by using the admin account and using admin as the password.

    Note

    If you need RTL support, please install node and rtlcss via the following command:sudo apt-get install nodejs npm -y sudo npm install -g rtlcss

How it works...

In step 1, we installed several core dependencies. These dependencies include various tools, such as git, pip3, wget, Python setup tools, and more. These core tools will help us install other Odoo dependencies using simple commands.

In step 2, we downloaded and installed the wkhtmltopdf package, which is used in Odoo to print PDF documents such as sale orders, invoices, and other reports. Odoo 14.0 needs version 0.12.5 of wkhtmltopdf, and that exact version might be not included in the current Linux distributions. Fortunately for us, the maintainers of wkhtmltopdf provide pre-built packages for various distributions at http://wkhtmltopdf.org/downloads.html and we have downloaded and installed it from that URL.

PostgreSQL configuration

In step 3, we installed the PostgreSQL database.

In step 4, we created a new database user with the login name of your system. $(whoami) is used to fetch your login name, and the -s option is used to give super user rights. Let's see why we need these configurations.

Odoo uses the psycopg2 Python library to connect with a PostgreSQL database. To access a PostgreSQL database with the psycopg2 library, Odoo uses the following values by default:

  • By default, psycopg2 tries to connect to a database with the same username as the current user on local connections, which enables password-less authentication (this is good for the development environment).
  • The local connection uses Unix domain sockets.
  • The database server listens on port 5432.

That's it! Your PostgreSQL database is now ready to be connected with Odoo.

As this is a development server, we have given --superuser rights to the user. It is OK to give the PostgreSQL user more rights as this will be your development instance. For a production instance, you can use the --createdb command line instead of --superuser to restrict rights. The –superuser rights in a production server will give additional leverage to an attacker exploiting a vulnerability in some part of the deployed code.

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'"

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 4 of this recipe. That's fine; just add the --dbname option with an existing database name, such as --dbname template1.

Git configuration

For the development environment, we are using Odoo sourced from GitHub. With git, you can easily switch between different Odoo versions. Also, you can fetch the latest changes with the git pull command.

In step 5, we configured your git user.

In step 6, we downloaded the source code from Odoo's official GitHub repository. We have used the git clone command to download Odoo's source code. We have used a single branch as we only want a branch for the 14.0 version. Also, we have used --depth 1 to avoid downloading the full commit history of the branch. These options will download the source code very quickly, but if you want, you can omit those options.

Odoo developers also propose nightly builds, which are available as tarballs and distribution packages. The main advantage of using 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 that you can make your bug reports more precise and helpful for developers.

Note

If you have access to the enterprise edition source code, you can download that too in a separate folder under the ~/odoo-dev directory.

Virtual environments

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

You can create as many environments as you wish using the python3 -m venv ~/newvenv command. This will create a newvenv directory in the specified location, containing a bin/ subdirectory and a lib/python3.6 subdirectory.

In step 7, we created a new virtual environment in the ~/venv-odoo-14.0 directory. This will be our isolated Python environment for Odoo, and all of Odoo's Python dependencies will be installed in this environment.

To activate the virtual environment, we need to use the source command. With the source ~/venv-odoo-14.0/bin/activate command, we have activated the virtual environment.

Installing Python packages

Odoo's source code has a list of Python dependencies in requirements.txt. In step 8, we installed all those requirements via the pip3 install command.

That's it. Now you can run the Odoo instance.

Starting the instance

Now comes the moment you've been waiting for. To start our first instance, in step 9, we first created a new empty database, used the odoo-bin script, and then started the Odoo instance with the following command:

python3 odoo-bin -d odoo-test -i base --addons-path=addons --db-filter=odoo-test$

You can also omit python3 by using ./ before odoo-bin as it is an executable Python script, as follows:

./odoo-bin -d odoo-test –i base --addons-path=addons --db-filter=odoo-test$

With odoo-bin, a script with the following command-line arguments are used:

  • -d database_name: Use this database by default.
  • --db-filter=database_name$: Only try to connect to databases that match the supplied regular expression. One Odoo installation can serve multiple instances that live in separate databases, and this argument limits the available databases. The trailing $ is important as the regular expression is used in match mode. This enables you to avoid 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 add-ons. This list is scanned at instance creation time to populate the list of available add-on modules in the instance. If you want to use Odoo's Enterprise Edition, then add its directory with this option.
  • -i base: This is used to install a base module. This is required when you have created a database via the command line.

If you are using a database user with a database login that is 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: This is the password for authenticating against the PostgreSQL server.

To get an overview of all available options, use the --help argument. We will see more of the odoo-bin script later in this chapter.

When Odoo is started on an empty database, it will first create the database structure that's needed to support its operations. It will also scan the add-ons path to find the available add-on modules and insert some into the initial records in the database. This includes the admin user with the default admin password, which you will use for authentication.

Pointing your web browser to http://localhost:8069/ leads you to the login page of your newly created instance, as shown in the following screenshot:

Figure 1.2 – Login screen of the Odoo instance

Figure 1.2 – Login screen of the Odoo instance

This is due to the fact that Odoo includes an HTTP server. By default, it listens on all local network interfaces on TCP port 8069.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images