Tải bản đầy đủ (.pdf) (398 trang)

Odoo 11 development essentials develop and customize business applications with odoo 11 3rd edition

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.47 MB, 398 trang )


Odoo 11 Development Essentials
Third Edition

Develop and customize business applications with Odoo 11

Daniel Reis


BIRMINGHAM - MUMBAI



Odoo 11 Development Essentials Third 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 author, 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 Lazar
Acquisition Editor: Larissa Pinto
Content Development Editor: Flavian Vaz
Technical Editor: Sachin Sunilkumar
Copy Editor: Safis Editing
Project Coordinator: Devanshi Doshi
Proofreader: Safis Editing
Indexer: Aishwarya Gangawane
Graphics: Jason M onteiro


Production Coordinator: Aparna Bhagat
First published: April 2015
Second edition: November 2016
Third edition: M arch 2018
Production reference: 1230318
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78847-779-6
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 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 author
Daniel Reis has had a long career in the IT industry, most as a consultant implementing business
applications in 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.
I would like to thank my wife, Maria, for all the patience and support.
My thanks also to Olivier Dony, for agreeing to be part of this project and for all the valuable feedback given.


About the reviewer
Olivier Dony is a Belgian engineer specialized in network engineering, databases, and
information security. He enjoys analyzing and solving complex problems and getting
to the bottom of seemingly inexplicable bugs.
He joined Odoo Belgium in 2009, where he had the chance to take on many roles,
such as working with the community, running the cloud platform with his team,
setting up the security response team, or just as importantly, solving interesting

problems everyday within the Odoo R&D.


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
Title Page
Copyright and Credits
Odoo 11 Development Essentials Third Edition
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
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
Get in touch
Reviews


1.

Quick Start – The Odoo Developer Mode and Concepts
The Odoo architecture
The Developer mode
Introducing the Library project
Creating a work database
Enabling the developer tools
Adding a field to an existing Model
Adding Menus, Models, and Views
Creating menus
Creating a Custom Model
Creating Views
Configuring access control security
About Odoo base Models
Summary

2.

Installing and Organizing the Development Environment
Setting up a host for the Odoo server
Using a virtual machine
Installing a Linux server
Installing Odoo from source
Initializing a new Odoo database
Managing Odoo databases
A word about Odoo product versions
More server configuration options
Odoo server configuration files
Changing the listening port

The database filter option
Managing server log messages
Developing from a remote workstation


Using a Linux text editor
Installing and configuring Samba
Installing additional modules
Finding community modules
Configuring the addons path
Updating the apps list
Summary

3.

Your First Odoo Application – A Practical Overview
Creating a new addon module
Creating the module's basic skeleton
Adding an icon
A word about licenses
Discovering and installing new modules
Adding to the addons path
Installing the new module
Upgrading a module
The server development mode
The model layer
Creating the data model
Extending existing models
The view layer
Adding menu items

Creating the form view
Business document form views
Adding action buttons
Using groups to organize forms
The complete form view
Adding list and search views
Extending views
The business logic layer
Adding business logic
Extending Python methods
Adding automated tests
Testing business logic
Testing access security
Setting up access security
Adding access control security
Row-level access rules
Web pages and controllers
Summary

4.

Models – Structuring the Application Data
Organizing application features into modules
Introducing the todo_stage module
Creating models
Model attributes
Models and Python classes
Transient and Abstract models
Inspecting existing models



Creating fields
Basic field types
Common field attributes
Special field names
Extending models
Adding fields to a model
Modifying existing fields
Relationships between models
Many-to-one relationships
Many-to-many relationships
One-to-many inverse relationships
Hierarchical relationships
Dynamic relationships using Reference fields
Computed fields
Searching and writing to computed fields
Storing computed fields
Related fields
Model constraints
More model inheritance mechanisms
Copying features with prototype inheritance
Embedding models using delegation inheritance
Summary

5.

Import, Export, and Module Data
Understanding external identifiers
Finding external identifiers
Exporting and importing data

Exporting data
Importing data
Related records in CSV data files
Module data
Demonstration data
XML data files
The noupdate data attribute
Defining records in XML
Setting field values
Setting values using expressions
Setting values for relation fields
Shortcuts for frequently used models
Other actions in XML data files
Deleting records
Triggering functions
Modifying data
Summary

6.

The ORM API – Handling Application Data
Understanding the ORM decorators
Record handling decorators
Specific purpose decorators
Using the ORM built-in methods
Methods for writing model data


Methods for web client use over RPC
Methods for data import and export

Methods for the user interface
The Mail and Social features API
Message subtypes
Posting messages
Adding followers
Summary

7.

Business Logic – Supporting Business Processes
Creating a wizard
The wizard model
The wizard form
The wizard business logic
Logging
Raising exceptions
Helper actions in wizards
Working with the ORM API
The shell command
The server environment
Modifying the execution environment
Transactions and low-level SQL
Context and domain
Context data
Domain expressions
Working with recordsets
Querying models
Singletons
Writing on records
Working with time and dates

Operations on recordsets
Manipulating recordsets
Using relational fields
Working with relational fields
Summary

8.

External API – Integrating with Other Systems
Setting up a Python client
Calling the Odoo API using XML-RPC
Opening an XML-RPC connection
Reading data from the server
Calling other methods
Writing a To-Do client application
Communication layer with Odoo
Creating the CLI
Introducing the ERPpeek client
The ERPpeek API
The ERPpeek CLI
Introducing the OdooRPC library
Summary


9.

Backend Views – Design the User Interface
Defining the user interface with XML files
Menu items
Window actions

The form views
Dealing with several views of the same type
Business document views
The header
The document sheet
Title and subtitle
Smart buttons area
Grouping content in a form
Tabbed notebooks
View semantic components
Fields
Labels for fields
Relational fields
Field widgets
Buttons
Smart buttons
Dynamic views
On change events
Dynamic attributes
List views
Search views
Calendar views
Graph and pivot views
Other view types
Summary

10.

Kanban Views and Client-Side QWeb
About Kanban boards

Kanban views
Designing Kanban views
Priority, kanban state, and color
Kanban card elements
The Kanban card layout
Adding a Kanban card option menu
Actions in Kanban views
The QWeb templating language
The QWeb JavaScript evaluation context
Dynamic attributes by string substitution – t-attf
Dynamic attributes by expressions – t-att
Loops – t-foreach
Conditionals – t-if
Rendering values – t-esc and t-raw
Set values on variables – t-set
Call and reuse other templates – t-call
Dynamic attributes using dicts and lists


Inheritance on Kanban views
Custom CSS and JavaScript assets
Summary

11.

Reports and Server-Side QWeb
Installing wkhtmltopdf
Creating business reports
QWeb report templates
Presenting data in reports

Rendering images
Report totals
Defining paper formats
Enabling language translation in reports
Reports based on custom SQL
Summary

12.

Creating Website Frontend Features
Roadmap
Our first web page
Hello World!
Hello World! with a QWeb template
Extending web features
HelloCMS!
Building websites
Adding CSS and JavaScript assets
The to-do list controller
The to-do list template
The To-Do Task detail page
Website forms
The form page
Accessing the security and menu item
Adding custom logic
Summary

13.

Debugging and Automated Tests

Unit tests
Adding unit tests
Setting up tests
Writing test cases
Testing exceptions
Running tests
About YAML tests
Development tools
Server development options
Debugging
The Python debugger  
A sample debugging session
Alternative Python debuggers
Printing messages and logging 
Killing running processes


Summary

14.

Deploying and Maintaining Production Instances
Available prebuilt packages
Installing dependencies
Preparing a dedicated system user
Installing from the source code
Setting up the configuration file
Multiprocessing workers
Setting up Odoo as a system service
Creating a systemd service

Creating an Upstart/sysvinit service
Checking the Odoo service from the command line
Using a reverse proxy
Setting up Nginx as a reverse proxy
Enforcing HTTPS
Nginx optimizations
Long polling
Server and module updates
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think


Preface
Odoo is a full featured open source platform to build applications. Based on this core framework, a
suite of integrated applications was built, covering all business areas from CRM and Sales to Stocks
and Accounting.
Beyond these out-of-the-box features, Odoo's framework was built with extensibility in mind.
Extensions and modifications can be implemented as modules, to be applied over the module with the
feature being changed, without actually changing it. This provides a clean and easy -to-control and
customized applications.
This capability to combine several modules into feature-rich applications, along with the open source
nature of Odoo, are probably important factors explaining the community that grew around Odoo. In
fact, there are thousands of community modules available for Odoo, covering virtually every topic,
and the number of people getting involved has been steadily growing every year.
Odoo 11 Development Essentials provides a step-by-step guide to Odoo development, allowing
readers to quickly climb the learning curve and become productive in the Odoo application platform.
At the same time, it tries to provide good reference materials, to be kept nearby every time you are
working with Odoo.



Who this book is for
This book was written keeping in mind developers with minimal programming knowledge but a
strong will to learn. We will use a lot the Python language and explain how to run Odoo in a
Ubuntu/Debian system, but little previous knowledge on them is assumed. The code examples are
kept simple and clear, and they are accompanied with appropriate explanations to help build up the
knowledge on them.
Experienced developers, already familiar with Odoo, should also be able to benefit from this book.
Not only does it allow them to consolidate their knowledge, it also provides an easy way to get up to
date with all the details that changed with Odoo 11.0. In fact, special care was taken to highlight all
the relevant changes between Odoo versions since 8.0.
Finally, this book should provide a solid reference to be used daily, both by newcomers and
experienced developers. The documentation of the relevant differences between the several Odoo
versions should also be a good resource for any developer working with different Odoo versions at
the same time or porting modules to other versions.


What this book covers
This books contains 14 chapters, which can be seen to be organized in five parts, roughly,
introduction, models, business logic, views, and deployment.
The first part introduces the Odoo framework, explains how to set up your development environment
and provides a tutorial with thorough, step-by-step creation of a new Odoo module:
, Quick Start – The Odoo Developer Mode and Concepts, visually introduces the Odoo
development concepts, creating a simple Odoo application directly from the user interface. An
existing Odoo installation, or an Odoo.com instance, can be used, so no local setup is needed.
Chapter 1

, Installing and Organizing the Development Environment , explains how to install Odoo
from source and how to set up the development environment to be used throughout the book. We
choose to install Odoo in an Ubuntu host, but guidance is given to have a perfectly functioning

development environment in a Windows machine with an Ubuntu virtual machine.
Chapter 2

, Your First Odoo Application – A Practical Overview, provides a step-by-step guide
through the creation of our first Odoo module. While the example is kept simple, it covers all the
different layers and components that can be involved in an Odoo application: models, business logic,
backend views, and web frontend views.
Chapter 3

The second part of the book introduces Models, responsible for the data model structures around
which the application is built:
, Models – Structuring the Application Data, discusses the Model layer in detail,
introducing the framework's Object-Relational Mapping (ORM), the different types of models
available, and the field types, including relational and computed fields.
Chapter

4

, Import, Export, and Module Data, covers the most used Odoo data file formats—XML and
CSV—the external identifier concept, how use to data file in modules, and data import/export
operations.
Chapter 5

In the third part, we explain how to write the business logic layer on top of the Models—the
"Controller "component of the architecture:
, The ORM API – Handling Application Data, goes further into the ORM, explaining how it
can be used to manipulate the data in the Models. The API for social features, such as followers and
notifications, is also explained.
Chapter 6


, Business Logic – Supporting Business Processes, explains how to program business logic
on the server side, leveraging the ORM concepts and features. It also explains how to use wizards for
a more sophisticated user interaction.
Chapter 7


, External API – Integrating with Other Systems, shows how to implement Odoo external
applications by implementing a command-line client that interacts with our Odoo server. There are
several alternative client programming libraries available, which are introduced and used to
implement our showcase client utility.
Chapter 8

The fourth part explores the "View" layer and the several technologies that can be used for that layer:
, Backend Views – Design the User Interface, covers the web client's View layer, explaining
the several types of views in detail and all the elements that can be used to create dynamic and
intuitive user interfaces.
Chapter 9

, Kanban Views and Client-Side QWeb , keeps working with the web client, but introduces
Kanban views and explains the QWeb templates used to design the Kanban board elements.
Chapter 10

, Reports and Server-Side QWeb, discusses using the QWeb-based report engine and
everything needed to generate printer-friendly PDF reports.
Chapter 11

, Creating Website Frontend Features , introduces Odoo website development, including
web controller implementations and using QWeb templates to build frontend web pages.
Chapter 12


Finally, the fifth part covers deployment and maintenance practices. Some special considerations, not
relevant for development environments, need to be taken into account when deploying for production
use.
, Debugging and Automated Tests , shares some debugging techniques to be used when
developing Odoo modules. How to implement automated tests is also explained, since this is an
essential practice to produce reliable code.
Chapter 13

, Deploying and Maintaining Production Instances, shows us how to prepare a server for
production prime time, explaining what configuration should be taken care of and how to configure an
Nginx reverse proxy for improved security and scalability.
Chapter 14

At the end of the book, the reader should have a solid understanding of all the steps and components
involved in the Odoo application development cycle, from the drawing board to production
deployment and maintenance of these applications.


To get the most out of this book
Other than being familiar with programming, no particular knowledge is expected to be able to take
advantage of this book.
Odoo is built using the Python programming language, so it is a good idea to get solid knowledge of
it. We also choose to run Odoo in an Ubuntu host, and will do some work on the command line, so it
will help to be familiar with it.
To get the most out of this book, we recommend that you find complementary readings on the Python
programming language, Ubuntu/Debian Linux operating system, and the PostgreSQL database.
While we will run Odoo in an Ubuntu host (a popular cloud hosting option), we will provide
guidance on how to set up our development environment in a Windows system with the help of
VirtualBox. Of course, working from an Ubuntu/Debian system is also a possible choice.
All the required software is freely available, and the instructions on where to find it will be given.



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:
1.
2.
3.
4.

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 />pment-Essentials-Third-Edition. We also have other code bundles from our rich catalog of books and
videos available at Check them out!


×