Odoo 11 Development Cookbook - Second Edition - Holger Brunn - E-Book

Odoo 11 Development Cookbook - Second Edition E-Book

Holger Brunn

0,0
40,79 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Create fast and efficient server-side applications using the latest features of Odoo v11

Key Features

  • Get the most up-to-date guide on Odoo 11 to create custom and reusable modules
  • Interconnect your application with other systems by implementing web APIs
  • Understand the mechanisms powering the Odoo framework to build robust enterprises

Book Description

Odoo is a full-featured open source ERP with a focus on extensibility. The flexibility and sustainability of open source are 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. Version 11 offers better usability and speed: a new design (as compared to the current Odoo Enterprise version) and a mobile interface.

The book starts by covering Odoo installation and administration and Odoo Server deployment. It then delves into the implementation of Odoo modules, the different inheritance models available in Odoo. You will then learn how to define access rules for your data; how to make your application available in different languages; how to expose your data models to end users on the back end and on the front end; and how to create beautiful PDF versions of your data.

By the end of the book, you will have a thorough knowledge of Odoo and will be able to build effective applications by applying Odoo development best practices

What you will learn

  • Install and manage Odoo environments and instances
  • Use models to define your application's data structures
  • Add business logic to your applications
  • Add automated tests and learn how to debug Odoo apps
  • Learn about the access security model and internationalization features
  • Customize websites built with Odoo, by writing your own templates and providing new snippets for use in the website builder
  • Extend the web client with new widgets and make RPC calls to the server

Who this book is for

If you're a Python developer and want to develop highly efficient business applications with the latest Odoo framework (or if you just want a hands on problem solution book for all your Odoo Development related issues), this book is for you! Some experience with the JavaScript programming language and web development is required to get the most out 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 (OCA).

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 546

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Odoo 11 Development CookbookSecond Edition

 

 

Over 120 unique recipes to build effective enterprise and business applications

 

 

 

 

 

Alexandre Fayolle
Holger Brunn

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Odoo 11 Development Cookbook Second Edition

Copyright © 2018 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.

Commissioning Editor: Aaron LazarAcquisition Editor: Akshay GhadiContent Development Editor: Francis CarneiroTechnical Editor: Akhil NairCopy Editor: Shaila KusanaleProject Coordinator: Devanshi DoshiProofreader: Safis EditingIndexer: Tejal Daruwale SoniGraphics: Jason MonteiroProduction Coordinator: Deepika Naik

First published: April 2016 Second edition: January 2018

Production reference: 1180118

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78847-181-7

>www.packtpub.com

mapt.io

Mapt is an online digital library that gives you full access to over 5,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.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

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 www.PacktPub.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.PacktPub.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.

Contributors

About the authors

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.

I'd like to thank Daniel Reis for his work on the first edition of this book, and Nadège Gauffre for her patience and understanding while I was working on the Cookbook. 

 

 

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 (OCA).

About the reviewer

Yannick Vaucher is a strong believer in FOSS. Starting in 2011, he is employed by Camptocamp as an Odoo developer. Besides developing, he also teaches time-to-time technical Odoo formation to developers.

He joined the Odoo Community Association (OCA) as a core contributor and manages the Switzerland localization and Geospatial OCA projects. You may find multiple contributions to the Odoo module ecosystem have been made by him.

 

 

 

 

 

Packt is searching for authors like you

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.

Table of Contents

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Conventions used

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Get in touch

Reviews

Installing the Odoo Development Environment

Introduction

Easy installation of Odoo from source

Getting ready

How to do it...

How it works...

Virtual environments

PostgreSQL configuration

Git configuration

Downloading the Odoo source code

Starting the instance

There's more…

Managing Odoo environments using the start command

Getting ready

How to do it...

How it works...

There's more…

Managing Odoo server databases

Getting ready

How to do it...

Accessing the database management interface

Setting or changing the master password

Creating a new database

Duplicating a database

Removing a database

Backing up a database

Restoring a database backup

How it works...

There's more...

Storing the instance configuration in a file

How to do it...

How it works...

Activating the Odoo developer tools

How to do it...

How it works...

Updating Odoo from source

Getting ready

How to do it...

How it works...

Managing Odoo Server Instances

Introduction

Configuring the addons path

Getting ready

How to do it…

How it works…

There's more…

Updating the addon modules list

Getting ready

How to do it…

How it works…

Standardizing your instance directory layout

How to do it…

How it works…

See also

Installing and upgrading local addon modules

Getting ready

How to do it…

From the web interface

From the command line

How it works…

Addon installation

Addon update

There's more…

Installing addon modules from GitHub

Getting ready

How to do it…

How it works…

There's more…

Applying changes to addons

Getting ready

How to do it…

How it works…

See also

Applying and trying proposed pull requests

Getting ready

How to do it…

How it works…

There's more…

Server Deployment

Introduction

Installing Odoo for production use

Getting ready

How to do it...

How it works...

There's more...

Server dimensioning

PostgreSQL tuning

Source code version

Backups

See also

Adapting the configuration file for production

Getting ready

How to do it...

How it works...

There's more...

Setting up Odoo as a system service

Getting ready

How to do it...

How it works...

There's more...

Configuring a reverse proxy and SSL with nginx and Let's Encrypt

Getting ready

How to do it...

How it works...

There's more...

See also

Using buildout for repeatable builds

Getting ready

How to do it...

How it works...

There's more...

Temporary merges

Freezing a buildout

See also

Using Docker to run Odoo

Getting ready

How to do it…

Building a Docker image

Running Odoo in a container

How it works…

There's more…

Creating Odoo Addon Modules

Introduction

Creating and installing a new addon module

Getting ready

How to do it...

How it works...

Completing the addon module manifest

Getting ready

How to do it...

How it works...

There's more…

Organizing the addon module file structure

Getting ready

How to do it...

How it works...

Adding models

Getting ready

How to do it...

How it works...

Adding Menu Items and Views

Getting ready

How to do it...

How it works...

Adding Access Security

Getting ready

How to do it...

How it works…

Using scaffold to create a module

Getting ready

How to do it...

How it works...

Application Models

Introduction

Defining the Model representation and order

Getting ready

How to do it...

How it works...

There's more...

Adding data fields to a Model

Getting ready

How to do it...

How it works...

There's more...

Using a float field with configurable precision

Getting ready

How to do it...

How it works...

Adding a monetary field to a Model

Getting ready

How to do it...

How it works...

Adding relational fields to a Model

Getting ready

How to do it...

How it works...

There's more...

Adding a hierarchy to a Model

Getting ready

How to do it...

How it works...

There's more...

Adding constraint validations to a Model

Getting ready

How to do it...

How it works...

Adding computed fields to a Model

Getting ready

How to do it...

How it works...

Exposing related fields stored in other Models

Getting ready

How to do it...

How it works...

There's more...

Adding dynamic relations using Reference fields

Getting ready

How to do it...

How it works...

Adding features to a Model using inheritance

Getting ready

How to do it...

How it works...

There's more...

Using abstract Models for reusable Model features

Getting ready

How to do it...

How it works...

There's more...

Using delegation inheritance to copy features to another Model

Getting ready

How to do it...

How it works...

There's more...

Basic Server-Side Development

Introduction

Defining model methods and using the API decorators

Getting ready

How to do it...

How it works...

There's more...

See also

Reporting errors to the user

Getting ready

How to do it...

How it works...

There's more...

Obtaining an empty recordset for a different model

Getting ready

How to do it...

How it works...

See also

Creating new records

Getting ready

How to do it...

How it works...

There's more…

Updating values of recordset records

Getting ready

How to do it...

How it works...

There's more...

Searching for records

Getting ready

How to do it...

How it works...

There's more...

Combining recordsets

Getting ready

How to do it...

How it works...

There's more...

Filtering recordsets

Getting ready

How to do it...

How it works...

There's more...

Traversing recordset relations

Getting ready

How to do it...

How it works...

There's more...

See also

Extending the business logic defined in a Model

Getting ready

How to do it...

How it works...

There's more...

Extending write() and create()

Getting ready

How to do it...

How it works...

There's more...

Customizing how records are searched

Getting ready

How to do it...

How it works...

There's more...

See also

Module Data

Introduction

Using external IDs and namespaces

How to do it...

How it works...

There's more...

See also

Loading data using XML files

How to do it...

How it works...

There's more...

Using the noupdate and forcecreate flags

How to do it...

How it works...

There's more...

See also

Loading data using CSV files

How to do it...

How it works...

There's more...

Loading data using YAML files

How to do it...

How it works...

There's more...

See also

Addon updates and data migration

How to do it...

How it works...

There's more...

See also

Debugging and Automated Testing

Introduction

Producing server logs to help debug methods

Getting ready

How to do it...

How it works...

There's more...

See also

Using the Odoo shell to interactively call methods

Getting ready

How to do it...

How it works...

Using the Python debugger to trace method execution

Getting ready

How to do it...

How it works...

There's more...

See also

Writing tests for your module using Python unit tests

Getting ready

How to do it...

How it works...

There's more...

Running server tests

Getting ready

How to do it...

How it works...

There's more...

Using the Odoo Community Association maintainer quality tools

Getting ready

How to do it...

How it works...

There's more...

Using Pylint to check your code

Using Flake8 to check your code

Advanced Server-Side Development Techniques

Introduction

Changing the user performing an action

Getting ready

How to do it...

How it works...

There's more...

See also

Calling a method with a modified context

Getting ready

How to do it...

How it works...

There's more...

See also

Executing raw SQL queries

Getting ready

How to do it...

How it works...

There's more...

See also

Writing a wizard to guide the user

Getting ready

How to do it...

How it works...

There's more...

Using the context to compute default values

Wizards and code reuse

Redirecting the user

Defining onchange methods

Getting ready

How to do it...

How it works...

There's more...

Calling onchange methods on the server side

Getting ready

How to do it...

How it works...

There's more...

See also

Defining a model based on a SQL view

Getting ready

How to do it...

How it works...

There's more...

Backend Views

Introduction

Adding a menu item and window action

How to do it...

How it works...

There's more...

See also

Having an action open a specific view

How to do it...

How it works...

ir.actions.act_window.view

Adding content and widgets to a form view

How to do it...

How it works...

Form

Header

Button

Group

Field

General attributes

Other tags

There's more...

See also

Adding buttons to forms

How to do it...

How it works...

There's more...

Passing parameters to forms and actions – Context

Getting ready

How to do it...

How it works...

There's more...

See also

Defining filters on record lists – Domain

How to do it...

How it works...

There's more...

Operators

Pitfalls

See also

List views

How to do it...

How it works...

There's more...

Search views

How to do it...

How it works...

There's more...

See also

Changing existing views – View inheritance

How to do it...

How it works...

There's more...

Order of evaluation in view inheritance

See also

Document-style forms

How to do it...

How it works...

Dynamic form elements using attrs

How to do it...

How it works...

There's more...

Embedded views

How to do it...

How it works...

There's more...

Kanban views

How to do it...

How it works...

There's more...

Showing kanban cards in columns according to their state

Getting ready

How to do it...

How it works...

There's more...

Calendar and gantt views

How to do it...

How it works...

There's more...

Graph and pivot views

Getting ready

How to do it...

How it works...

There's more...

Access Security

Creating security groups and assigning them to users

Getting ready

How to do it...

How it works...

There's more...

Adding security access to models

Getting ready

How to do it...

How it works...

There's more...

Limiting access to fields in models

How to do it...

How it works...

There's more...

Limiting record access using record rules

Getting ready

How to do it...

How it works...

There's more...

Using security groups to activate features

Getting ready

How to do it...

How it works...

There's more...

Internationalization

Installing a language and configuring user preferences

Getting ready

How to do it...

How it works...

There's more...

Configuring language-related settings

Getting ready

How to do it...

How it works...

There's more...

Translating texts through the web client user interface

Getting ready

How to do it...

How it works...

There's more...

Exporting translation strings to a file

Getting ready

How to do it...

How it works...

There's more...

Using gettext tools to ease translations

How to do it...

How it works...

There's more...

Importing translation files into Odoo

Getting ready

How to do it...

How it works...

Automation, Workflows, Emails, and Printouts

Introduction

Using Kanban stages and features

Getting ready

How to do it...

How it works...

There's more...

Creating server actions

Getting ready

How to do it...

How it works...

There's more...

Using Python code server actions

Getting ready

How to do it...

How it works...

There's more...

Using automated actions on time conditions

Getting ready

How to do it...

How it works...

There's more...

Using automated actions on event conditions

Getting ready

How to do it...

How it works...

There's more...

Adding messaging and tracking features

Getting ready

How to do it...

How it works...

There's more...

Email templates

Getting ready

How to do it...

How it works...

There's more…

See also

QWeb-based PDF reports

Getting ready

How to do it...

How it works...

There's more...

Producing LibreOffice-based reports with Py3O

Getting ready

Getting the report_py3o module in Odoo

Installing and running a py3o.fusion server

How to do it...

How it works...

Placeholders

Control structures

There's more...

Web Server Development

Introduction

Making a path accessible from the network

Getting ready

How to do it...

How it works...

odoo.http.route

Return values

odoo.http.request

There's more...

See also

Restricting access to web accessible paths

Getting ready

How to do it...

How it works...

There's more...

Consuming parameters passed to your handlers

How to do it...

How it works...

There's more...

See also

Modifying an existing handler

Getting ready

How to do it...

How it works…

There's more...

See also

Using the RPC API

How to do it...

How it works...

XMLRPC

JSONRPC

There's more...

See also

Web Client Development

Introduction

Creating custom widgets

Getting ready

How to do it...

How it works...

There’s more...

See also

Using client-side QWeb templates

Getting ready

How to do it...

How it works...

There’s more...

See also

Making RPC calls to the server

Getting ready

How to do it...

How it works...

There’s more...

See also

Writing tests for client-side code

Getting ready

How to do it...

How it works...

There’s more...

See also

Debugging your client-side code

Getting ready

How to do it...

How it works...

There’s more...

CMS Website Development

Introduction

Extending CSS and JavaScript for the website

Getting ready

How to do it...

How it works...

There's more...

Creating or modifying templates – QWeb

Getting ready

How to do it...

How it works...

Loops

Attributes

Fields

Conditionals

Inline editing

There's more...

See also

Offering snippets to the user

Getting ready

How to do it...

How it works...

There's more...

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

Odoo, formerly known as OpenERP, is a great platform for developers. The framework, at its core, is very rich and allows building client-server applications from scratch, as well as adapting existing applications to your needs through a clever extension mechanism and a very modular design. The latest versions have brought a wealth of new possibilities with the addition of a full-featured website development stack. The scope is huge, and it is easy for newcomers to feel lost.

For years, Odoo developers have been learning their craft by reading the code of addon modules, which are built on top of the framework to provide enterprise management features.

While effective, this process is long and error-prone, since it is difficult to know whether the source code you are learning from uses the latest possibilities offered by the framework, or if you are looking at an older module that has not been updated to use these features. To make things worse, some code flows are intrinsically hard to follow because they're partly in the business logic layer, partly in the database layer, partly in the request handling layer, and partly in the client-side code.

This book is meant to save you time by tapping into the years of experience accumulated by long-time Odoo contributors to learn the current best practices in Odoo development by focusing on the new features of version 11, and also giving a solid base in the existing mature functionality of the framework. Since Odoo has a long tradition of guaranteeing backward compatibility, most of the presented material should still work with upcoming versions.

Who this book is for

If you are a Python developer who wants to develop highly efficient business applications with the latest Odoo framework and want a hands-on problem/solution book for all of your Odoo development-related issues, this book is for you! Some experience with the JavaScript programming language and web development is assumed to make the most of this book.

What this book covers

Chapter 1, Installing the Odoo Development Environment, begins with how to create a development environment for Odoo, start Odoo, create a configuration file, and activate the developer tools of Odoo.

Chapter 2, Managing Odoo Server Instances, is about addon installation and upgrading. It provides useful tips for working with addons installed from GitHub and organizing the source code of your instance.

Chapter 3, Server Deployment, provides advice on how to install and configure Odoo for production, including setting up a reverse proxy to encrypt network communications over HTTPS and ensuring that Odoo starts when the server boots.

Chapter 4, Creating Odoo addon Modules, explains the structure of an Odoo addon module and provides a step-by-step guide for creating a simple module from scratch.

Chapter 5, Application Models, focuses on Odoo model descriptions, and explains the various field types and the different inheritance models available in Odoo.

Chapter 6, Basic Server-Side Development, introduces the API of Odoo, presents the commonly used methods of the Model class, and explains how to write business logic methods. More advanced topics are covered in Chapter 9, Advanced Server-Side Development.

Chapter 7, Module Data, shows how to ship data along with the code of your module. It also explains how to write a migration script when a data model provided by an addon is modified in a new release.

Chapter 8, Debugging and Automated Testing, proposes some strategies for server-side debugging and provides an introduction to the Python debugger. It also explains how to write and run automated tests using YAML or Python for your addon modules. Client-side testing is covered in Chapter 15, Web Client Development. 

Chapter 9, Advanced Server-Side Development Techniques, outlines more advanced topics useful when writing business methods, such as writing wizards to walk the user through a process, writing onchange methods, manipulating the execution context or bypassing the ORM to use raw SQL. 

Chapter 10, Backend Views, teaches you how to write business views for your data models and how to call server-side methods from these views. It covers the usual views (list view, form view, and search view) as well as the views introduced in recent versions of Odoo (kanban, graph, calendar, pivot, and so on).

Chapter 11, Access Security, discusses how to control who has access to what in your Odoo instance by creating security groups, writing access control lists to define what operations are available to each group on a given model, and if necessary by writing record-level rules.

Chapter 12, Internationalization, takes you through the translation of the user interfaces of your addons and provides useful tricks about using GNU Gettext command line tools to manipulate translation files.

Chapter 13, Automation, Workflows, Emails, and Printouts, illustrates the different tools available in Odoo to implement business processes for your records. It also shows how server actions and automated rules can be used to support business rules, and how to generate emails and PDF documents with Odoo. 

Chapter 14, Web Server Development, deals with the core of the web server in Odoo. It explains how to map URLs to methods and how to control who can access these URLs.

Chapter 15, Web Client Development, dives into the JavaScript part of Odoo and explains how you can provide new widgets and make RPC calls to the server. It also gives tips about debugging and testing this part of your code.

Chapter 16, CMS Website Development, shows how to customize websites built with Odoo, by writing your own templates and providing new snippets for use in the website builder.

 

To get the most out of this book

This book is meant for developers, and it is not a development tutorial. It expects you to be familiar with the Python programming language (especially in the backend development chapters), the JavaScript programming language (especially in the front end development chapters). Since the data files in the modules are often formatted as XML files, some knowledge of the XML syntactic rule is definitely welcome. 

Odoo will run without problem on any recent GNU/Linux distribution. In order to simplify the code in the recipes, we chose to  Debian GNU/Linux 9 (code name Stretch), so it will also help if you had some previous acquaintance with the apt-get command used to install software packages, and some notions about GNU/Linux system administration. 

Download the example code files

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

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

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

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Odoo-11-Development-Coobook-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

 

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

Installing the Odoo Development Environment

In this chapter, we will cover the following topics:

Easy installation of Odoo from source

Managing Odoo environments using the

start

command

Managing Odoo server databases

Storing the instance configuration in a file

Activating the Odoo developer tools

Updating Odoo from source

Introduction

There are lots of ways to set up an Odoo development environment. This chapter proposes one of these, although 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 requirements different from a production environment, which will be covered inChapter 3, Server Deployment.

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 occurring on GNU/Linux than on Windows.

It is also recommended to develop 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 some library has a different version than is expected, with 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 install a GNU/Linux distribution in the VM.

To avoid copying files between your workstation where you are running your development environment and the virtual machine that 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 that you are running Debian GNU/Linux as its stable version (this is version 9, code name Stretch 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 becausesudohas been configured. In the following pages, we will use $(whoami)whenever the login of your work user 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. Go tohttps://github.comand create one if you don't have one already.

How to do it...

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

Run the following commands to install the main dependencies:

$ sudo apt-get update$ sudo apt-get install -y git python3.5 postgresql nano virtualenv \ xz-utils wget fontconfig libfreetype6 libx11-6 libxext6 libxrender1 \ node-less node-clean-css xfonts-75dpi

Download and install

wkhtmltopdf

:

$ wget -O wkhtmltox.tar.xz \ https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz

$ tar xvf wkhtmltox.tar.xz

$ sudo mv wkhtmltox/lib/* /usr/local/lib/

$ sudo mv wkhtmltox/bin/* /usr/local/bin/

$ sudo mv wkhtmltox/share/man/man1 /usr/local/share/man/

Now, use this to install the build dependencies:

$ sudo apt-get install -y gcc python3.5-dev libxml2-dev \libxslt1-dev libevent-dev libsasl2-dev libssl1.0-dev libldap2-dev \libpq-dev libpng-dev libjpeg-dev

Configure

PostgreSQL

:

$ sudo -u postgres createuser --createdb $(whoami)$ createdb $(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 11.0 --single-branch\ https://github.com/odoo/odoo.git$ cd odoo

Create an

odoo-11.0

virtual environment

and activate it:

$ virtualenv -p python3 ~/odoo-11.0$ source ~/odoo-11.0/bin/activate

Install the Python dependencies of Odoo in

virtualenv

:

$ pip3 install -r requirements.txt

Create and start your first Odoo instances:

$ createdb odoo-test$ python3 odoo-bin -d odoo-test --addons-path=addons \ --db-filter=odoo-test$

Point your browser to

http://localhost:8069

and authenticate it using the

admin

account and

admin

as password.

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 emailed to you directly. You can download the code files by following these steps:
Log in or register to our website using your email address and passwordHover the mouse pointer on the SUPPORT tab at the topClick on Code Downloads & ErrataEnter the name of the book in the Search boxSelect the book for which you're looking to download the code filesChoose where you purchased this book from in the drop-down menuClick 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. Note that you need to be logged in to your Packt account.

Once the file is downloaded, ensure that you unzip or extract the folder using the latest version of the following:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux

How it works...

Dependenciescome 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 gettingthe source code of Odoo itself.

Versions of Odoo prior to 11.0 ran with Python 2.7. Starting with Odoo 11.0, the minimum supported version of Python is 3.5. These two versions of Python are not compatible, so a module running on Python 2.7 (with Odoo 9.0 or 10.0, for instance) will require both porting to the specifics of Odoo 11.0 and porting to Python 3. 

Since we will need to edit some files asrootor aspostgres(the PostgreSQL administrative user) on our server, we need to install a console-based text editor. We suggestnanoas 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 asvim,e3,oremacs-nox.

Wkhtmltopdfis a runtime dependency of Odoo used to produce PDF reports. The version required by Odoo 11.0 is 0.12.4, which is not included in the current GNU/Linux distributions. Fortunately for us, the maintainers of wkhtmltopdf provide pre-built packages for various distributions onhttp://wkhtmltopdf.org/downloads.html.

There are lots of other runtime dependencies that are Python modules, which we can install usingpip3in 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. Therefore, we 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 usepip3 install -r requirements.txt(a file that comes from the Odoo source code distribution) to download, compile, and install the Python modules.

Virtual environments

Python virtual environments, orvirtualenvfor short, are isolated Python workspaces. They 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 thevirtualenv -p python3 path/to/newenvcommand. This will create anewenvdirectory in the specified location, containing abin/ sub-directory and alib/python3.5subdirectory. Don't forget -p python3, or you are likely to get a python 2.7 virtual environment that will not be able to run Odoo 11.0.

Inbin/, you will find several scripts:

activate

: The script is not executed; it is sourced using the shell built-in

source

command. 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.

pip3

: This is a special version of the

pip3

command that acts inside the

virtualenv

only.

python3

: This is a wrapper around your system Python interpreter that uses the packages installed in the

virtualenv

.

The shell built-in source command 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 avirtualenv. You may activate it as we show in the recipe (and calldeactivatewhen you're done), or you may use the scripts in thebin/directory of the environment explicitly by calling them with their full path, in which case you don't need to activate thevirtualenv. 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 python3

These will be easier to use with an activatedvirtualenv.

This is the case with theodoo-binscript, which you can call in the following way:

$ ./odoo-bin -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 ofpsycopg2that the Python module used to access aPostgreSQLdatabase:

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 thepostgresadministrative user to create a database user who shares our login name and gives it the right to create new databases. We then create a new database with the same name as the new user, whichwill be used as a default database when using thepsqlcommand.

When on a development server, it is okay to give the PostgreSQL user more rights and to use the--superusercommand-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 that--superuseris too much, you may still want to use--createrolein addition to--createdbwhen creating your database user. Avoid doing this on production servers as it will give additional leverage to an attacker exploiting a vulnerability in some part of the deployed code (refer toChapter 3, 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--pwpromptflag 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 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 usegit 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.

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 email for the merging to succeed.

Downloading the Odoo source code

Downloading the Odoo code base is done by performing agit cloneoperation; be patient as this will take some time. The--branch 11.0 --single-branch options avoid downloading other branches and save a little time. The--depthoption 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.

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 theodoo-binscript 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 addon 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 of the available options, use the--helpargument. We will see much more about theodoo-binscript in this chapter as well as inChapter 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 into the initial records in the database. This includes theadminuser with the default passwordadmin, which you will use to authenticate with.

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

There's more…

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

$ git clone -b 11.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. TheOdoo 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 11.0 --single-branch https://github.com/OCA/OCB.git odoo

Managing Odoo environments using the start command

We will often want to use custom or community modules with our Odoo instance. Keeping them in a separate directory makes it easier to install upgrades to Odoo or troubleshoot issues from our custom modules. We just have to add that directory to the addons path, and they will be available in our instance, just like the core modules are.

It is possible to think about this module directory as an Odoo environment. The Odoostartcommand makes it easy to organize Odoo instances as directories, each with its own modules.

Getting ready

For this recipe, we need to have already installed Odoo. We assume that it will be at ~/odoo-dev/odoo, and that thevirtualenvis activated.

This means that the following command should successfully start an Odoo server:

$ ~/odoo-dev/odoo/odoo-bin

How to do it...

To create a work environment for your instance, you need to follow these steps:

Change to the directory where Odoo is:

$ cd ~/odoo-dev

Choose a name for the environment and create a directory for it:

$ mkdir my-odoo

Change to that directory and start an Odoo server instance for that environment:

$ cd my-odoo/$ ../odoo/odoo-bin start

How it works...

The Odoostartcommand is a shortcut to start a server instance using the current directory. The directory name is automatically used as the database name (for the-doption), and the current directory is automatically added to the addons path(the--addons-pathoption), as long as it contains an Odoo addon module. In the preceding recipe, you won't see the current directory in the addons path because it doesn't contain any modules yet.

There's more…

By default, the current directory is used, but the--pathoption allows you to set a specific path to use instead. For example, this will work from any directory:

$ ~/odoo-dev/odoo/odoo-bin start --path=~/odoo-dev/my-odoo

The database to use can also be overridden using the usual-doption. In fact, all of the other usualodoo-bincommand-line arguments, except--addons-path, will work. For example,to set the server listening port, use the following command:

$ ../odoo/odoo-bin start -p 8080

As we can see, the Odoostartcommand can be a convenient way to quickstart Odoo instances with their own module directory.

Managing Odoo server databases

When working with Odoo, all of the data of your instance is stored in a PostgreSQL database. All of the standard database management tools you are used to are available, but Odoo also proposes a web interface for some common operations.

Getting ready

We assume that your work environment is set up, and you have an instance running. Do not start it using theodoo-bin startcommand shown in the previous recipe, as it configures the server with some options that interfere with multi database management.

How to do it...

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 that is used to protect access to the database management interface.

Accessing the database management interface

To access the database, the following steps need to be performed:

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).

Setting or changing the master password

If you've set up your instance with default values, and not yet modified it as explained in the following section, the database management screen will display a warning, telling you that the master password is not set and advising you to set one with a direct link:

To set the master password, you need to perform the following: 

Click on the

Set Master Password

button. You will get a dialog box asking you to provide the

New Master Password

:

Type in a non-trivial new password and click on 

Continue

If the master password is already set, click on theSetMaster 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 onContinue.

The master password is in 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. See the next recipe for more information about the configuration file.

Creating a new database

This dialog box can be used to create a new database instance that will be handled by the current Odoo server:

In the database management screen, click on the

Create Database

button at the bottom of the screen:

Fill in the form, as follows:

Master Password

: The master password for this instance.

Database Name

: Input the name of the database you wish to create.

Password

:

Type the password you want to set for the admin user of the new instance.

Language

: Select the language you wish to be installed by default in the new database in

the drop-down list.

Country

: Select the country of the main company in the drop-down list.

Load demonstration data

: Check this box to have demonstration data. This is useful to run interactive tests or set up a demonstration for a customer, but it should not be checked for a database meant to contain production data.

If you wish to use the database to run the automated tests of the modules (refer to Chapter 8, Debugging and Automated Testing), you need to have the demonstration data, as the vast majority of the automated tests in Odoo depend on these records to run successfully.

Click on the

Continue

button, and wait a little until the new database is initialized. You will then be redirected to the instance, connected as the administrator.

Troubleshooting: If you are redirected to a login screen, this is probably because the --db-filter option was passed to Odoo and the new database name did not match the new database name. Note that the odoo-bin start command does this silently, making only the current database available. To work around this, simply restart Odoo without the start command, as shown in the first recipe of this chapter. If you have a configuration file (refer to the Storing the instance configuration in a file recipe later in this chapter), then check that the db_filter option is unset or set to a value matching the new database name.

Duplicating a database

Very often, you will have an existing database and you want to experiment with it to try a procedure or run a test, but without modifying the existing data. The answer is simple—duplicate the database and run the tests on the copy. Repeat this as many times as required:

In the database management screen, click on the

Duplicate Database

link next to the name of the database you wish to clone:

Fill in the form as follows:

Master Password

: The master password of the Odoo server

New Name

:

 

T

he name you want to give to the copy

Click on the

Continue

button

You can then click on the name of the newly created database in the database management screen to access the login screen for that database

Removing a database

When you have finished your tests, you will want to clean up the duplicated databases. To do this, perform the following steps:

In the database management screen, click on the

Delete Database

link next to the name of the database you want to remove:

Fill in the form; enter the

Master Password

, which is the master password of the Odoo server

Click on the

Delete

button

Caution! Potential data loss!If you selected the wrong database, and have no backup, there is no way to recover the lost data.

Backing up a database

For creating a backup, the following steps need to be performed:

In the database management screen, click on the Backup Database link next to the database you want to back up:      

Fill in the form:

Master Password

: The master password of the Odoo server.

Backup Format

: Always use

zip

for a production database, as it is the only real full backup format. Only use the

pg_dump

format for a development database where you don't really care about the file store.

Click on the

Backupbutton.

The backup file will be downloaded to your browser.

Restoring a database backup

If you need to restore a backup, this is what you need to do:

In the database management screen, click on the

Restore Database

button at the bottom of the screen:

Fill in the form:

Master Password

: The master password of the Odoo server.

File

: A previously downloaded Odoo backup.

Database Name

: Provide the name of the database in which the backup will be restored. The database must not exist on the server.

This database might have been moved or copied

: Choose "

This database was moved

" if the original database was on another server, or if it has been deleted from the current server. Otherwise, choose "

This database is a copy

", which is a safe default.

Click on the

Continue

button.

It is not possible to restore a database on top of itself. If you try to do this, you will get an error message (Database restore error: Database already exists). You need to remove the database first.