New building block result

Creating building blocks in Odoo

In this tutorial I will learn you how to create new building blocks and how to add the blocks to the menu so that you can quickly drag and drop the blocks in the webpage.
In this tutorial I will create a new building block named “References with title” which will show four icons and a title above it:
New building block result

1. Adding the dependency

Before you can start creating a new building block you will need to create a new module and configure the manifest.py correctly. Open up your manifest.py and add the website as a dependency:

Without this dependency you cannot create and add new building blocks to the Odoo website.

2. Creating a new XML file

Now create a new XML file named “snippets.xml” under the “views” folder:
snippets.xml structure
In this file we will add all the code to create the building block (snippet) and to make it visible in the editor.

2.1 Creating the building blocks

First we will need to create the building block itself. Let us create a building block that has a title (h3) saying “Our references” with four logo’s under the title. Have a look at this code:

So, what does this tell us? We first create a new XML record. After doing this we add all our code within a section block and inside this we create a container div. Within this section and container you can basically code anything you like, this is the framework for any building block. Generally when you create a building block you try to use as much bootstrap classes as possible. In the above example I simply made two rows. One row for the title and one row for the images. Those images are all the same width thanks to the default bootstrap classes col-md, col-sm and col-xs-6.
You’re already well over halfway to your own building block! If you would install this module right now all the code would be there that is needed for a building block, but we still have to show it in the editor so that we can use it.

2.2 Adding the building block to the editor

Let us continue and add a building block preview to the editor so you can quickly find it from the editor.
You can do this by inheriting the default “website.snippets” record and doing an xpath in the “snippet_structure” id, which holds the main structure of the editor. Have a look at this code:

Let me explain the code a bit further. We first inherit the default ‘website.snippets’ record, which holds the link to all available snippets. By doing an xpath on ‘snippet_structure’ we’re telling Odoo to add our building block preview within the editor. In this xpath element we add a div and we use the t-snippet element made by Odoo. By doing so Odoo knows we want to add a snippet preview to the editor.
Finally save this file and add it in the manifest.py file so that it is loaded:

When you now install the module you will see your new building block is available for use from the editor:
Building block editor example
That is all. You’ve just made your own building block, congratulations!

3. Conclusion

Thanks to the Odoo framework it is very easy to create and use new building blocks. The functionality is so flexible and easy to use that you can create a building block for about anything. Creating and reusing building blocks in Odoo is one of the biggest strengths of the Odoo website editor.

Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!


PayPal

Odoo Experts

Odoo homepage screen

Installing Odoo frontend and backend on different servers

In this tutorial I will teach you how to separate your Odoo frontend and backend. I will install the Odoo codebase/frontend on one server and the backend (your PostgreSQL server and database) on another server. You will learn how to configure both Odoo and PostgreSQL to achieve this.

1. Introduction

In order to achieve and demonstrate this setup to you I will be working with two virtual machines.
I will refer to the Odoo codebase/frontend server as the ‘frontend server’. The PostgreSQL server and database will be named ‘backend server’ throughout this tutorial.
My frontend server has the IP 192.168.118.167 and my backend server has the IP 192.168.118.168.
Tip: Make sure that both your servers have a fixed IP, otherwise your setup will break due to IP changes!

2. Configuring the backend server

Let us start with the configuration of the backend server. This server will contain the PostgreSQL server and the database(s).

2.1 Installing postgreSQL

Open up a terminal on your backend server and install PostgreSQL if you haven’t yet:

2.2 Creating the PostgreSQL user

Now create a new PostgreSQL user. Make sure that the PostgreSQL username matches with the username of the user running Odoo on your frontend server. In my example I will create a new PostgreSQL user with the name “odoo11”:

After you execute this command the system will ask you for a password for this user. Fill in a password and confirm this password again.
Tip: Don’t forget to remember this password, you’ll need it later on.

2.3 Configuring pg_hba.conf

After installing the PostgreSQL server and creating the user we now need to configure the remote connections. As our backend server will be used for the database connections our frontend server needs to be able to access it. Open up your frontend server and get the IP of the server. In my example my frontend server has the IP 192.168.118.167. Now open up your pg_hba.conf file on your backend server:

Scroll in this file and search for the following line of code:

Add a new line after the existing one which contains your frontend server its IP address. As you don’t want to use the exact IP address you should use the /24 subdomain. In my example this results to 192.168.118.0/24 instead of 192.168.118.167. Your configuration file should now look like this:

Finally, save your file and close it. Your PostgreSQL now knows that you want to allow connections from the backend server (IP 192.168.118.167).

2.4 Configuring postgresql.conf

Your PostgreSQL still needs to know the listen address of your frontend server too though. To achieve this we have to edit the postgresql.conf file:

Find the ‘listen_addresses’ line, which looks like this by default:

Now add in the IP of your frontend Odoo server (in my example 192.168.118.192):

Tip: Don’t forget to remove the # in this line because otherwise this line will be skipped!
Save this file and close it.
You’ve now applied all the changes that you need to do on the backend server. Next reload the PostgreSQL service in order to apply all the changes:

3. Configuring the frontend server

Your backend server is done now. Switch to the frontend server and open up your Odoo configuration file.
Tip: If you haven’t installed an Odoo yet you can follow my tutorial here
Typically your Odoo configuration file is under /etc/ and is named your-odoo.conf:

Add or change the following parameters in the configuration file:

Finally, restart your Odoo service to reload your Odoo configuration:

When you now browse to your Odoo instance (on your frontend server) you’ll see that your new Odoo is ready to use.
Odoo homepage screen
That is all! You’ve now setup your own Odoo instance where the frontend and backend are split between two servers.

4. Tips

While this is everything that you need in order to split up the backend and frontend of Odoo there are still some things to consider.
Because of security reasons you’ll most likely want to encrypt all the data and place everything on SSL. This also includes the calls from the frontend server to the backend server.
After you’ve created a new database you should check if the database is in fact created on the backend server instead of on the frontend server. You can do this executing the following commands (one by one) on your backend server:

After running these commands you can see an overview of your databases and to what user the database belongs. In my example you can see a new database named ‘ABC’ which is owned by my Odoo/PostgreSQL user ‘odoo11’:
Database overview result backend

5. Conclusion

Setting up an Odoo instance where the frontend and the backend are split is actually quite easy. Due to the built-in parameters from Odoo it is very easy to configure the frontend side. When you split the frontend and backend up and apply extra security – such as HTTPS connections and VLAN’s – you’ll have a safer and more controlable Odoo instance.
If you’d like to learn more about the deployment of servers and the default Odoo parameters you can have a look at the official documentation.

Has this tutorial helped you, do you have any feedback or questions? Post away!


PayPal

Odoo Experts

Creating automated tests in Odoo

In this tutorial I will learn you how to write tests, how to use the tests in Odoo and how to test them. I will create an example test that creates a new project, a new task and attaches the project to the new task.
Tip: This tutorial is based on Odoo V11 but will also work in V10.

1. Creating a new module

Let us create a new module to start. You can either manually create a module or create one with the scaffold command from Odoo. Now that you’ve created your module you should add the dependency to the module where you want to create a test for. In this tutorial I will create a test for the project module so I’ll add a dependency for ‘project’. Add your depends in the manifest.py (or __openerp__.py in V10):

2. Creating the Python files

After you’ve created your new module you should create a new folder named ‘tests’. Create a new Python file for the test, in this example I’ll create a new file named ‘test_project.py’.
Tip: The filename of a Python test should always start with ‘test_’ or it will not be executed! Your folder structure should now look like this:
Tests folder structure

Next create a new Python file named __init__.py and import the Python file ‘test_project’ in it:

Your folder structure should now look like this:
Complete tests folder structure
Tip: You should not import the tests folder in the main __init__.py file. Odoo tests are an exception and have a built-in check by Odoo. They do not need an explicit import like models or controllers.

3. Creating the class and adding imports

Now open up your ‘test_project.py’ file again and let us start by adding the import and creating the class structure!
In order to write a test you should import ‘odoo.tests’ and you should create a new class with a TransactionCase. Have a look at this code:

So, does this make sense to you? We simply import the default Odoo framework options for tests with ‘from odoo.tests import common’. We then create a new class in which we add ‘common.TransactionCase’. The ‘common.TransactionCase’ tells Odoo that we want to test a transaction and that we want to do a rollback of this record after the test is done.

4. Writing the tests

Now that we have the basic structure setup there is just one more thing to do: write a function in which you want to do your tests. Go ahead and create a new function in your file.
Tip: Function names for tests should always start with ‘test_’ or they will not be run by Odoo!

Finally, write the test cases you’d like in this function. In my example I will create a test that creates a new project, then creates a new task and then adds the project to the task. Have a look at this piece of code:

So, does this make sense to you? A lot of the code looks the same as if you’re writing custom code. With self.env[‘project.project’].create({}) we’ll create a new project named ‘TestProject’. With self.env[‘project.task’].create({}) we’ll create a new task named ‘ExampleTask’ and we will assign the project to this task, thanks to the ‘test_project’ variable.
If you would run this code it would create a new project and task and the moment the test is finished the transaction is rolled back (so there is no record in the database).

4.1 Understanding assertEqual

The final part of my code shows some assertEqual calls, so what do these calls do? assertEqual is made available by default in Odoo to test and compare values. For example:

This will test if the just created project record it’s name is indeed named ‘TestProject’. In the second assertEqual I check if the task name is in fact ‘ExampleTask’. And, finally, in the third assertEqual I check if the project attached to the task is indeed the right project:

The first variable of assertEqual is always the value you’ve gotten back from the test and the second part is the result that you’re expecting. If there is a difference in those two the test will fail.

5. Running / testing the test

So now we’ve coded the whole test but how do we run and test it? In order to run a test you should use the ‘–test-enable’ parameter and the ‘-i’ parameter to say which module(s) you want to test. If you want to test this in your local Odoo instance you will need to run your Odoo from the terminal like this:

If you have a runbot instance (or use Odoo.sh) the tests will be executed automatically and you will see if the test succeeded or failed right away. An example of Odoo.sh:
Odoo.sh status example
An example of a runbot instance:
Runbot status example

When you just run the tests locally you can see if the test succeeded or failed in the logfile. Open up your logfile after you’ve ran the test and have a look at the output:

If you add a print statement in the test, like in my example, you will also see it printed in the terminal:
Succesfull test

5.1 Making a test fail

We’ve now seen how to create a test and how to run it, but how exactly does a failed test look?
Open up your test again and change the assertEqual from this:

to this:

Run the test from your terminal again with -i your_module –test-enable in the command. You’ll see that the print statement is no longer printed in your terminal. Now open up your logfile again after your Odoo is loaded. You’ll now find a failure in the logfile:

This shows you that the input ‘TestProject’ does not match what we expect (‘TestProject11’) and because of this our test failed.
If you would commit these changes to Github and if you’re using a runbot or Odoo.sh you’ll also see a failed instance after this.

6. Conclusion

Writing and using tests in Odoo is quite easy to do. It is a great feature to use if you’re writing a lot of custom code and want to keep overview. Thanks to Odoo.sh and runbots it is also very easy to follow up on your tests and to see if something has broken or not. Sadly at this point running tests locally isn’t very easy to follow up though so you might want to consider setting up a runbot or Odoo.sh account.
Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!


PayPal

Odoo Experts

Creating a tour in Odoo

In this tutorial I will teach you how to create a tour in Odoo. You will learn how to create a tour step by step in JavaScript and how to guide your users through your custom development.
In this tutorial I will create a new tour throughout the contacts app in order to guide a new user through this module.
Tip: This tutorial is based on Odoo V11 but will also work in V10.

1. Creating a new module

Let us create a new module to start. You can either manually create a module or create one with the scaffold command from Odoo. Now that you’ve created your module you should add the dependency to the module where you want to create a tour. In this tutorial I will create a tour that guides the user through the contacts app.

2. Creating the tour

The first thing to do in order to create a tour is to create a JavaScript file. In Odoo you should place all JavaScript code under /static/src/js so create these folders first. Create these new folders and then create a new JavaScript file named ‘tour.js’. You should now have this:
JavaScript folder structure
Now open up your JS file and define a new function. In this function you should require ‘web.core’ and ‘web.core.tour’ and include ‘core._t’ in order to manage translations later on. Your code should now look like this:

2.1. Registering the tour

You now have the basic JavaScript structure! It is time to register a tour and add steps to this tour. Just have a look at this code for a minute:

Does this make any sense to you? Let us walk over it line by line. The tour.register function will tell Odoo that you want to add a new tour to Odoo. ‘example_tour’ is the technical name of the tour, but be sure that it is unique! The next line shows “url” which tells Odoo on which page the tour should start (in this tutorial from the home screen). ‘tour.STEPS.MENU_MORE’ is built in in Odoo to know where/when/how the tour should be loaded.

Finally, you’ll see the options ‘trigger’, ‘content’ and ‘position’. The ‘trigger’ will tell Odoo when exactly that you would want to trigger the tour step. In this example our tour would start on the main Odoo screen and will show a little icon under the contacts app. If you would install the app with this piece of code you would get the following result:
Tour home screen

The ‘content’ will show a text dialog to the enduser and the ‘position’ tells Odoo where exactly to show this text (top, right, left or bottom).

2.2 Choosing your tour steps

A tour should contain multiple steps and a logical flow though. Take a minute to figure out the steps you would like your users to follow in your tour.
In this tutorial I will add the following steps:
– The moment the user clicks on ‘Contacts’ and when the Kanban view opens we’ll show a hint to click on the ‘Create’ button.
– After clicking on the ‘Create’ button we’ll show a hint to fill in the name of the contact.
– When the name of the contact is filled in we’ll show a hint to click on ‘Save’.

2.3 Creating all tour steps

So, how do we code all the steps? Just have a look at the full code for a minute:

If you would run this code it would do exactly the points that I’ve subscribed. The trigger tells when the next step should go off. For example with the trigger ‘.o-kanban-button-new’ it will show a blinking icon next to the ‘Create’ button. Have you noticed the ‘extra_trigger’ option and the ‘run’ option in the third step? By setting an ‘extra_trigger’ Odoo knows when the trigger should go off (in this case after the user clicked on create so he is in edit mode). The ‘run’ option will tell Odoo to automatically fill in text in the field that we’ve put a trigger on, in this example the field ‘name’. If we would run (play) the tour from the Odoo interface it would now automatically fill in the name of the new contact.
That is all! You’ve now got the JavaScript code to create a tour.

3. Including the JavaScript

Finally you should now include your JavaScript file so that it gets compiled in Odoo too. Create a new xml file named ‘assets.xml’ in a new folder ‘views’:
Assets folder structure

Open up the XML file, inherit the assets template and insert the tour.js file into the assets. Your code should look like this:

Now open up your manifest.py (or openerp.py if you’re in V10) and add the ‘assets.xml’ file to the data key so that it gets loaded too:

That’s it, you’ve created your very first tour! If you save this code and run the module you will get the following behaviour:


4. Translating the tour

Now what if you have Odoo users that speak different languages? Luckily you can also translate tours to any language. Activate developer mode and then go to ‘Settings’ > ‘Translations’ > ‘Export translation’. You’ll now see a dialog from which you can choose the target language and where you can select your app. Select your target language, choose the file format ‘PO File’ and choose your app. Finally click on ‘Export’:
Export translations

Next create a new folder named ‘i18n’ and add your downloaded file to this folder:
Translation folder structure
Finally, open up the PO file and translate all the terms:
Translation example
When you now update your module and switch to another language you’ll see that the tour is translated.

5. Conclusion

Because of the built-in framework from Odoo it is very easy to create a tour for users. With this tour you can guide a new user through your own custom development in a matter of minutes and with personalised text. Creating tours is really quick and just takes a few lines of code.
Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!


PayPal

Odoo Experts

Installing and configuring Odoo on DigitalOcean

In this tutorial I will teach you how to install Odoo on a new DigitalOcean VPS. You will learn how to create a new account, create a new server (droplet) and how to install any Odoo version on it.
In this tutorial I will show you step by step how to create the server and how to install the Odoo thanks to my installation scripts.

1. Creating a DigitalOcean account

If you do not have a DigitalOcean account yet you should create one. If you do you can continue to chapter 2. You can create a new DigitalOcean account through this link, which will give you $10 for free to start with. After clicking on this link you will see the following screen:
DigitalOcean website
Simply click on the link, fill in an e-mailadress and a password and click on “Sign Up”. After doing so you’ll get an e-mail in your inbox. Open up the link in your e-mail and your account will be activated.

2. Creating a new server (droplet)

Now that you have an account it is time to create a VPS. DigitalOcean calls a VPS (or a server) a droplet. When you’re on the home screen after logging in you’ll see a button “Create”. After you click on this button you will see a dropdown that has an option called “Droplets“, click on it:
Create droplet button
After clicking on the button a new page will show up asking which OS you want to use and what specifications (with their prices) you’d like:
Droplet configuration

For this tutorial I will create an Ubuntu 16.04.3 droplet (VPS) with 2GB RAM, 2 CPU’s and 40GB storage. This will cost $20 per month at the time of writing. Choosing what server you need is up to you and depends on your requirements. Do know that a server with less than 1GB of RAM will not work out. Finally you will see some additional options, the ability to add an SSH key and to name your droplet (VPS) as you’d like:
Droplet configuration
I’d advice you to check on “Monitoring” as it has no extra costs but it allows you to configure warnings. For more information about monitoring you should read this article from DigitalOcean.
Now give your droplet (VPS) a name and click on the “Create” button. A few seconds later you will see that the droplet (VPS) is being created:
Droplet creation page
That’s it! You’ve just created a new droplet and are ready to connect with it.

3. Connecting with the DigitalOcean droplet (VPS)

Because you’ve just created a new droplet (VPS) you’ve gotten an e-mail in your inbox. Open up your mailbox and you will find an e-mail with all the information you need to connect to the remote server:
Droplet connection details

You can now SSH to the server with these details in order to configure the server and install Odoo. You can connect by using the Linux command ‘ssh’, which looks like this:

SSH tells Linux that you want to open up a secure connection, root is the username of the user you’re connecting with and 138.68.95.197 is the IP of the server.
After entering this command you will get the question if you’re sure to continue connecting, just type yes. Now you’ll need to enter the password that is inside the e-mail you’ve recieved from DigitalOcean. Finally the server will ask you your current password and a new one, in order to change your automatically generated password to a safer one. Go ahead and change your password, after doing so you’re good to go:
SSH connection

4. Installing Odoo

That wasn’t too bad right? Now there is just one more thing to do: install Odoo on the server!
In order to install Odoo on the server you can use my installation script, which does a lot of the installation automatically. You can find an installation script for every Odoo version on Github. In this example I will install Odoo 10, but you’re free to install any other version.

4.1 Downloading the installation script

  • If you want to install Odoo 10 you can run the following command:
  • If you want to install Odoo 9 you can run the following command:
  • If you want to install Odoo 8 you can run the following command:

As a result you will get a new file (named ‘odoo_install.sh’) on your system which contains all the code to install Odoo:
Download installation script

4.2 Configuring the installation script

Now open up the file and edit the parameters to your liking:

There are some things you can configure/change to your likings at the top of the script. You can choose if you wish to install Wkhtmltopdf or not, which version you’d like, where the location is and most importantly what the master admin password is.
Tip: always modify this for every Odoo you install!
If you want the enterprise version of V10 you should change the line IS_ENTERPRISE to true:

If you want the community version you can just continue and keep the IS_ENTERPRISE key on False (which is the case by default):

4.3 Making the Odoo installation file executable

The next step is to make this file executable. After you’ve made it executable you can execute it and everything will be installed automatically.
You can do this with the following command:

4.4 Running the installation script

Now that the code is in your file and the file is executable you simply have to execute it with the following command:

You will see that the script automatically starts updates, downloads required packages, creates the user, downloads the code from Github, … Eventually, if you’ve chosen to install the enterprise version, you will need to give in your Github credentials to download the Enterprise code (since this is a private repository). Fill in your details and let the script continue:
Enterprise authentication
Finally, give the script a few minutes to configure and install everything and eventually you will see something like this:
Result installation script

You now have a fully functional Odoo on your system! Congratulations.

4.5 Accessing your Odoo

Good job! You’ve created a DigitalOcean account, created a droplet (VPS), configured it and installed Odoo on it. Now it is time to enjoy the results of your hard work. If you now open up your browser and surf to your IP and the port you’ve installed Odoo on (by default 8069) you can see your Odoo is up and running. Because the droplet (VPS) is automatically connected to the internet (a public IP address) your Odoo will also be publicly available. Go to http://yourip:yourport and you will see your just installed Odoo:
Public Odoo page

4.6 Security advisory

As security is a very big topic I did not add anything about security in this tutorial. Please do configure your Odoo with a firewall, Nginx, SSL and best security practices. The security part is out of scope in this tutorial because of the size of the subject but please do add in security rules.
For more information please do look at Nginx, the official DigitalOcean security measures and LetsEncrypt.

5. Conclusion

Creating a new server, configuring it and installing an Odoo on it is very easy with DigitalOcean. You’ll have a VPS in a matter of minutes for an affordable price. Due to the accessibility and flexibility of DigitalOcean and Odoo it has never been so easy to have an ERP publicly available.

Has this tutorial helped you, do you have any feedback or questions? Post away!




PayPal

Odoo Experts

Extend existing selection with selection_add in Odoo 10

In this tutorial I will teach you how to extend an existing selection through code in Odoo 10. You will learn how to extend the selection (without overriding the existing keys) and only adding new keys with the “selection_add” parameter.
In this tutorial I will extend the existing selection for an employee where the gender is set. After this toturial you will be able to add new keys to a selection.

1. Adding the correct dependencies

Alright let us start right away! First of all you will need to add a dependency to the module from which you want to extend the selection. In this tutorial I will need the “hr” module and I’ll add in the “contacts” module too because you’ll want to see the contacts anyway. Open up your __manifest__.py and add in the dependencies you need:

2. Inheriting the model

Now create a new Python file that inherits the model on which you want to extend an existing selection. In my example I’ll need to inherit ‘hr.employee’ as the field ‘gender’ is on this model:
Gender field on hr.employee
Your code should now look like this:

3. Extending the selection

Finally (or should I say already?) you can extend the field and add the new options you’d like. Inheriting the existing field is as simple as calling it with the same name again. Then just use the “selection_add” key to insert new options. Your code should be looking like this:

So what does this code do? Rather then having to give all key values again you can simply add only the new values you’d like. Odoo will automatically add these values to the selection without modifying anything to the existing keys. As a result this is the cleanest way to extend selections and prevents you from reassigning values every time. Your final code should now look like this:

After you’ve installed this custom code you’ll see that the new value is added because of this code:
Result selection field

4. Conclusion

Extending selections in Odoo has become very easy due to the “selection_add” parameter. It allows you to extend selections without having to override existing keys and removes some complexity and margin for errors.
Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!




PayPal

Odoo Experts