Reader small image

You're reading from  Odoo 15 Development Essentials - Fifth Edition

Product typeBook
Published inFeb 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781800200067
Edition5th Edition
Languages
Tools
Right arrow
Author (1)
Daniel Reis
Daniel Reis
author image
Daniel Reis

Daniel Reis has a degree in applied mathematics and an MBA. He has had a long career in the IT industry, mostly as a consultant implementing business applications in a variety of sectors. He has been working with Odoo (OpenERP at the time) since 2010 and is an active contributor to the Odoo Community Association (OCA), where he also serves as a board member. He is the managing director of Open Source Integrators, a leading open source and Odoo consultancy firm.
Read more about Daniel Reis

Right arrow

Chapter 3: Your First Odoo Application

Developing in Odoo usually means creating our own modules. In this chapter, we will create our first Odoo application, learn the steps needed to make it available to Odoo, and install it.

We will get started by learning the basics of the development workflow—we'll create and install a new module and update it to apply the changes we make throughout the development iterations.

Odoo follows a Model-View-Controller (MVC)-like architecture, and we will go through the different layers to implement a library application.

In this chapter, we will cover the following topics:

  • Overview of the library project
  • Step 1 – Creating a new addon module
  • Step 2 – Creating a new application
  • Step 3 – Adding automated tests
  • Step 4 – Implementing the model layer
  • Step 5 – Setting up access security
  • Step 6 – Implementing the backend view layer
  • Step 7 – Implementing...

Technical requirements

This chapter requires you to have an Odoo server installed and be able to start it from the command line to perform actions such as installing modules or running tests. If you don't have a working Odoo development environment, make sure you review Chapter 2, Preparing the Development Environment.

In this chapter, we will create our first Odoo application from a blank slate, so we won't need any additional code to get started.

The code for this chapter can be found in the book's GitHub repository at https://github.com/PacktPublishing/Odoo-15-Development-Essentials, in the ch03 directory.

Overview of the library project

We will use a learning project to better explore the topics explained in this chapter, and see them work in practice. We will create a new Odoo app to manage a book library. We will use this project in all the following chapters, where each chapter will be an iteration, adding features to the app. Here, we will create a first iteration of the library app.

The first feature we will implement will be the book catalog. The catalog allows us to keep records of the books in our library, with their relevant details. We also want to make this catalog available through a public website, where the available books can be seen.

Library books should have the following data:

  • Title
  • Authors
  • Publishing company
  • Date published
  • Cover image
  • International Standard Book Number (ISBN), with check digit validation
  • Active flag, indicating the books that should be publicly available on the website

As is usual for the Odoo base apps...

Step 1 – Creating a new addon module

An addon module is a directory containing files that implement some Odoo features. It can add new features or modify existing ones. The addon module directory must contain a manifest file—or descriptor file—named __manifest__.py.

Some module addons are featured as an app. Apps are the top-level module for a feature area in Odoo, and we expect our module to be featured in the top-level Apps menu. Examples of apps in base Odoo include CRM, Project, and HR. A non-app module addon is expected to depend on an app, adding or extending features to it.

If a new module adds new or major functionality to Odoo, it probably should be an app. If the module just makes changes to an existing app, it probably should be a regular addon module.

To develop a new module, we will do the following:

  1. Ensure that the directory where we will work is in the Odoo server addons path.
  2. Create the module's directory, containing the...

Step 2 – Creating a new application

Some Odoo modules create new applications, and others add features or modify existing applications. While the technical components involved are about the same, an app is expected to include a few characteristic elements. Since the Library module is a new app, we should include them in our module.

An app is expected to have the following:

  • An icon, to be presented in the app list
  • A top-level menu item, under which all the app's menu items will be placed
  • Security groups for the app so that it can be enabled only for users that need it, and where access security will be set

The app icon is an icon.png file in the module's static/description/ subdirectory. This was done earlier, in the Adding an icon section.

Next, we will take care of the app's top-level menu.

Adding a top menu item

Since we are creating a new app, it should have a main menu item. On the Community Edition (CE), this is shown as...

Step 3 – Adding automated tests

Programming best practices include having automated tests for your code. This is even more important for dynamic languages such as Python—since there is no compilation step, you can't be sure there are no syntactic errors until the interpreter runs the code. A good editor can help us detect some of these problems ahead of time but can't help us ensure the code performs as intended, as automated tests can.

The TDD method states that we should write tests upfront, check that they fail, then develop code that, in the end, should pass the tests. Inspired by this approach, we will add our module tests now before we add the actual features.

Odoo supports automated tests, based on Python's built-in unittest library. Here, we will have a quick introduction to automated tests, and a longer explanation is provided in Chapter 8, Business Logic – Supporting Business Processes.

Changes in Odoo 12

Until Odoo 11, tests...

Step 4 – Implementing the model layer

Models describe and store business object data, such as a customer relationship management (CRM) opportunity, sales order, or a partner (customer, supplier, and so on). A model describes a list of fields and can also have specific business logic attached to it.

Model data structure and attached business logic are described with Python code, using an object class derived from an Odoo template class. A model maps to a database table, and the Odoo framework takes care of all the database interactions, both in keeping the database structure in sync with the object and in translating all transactions to database instructions. The framework component responsible for this is the object-relational mapping (ORM) component.

Our application will be used to manage a library, and we need a model for the book catalog.

Creating a data model

Following the Odoo development guidelines, the Python files for models should be placed inside a models...

Step 5 – Setting up access security

The library.book model was created as the database, but you might have noticed that when it is loaded, it prints this warning message to the server log:

 The model library.book has no access rules, consider adding one. 

The message is pretty clear—the new model has no access rules, and so it can't be used by anyone yet. Earlier, we created the security groups for this app, and we now need to give them access to the app's models.

Changes in Odoo 12

The admin user follows access security rules, just as with any other user, except for the root-like internal superuser. We need to grant it access to new models before it can use them. This was not the case up to Odoo 11. In these earlier Odoo versions, the admin user was also the internal superuser and bypassed access security rules. This means that newly created models were automatically available and usable to it.

Adding access control security

To get a picture...

Step 6 – Implementing the backend view layer

The view layer describes the UI. Views are defined using XML, which is used by the web client framework to dynamically generate data-aware HTML views.

Menu items can execute window actions to render views. For example, the Users menu item processes a window action, also called Users, that in turn renders a view composition, with a list and a form.

Several view types are available. The three most commonly used views are the List (sometimes called tree for historical reasons), the Form, and the Search options available in the top-right search box.

Throughout the next sections, we will make gradual improvements and will need frequent module upgrades to make them available, or we can use the --dev=all server option, which spares us from module upgrades while developing. Using it, the view definitions are read directly from the XML files, and the changes made are immediately available to Odoo without the need for a module upgrade...

Step 7 – Implementing the business logic layer

The business logic layer supports the application's business rules, such as validations and automation. We will now add the logic for the Check ISBN button. This is done using Python code, adding a method to the Python class representing the library.book model.

Adding business logic

Modern ISBNs have 13 digits, the last of which is a check digit computed from the first 12. If digits contains the first 12 digits, this Python code returns the corresponding check digit:

ponderations = [1, 3] * 6
terms = [a * b for a, b in zip(digits, ponderations)]
remain = sum(terms) % 10 
check = 10 - remain if remain != 0 else 0
return digits[-1]

The preceding code, with some adjustments, will be at the heart of our validation function. It should be a method in the class Book(...) object. We will add a method that checks a record's ISBN and returns True or False, as follows:

    def _check_isbn(self)...

Step 8 – Implementing the website UI

Odoo also provides a web development framework, to develop website features closely integrated with the backend apps. We will take our first steps toward this by creating a simple web page to display a list of active books in our library.

The book catalog page will respond to web requests at the http://my-server/library/books address, so /library/books is the URL endpoint we want to implement.

Web controllers are the components responsible for rendering web pages. A controller is a Python method in an http.Controller derived class. The method is bound to one or more URL endpoints using the @http.route controller. When any of these URL endpoints are accessed, the controller code executes and returns the HTML to be presented to the user. The HTML rendering will usually be done using the QWeb templating engine.

Adding the endpoint controller

Code for controllers is expected to be inside a /controllers subdirectory. To add a controller...

Quick reference

Most of the components are discussed in more detail in other chapters, and quick references are provided there, as follows:

  • Chapter 2, Preparing the Development Environment, for the CLI install and upgrade modules
  • Chapter 5, Importing, Exporting, and Module Data, for creating XML and CSV data files
  • Chapter 6, Models – Structuring the Application Data, for the model layer, defining models and fields
  • Chapter 7, Recordsets – Working with Model Data, for domain filter syntax and recordset manipulation
  • Chapter 8, Business Logic – Supporting Business Processes, for Python method business logic
  • Chapter 10, Backend Views – Designing the User Interface, for views, including window actions, menu items, forms, lists, and searches
  • Chapter 13, Creating Web and Portal Frontend Features, for web controllers and QWeb syntax

Not explained further elsewhere is access security, and we provide here a quick reference for...

Summary

We created a new module from scratch, covering the essential components involved in a module—models, access security, menus, the three basic types of views (form, list, and search), and business logic in model methods. We also learned how to create web pages using web controllers and QWeb templates.

In the process, we got familiar with the module-development process, which involves module upgrades and application-server restarts to make gradual changes effective in Odoo.

Always remember, when adding model fields, an upgrade is needed. When changing Python code, including the manifest file, a restart is needed. When changing XML or CSV files, an upgrade is needed; also, when in doubt, do both: restart the server and upgrade the modules.

We've gone through the essential elements and steps to create a new Odoo app. But in most cases, our modules will be extending existing apps to add features. This is what we will learn about in the next chapter.

Further...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Odoo 15 Development Essentials - Fifth Edition
Published in: Feb 2022Publisher: PacktISBN-13: 9781800200067
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Daniel Reis

Daniel Reis has a degree in applied mathematics and an MBA. He has had a long career in the IT industry, mostly as a consultant implementing business applications in a variety of sectors. He has been working with Odoo (OpenERP at the time) since 2010 and is an active contributor to the Odoo Community Association (OCA), where he also serves as a board member. He is the managing director of Open Source Integrators, a leading open source and Odoo consultancy firm.
Read more about Daniel Reis