29,99 €
With its latest iteration, the powerful Odoo framework released a wide variety of features for rapid application development. This updated Odoo development cookbook will help you explore the new features in Odoo 14 and learn how to use them to develop Odoo applications from scratch. You'll learn about the new website concepts in Odoo 14 and get a glimpse of Odoo's new web-client framework, the Odoo Web Library (OWL).
Once you've completed the installation, you'll begin to explore the Odoo framework with real-world examples. You'll then create a new Odoo module from the ground up and progress to advanced framework concepts. You'll also learn how to modify existing applications, including Point of Sale (POS) applications. This book isn't just limited to backend development; you'll discover advanced JavaScript recipes for creating new views and widgets. As you progress, you'll learn about website development and become a quality Odoo developer by studying performance optimization, debugging, and automated testing. Finally, you'll delve into advanced concepts such as multi-website, In-App Purchasing (IAP), Odoo.sh, the IoT Box, and security.
By the end of the book, you'll have all the knowledge you need to build impressive Odoo applications and you'll be well versed in development best practices that will come in handy when working with the Odoo framework.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 875
Veröffentlichungsjahr: 2020
Rapidly build, customize, and manage secure and efficient business apps using Odoo's latest features
Parth Gajjar
Alexandre Fayolle
Holger Brunn
Daniel Reis
BIRMINGHAM—MUMBAI
Copyright © 2020 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Group Product Manager: Aaron Lazar
Associate Publishing Product Manager: Shweta Bairoliya
Senior Editor: Rohit Singh
Content Development Editor: Dwayne Fernandes, Tiksha Lad
Technical Editor: Pradeep Sahu
Copy Editor: Safis Editing
Project Coordinator: Deeksha Thakkar
Proofreader: Safis Editing
Indexer: Pratik Shirodkar
Production Designer: Prashant Ghare
First published: April 2016
Second edition: January 2018
Third edition: April 2019
Fourth edition: December 2020
Production reference: 2150221
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80020-031-9
www.packt.com
I dedicate this book to my parents with love and gratitude. Thank you for giving me the freedom and support to follow my ambitions throughout my childhood.
– Parth Gajjar
Packt.com
Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at packt.com and, as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Parth Gajjar is an Odoo expert with a deep understanding of the Odoo framework. He started his career at Odoo and spent 7 years in the R&D department at Odoo India. During his time at Odoo, he worked on several key features, including a marketing automation app, mobile application, report engine, domain builder, and more. He also worked as a code reviewer and helped manage the code quality of the new features. Later, he started his own venture named Droggol and now provides various development services related to Odoo. He loves working on Odoo and solving real-world business problems with different technologies. He often gives technical training to Odoo developers.
I would like to thank my parents and family members for all of the support they have given throughout the writing of this book.
Alexandre Fayolle started working with Linux and free software in the mid-1990s and quickly became interested in the Python programming language. In 2012, he joined Camptocamp to share his expertise on Python, PostgreSQL, and Linux with the team implementing Odoo. He currently manages projects for Camptocamp and is strongly involved in the Odoo Community Association. In his spare time, he likes to play jazz on the vibraphone.
Holger Brunn has been a fervent open source advocate since he came into contact with the open source market sometime in the nineties.
He has programmed for ERP and similar systems in different positions since 2001. For the last 10 years, he has dedicated his time to TinyERP, which became OpenERP and evolved into Odoo. Currently, he works at Therp BV in the Netherlands as a developer and is an active member of the Odoo Community Association.
Daniel Reis has had a long career in the IT industry, largely as a consultant implementing business applications in a variety of sectors, and today works for Securitas, a multinational security services provider.
He has been working with Odoo (formerly OpenERP) since 2010, is an active contributor to the Odoo Community Association projects, is currently a member of the board of the Odoo Community Association, and collaborates with ThinkOpen Solutions, a leading Portuguese Odoo integrator.
Kishan Gajjar is a lead developer at Droggol and has been working on Odoo for the last 4 years. Previously, he worked at the Indian branch of Odoo for 3 years in the R&D department. During his time at Odoo, he mostly worked on the website builder, themes, and the backend JavaScript framework. Later, he established Droggol as one of the co-founders. He has expertise in website and theme development and loves creating beautiful and eye-catching websites in Odoo. Aside from development, he loves playing video games.
To begin with, I would like to thank my parents for allowing me to follow my ambitions throughout my childhood, and also my brother, who encourages me in his own particular way. He has been my inspiration and motivation.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
There are several ways to set up an Odoo development environment. This chapter proposes one of them; you will certainly find a number of other tutorials on the web explaining other approaches. Keep in mind that this chapter is about a development environment that has different requirements from a production environment.
If you are new to Odoo development, you must know about certain aspects of the Odoo ecosystem. The first recipe will give you a brief introduction to the Odoo ecosystem, and then we will move on to the installation of Odoo for development.
In this chapter, we will cover the following recipes:
Understanding the Odoo ecosystemEasy installation of Odoo from sourceManaging Odoo server databasesStoring the instance configuration in a fileActivating Odoo developer toolsUpdating the add-on modules listOdoo provides the developer with out-of-the-box modularity. Its powerful framework helps the developer to build projects very quickly. There are various characters in the Odoo ecosystem that you should be familiar with before embarking on your journey of becoming a successful Odoo developer.
Odoo comes with two different editions. The first one is the Community Edition, which is open source, and the second one is the Enterprise Edition, which has licensing fees. Unlike other software vendors, Odoo Enterprise Edition is just a pack of extra applications that adds extra features or new apps to the Community Edition. Basically, the Enterprise Edition runs on top of the Community Edition. The Community Edition comes under the Lesser General Public License v3.0 (LGPLv3) license and comes with all of the basic Enterprise Resource Planning (ERP) applications, such as sales, Customer Relationship Management (CRM), invoicing, purchases, and website builder. Alternatively, the Enterprise Edition comes with the Odoo Enterprise Edition License, which is a proprietary license. Odoo Enterprise Edition has a number of advanced features, such as full accounting, studio, Voice over Internet Protocol (VoIP), mobile responsive design, e-sign, marketing automation, delivery and banking integrations, IoT, and more. The Enterprise Edition also provides you with unlimited bugfix support. The following diagram shows that the Enterprise Edition depends on the Community Edition, which is why you need the latter in order to use the former:
Figure 1.1 – Differences between the Community and Enterprise Editions
You can see a full comparison of both editions here: https://www.odoo.com/page/editions.
Note
Odoo has the largest number of community developers, which is why you will find a large number of third-party apps (modules) on the app store. Some of the free apps use an Affero General Public License version 3 (AGPLv3). You cannot use the proprietary license on your app if your application has dependencies on such apps. Apps with an Odoo proprietary license can be developed only on modules that have LGPL or other proprietary licenses.
The entire code base of Odoo is hosted on GitHub. You can post bugs/issues for stable versions here. You can also propose a new feature by submitting Pull Requests (PR). There are several repositories in Odoo. See the following table for more information:
Table 1.1
Every year, Odoo releases one major (Long-Term Support (LTS)) version and a few minor versions. Minor versions are mostly used in Odoo's online SaaS service, meaning that Odoo SaaS users get early access to these features. Major version branches have names such as 14.0, 13.0, and 12.0, while minor version branches have names such as saas-14.1, and saas-14.2 on GitHub. These minor versions are mostly used for Odoo's SaaS platform. The master branch is under development and is unstable, so it is advisable not to use this for production since it might break down your database.
Runbot is Odoo's automated testing environment. Whenever there is a new commit in Odoo's GitHub branch, Runbot pulls those latest changes and creates the builds for the last four commits. Here, you can test all stable and in-development branches. You can even play with the Enterprise Edition and its development branches.
Every build has a different background color, which indicates the status of the test cases. A green background color means that all of the test cases run successfully and you can test that branch, while a red background color means that some test cases have failed on this branch and some features might be broken on that build. You can view the logs for all test cases, which show exactly what happens during installation. Every build has two databases. The all database has all of the modules installed on it, while the base database only has base Odoo modules installed. Every build is installed with basic demo data, and therefore you can test it quickly without extra configurations.
Note
You can access Runbot from the following URL: http://runbot.odoo.com/runbot.
The following credentials can be used to access any Runbot build:
Login ID: admin Password: adminLogin ID: demo Password: demoLogin ID: portal Password: portalNote
This is a public testing environment, so sometimes it is possible that other users are using/testing the same branch that you are testing.
Odoo launched the app store a few years back, and this was an instant hit. Right now, there are over 22,000+ different apps hosted there. In the app store, you will find lots of free and paid applications for different versions. This includes specific solutions for different business verticals, such as education, food industries, and medicine. It also includes apps that extend or add new features to existing Odoo applications. The app store also provides numerous beautiful themes for the Odoo website builder. In Chapter 3, Creating Odoo Add-On Modules, we will look at how you can set pricing and currency for your custom module.
You can access the Odoo app store via the following URL: https://www.odoo.com/apps.
You can access the Odoo themes via the following URL: https://www.odoo.com/apps/themes.
Note
Odoo has open sourced several themes with versions 13 and 14. Note that these were paid themes in previous versions. This means that, in Odoo versions 13 and 14, you can download and use those beautiful themes at no extra cost.
Odoo Community Association (OCA) is a non-profit organization that develops/manages community-based Odoo modules. All OCA modules are open source and maintained by Odoo community members. Under the OCA's GitHub account, you will find multiple repositories for different Odoo applications. Apart from Odoo modules, it also contains various tools, a migration library, accounting localizations, and so on.
Here is the URL for OCA's official GitHub account: https://github.com/OCA.
Odoo has a very powerful framework, and tons of things can be achieved just by using/activating options or by following specific patterns. Consequently, if you run into some technical issues or if you are not sure about some complex cases, then you can post your query on Odoo's official help forum. Lots of developers are active on this forum, including some official Odoo employees.
You can search your questions or post your new questions at the following URL: https://help.odoo.com.help.odoo.com.
Recently, Odoo has launched a new eLearning platform. This platform has lots of videos that explain how to use different Odoo applications. At the time of writing this book, this platform does not have technical videos, just functional ones.
Here is the URL for Odoo's eLearning platform: https://www.odoo.com/slides.
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.
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.
To install Odoo from source, perform the following steps:
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
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
Now, install the PostgreSQL database:$ sudo apt install postgresql -y
Configure PostgreSQL:$ sudo -u postgres createuser --superuser $(whoami)
Configure git:$ git config --global user.name "Your Name"
$ git config --global user.email [email protected]
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
Create an odoo-14.0 virtual environment and activate it:$ python3 -m venv ~/venv-odoo-14.0
$ source ~/venv-odoo-14.0/bin/activate
Install the Python dependencies of Odoo in venv:$ cd ~/odoo-dev/odoo/
$ pip3 install -r requirements.txt
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$
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
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.
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.
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.
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.
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.
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
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.
When working with Odoo, all the data in your instance is stored in a PostgreSQL database. All the standard database management tools you are used to are available, but Odoo also proposes a web interface for some common operations.
We are assuming that your work environment is set up and that you have an instance running.
The Odoo database management interface provides tools to create, duplicate, remove, back up, and restore a database. There is also a way to change the master password, which is used to protect access to the database management interface.
To access the database, perform the following steps:
Go to the login screen of your instance (if you are authenticated, log out).Click on the Manage Databases link. This will navigate to http://localhost:8069/web/database/manager (you can also point your browser directly to that URL):Figure 1.3 – Database manager
If you've set up your instance with default values and haven't modified it yet, as we will explain in the following section, the database management screen will display a warning, telling you that the master password hasn't been set and will advise you to set one with a direct link:
Figure 1.4 – Master password warning
To set the master password, perform the following steps:
Click on the Set Master Password button. You will get a dialog box asking you to fill in the New Master Password field:Figure 1.5 – Setting a new master password dialog
Type in a non-straightforward new password and click Continue.If the master password is already set, click on the Set Master Password button at the bottom of the screen to change it. In the displayed dialog box, type the previous master password and the new one and then click on Continue.
Note
The master password is the server configuration file under the admin_password key. If the server was started without specifying a configuration file, a new one will be generated in ~/.odoorc. Refer to the next recipe for more information about the configuration file.
