Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Odoo Development Cookbook

You're reading from   Odoo Development Cookbook Build effective business applications using the latest features in Odoo 17

Arrow left icon
Product type Paperback
Published in May 2024
Publisher Packt
ISBN-13 9781805124276
Length 774 pages
Edition 5th Edition
Languages
Tools
Arrow right icon
Authors (6):
Arrow left icon
Holger Brunn Holger Brunn
Author Profile Icon Holger Brunn
Holger Brunn
Husen Daudi Husen Daudi
Author Profile Icon Husen Daudi
Husen Daudi
Daniel Reis Daniel Reis
Author Profile Icon Daniel Reis
Daniel Reis
Jay Vora Jay Vora
Author Profile Icon Jay Vora
Jay Vora
Parth Gajjar Parth Gajjar
Author Profile Icon Parth Gajjar
Parth Gajjar
Alexandre Fayolle Alexandre Fayolle
Author Profile Icon Alexandre Fayolle
Alexandre Fayolle
+2 more Show less
Arrow right icon
View More author details
Toc

Table of Contents (28) Chapters Close

Preface 1. Chapter 1: Installing the Odoo Development Environment FREE CHAPTER 2. Chapter 2: Managing Odoo Server Instances 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. Chapter 25: Web Studio 26. Index 27. Other Books You May Enjoy

Installing Odoo from the source

It is highly recommended that you use the Linux Ubuntu operating system to install Odoo since this is the operating system that Odoo uses for all its tests, debugging, and installations of Odoo Enterprise. Additionally, most Odoo developers use GNU/Linux distributions, so they are much more likely to get support from the Odoo community for operating system-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 operating system, 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 the Microsoft Store, so you can use that if you don’t want to switch to Ubuntu.

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

Getting ready

We are assuming that you have Ubuntu 22.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 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 the source, perform the following steps:

  1. Run the following commands to install the main dependencies:
    $ sudo apt-get update
    $ sudo apt install openssh-server fail2ban python3-pip python3-dev libxml2-dev libxslt1-dev zlib1g-dev libsasl2-dev libldap2-dev build-essential libssl-dev libffi-dev libmysqlclient-dev libpq-dev libjpeg8-dev liblcms2-dev libblas-dev libatlas-base-dev git curl python3-venv python3.10-venv fontconfig libxrender1 xfonts-75dpi xfonts-base -y
  2. Download and install wkhtmltopdf:
    $ wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb
    $ sudo dpkg -i wkhtmltox_0.12.6.1-2.jammy_amd64.deb

    If you encounter any errors after running the 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 -i -u postgres createuser -s  $(whoami)
    $ sudo su postgres
    $ psql
    alter user $(whoami) with password 'your_password';
    \q
    exit
  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 17.0 --single-branch --depth 1 https://github.com/odoo/odoo.git
  7. Create an odoo-17.0 virtual environment and activate it:
    $ python3 -m venv ~/venv-odoo-17.0
    $ source ~/venv-odoo-17.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 by running 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 17.0 needs version 0.12.6.1 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.

After this, we configured PostgreSQL, which is used for Odoo’s database management.

PostgreSQL configuration

In step 3, we installed the PostgreSQL database.

In step 4, we created a new database user with the login name of our system. $(whoami) is used to fetch your login name, and the -s option is used to give superuser 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. Note that you can fetch the latest changes with the git pull command.

In step 5, we configured our git user.

In step 6, we downloaded the source code from Odoo’s official GitHub repository. We used the git clone command to download Odoo’s source code. We used a single branch as we only wanted a branch for the 17.0 version. We also 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 in a separate folder under the ~/odoo-dev directory.

Virtual environments

Python virtual environments, or venvs 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.10 subdirectory.

In step 7, we created a new virtual environment in the ~/venv-odoo-17.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. We used the source ~/venv-odoo-17.0/bin/activate command to activate 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 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:

./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 is 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 create the database structure that’s needed to support its operations. It will also scan the add-on 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 because Odoo includes an HTTP server. By default, it listens on all local network interfaces on TCP port 8069.

You have been reading a chapter from
Odoo Development Cookbook - Fifth Edition
Published in: May 2024
Publisher: Packt
ISBN-13: 9781805124276
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 $19.99/month. Cancel anytime
Banner background image