Create and manage Odoo databases with ERPpeek or XML-RPC

Hi guys,

In this tutorial I will learn you how to connect to your Odoo database with ERPpeek or with simple XML-RPC calls. In this tutorial you will learn how to create new databases, how to install modules, how to add models and how to search for data in the database or write data in the database. These XML-RPC calls can be done from anywhere, also remotely.

In this example I will learn you how to create a new database, how to list the installed modules, how to install a module, how to add models and eventually how to search for data and add data in the database!
In this example I will be working with two ways: first with ERPpeek (which is an XML-RPC library wrapper which makes coding even easier) and I will then switch to XML-RPC calls, which are slightly more complex but do not need any remote libraries installed.

1. Installing ERPpeek

The first thing that you will need for this library is the ERPpeek library, which you can install very easily:

1.1 Creating a new database

One of the handy methods in ERPpeek is that you can very easily create a new database by command in a Python file. In this blogpost I will be separating all examples completely so I will also add the connection code again every time. This is because I will be switching from Erppeek to normal XML-RPC calls later on, to learn you both concepts.
Alright let us start! Create a new folder somewhere where you can create new Python files in. Now create a new Python file named ‘create_database.py’. So, what do we need to create a new database? A database name, the path to the server (either on the same server or remote) and an administrator password for the admin user. A second thing that we need is to import the Python library ERPpeek, so we can use all the handy tools from there. The code will look like this:

So, we now have the credentials but how do we create the database? We can access all functions with ‘erppeek.client’ (you can find all methods in the ERPpeek documentation) which opens up a broad range of options. The first thing we need to do is create a client:

So we have the credentials now, we have a client and now we need to create the database! From here on we can acces everything we need from the variable ‘client’. Let us first add a validation to see that the database doesn’t exist yet and then create a new database with the function ‘create_database’! The code:

That is all you need to do! Just to be clear, here is whole code:

When you now execute this Python file it will create the new database for you. Save the Python file and execute it:

After a few seconds you will see the following result in your terminal:
New created database with ERPpeek

Congratulations, you’ve just created your first Odoo database by command. Now let us continue and see which modules are already installed.

1.2 Getting installed modules

A handy thing to know is which modules are installed in your Odoo, so let us create a new Python file named ‘list_installed_modules.py’. Import ERPpeek and create a new client to connect with:

The first value for the erppeek.Client is the server IP (or localhost, which translates to 0.0.0.0) and the port where the Odoo runs on, in this case 8080. The second value is the name of your database (ErpPeekDemoDatabase), the third is your Odoo username (admin) and the fourth is the user its password (admin).
So, we now have a working connection to the Odoo database and we now need to find out which modules are installed. This data is saved in the model ‘ir.module.data’, when the field ‘state’ is set to installed we know the module is installed. This translates to the following code with ERPpeek:

And for demo purposes let us simply print out this data in the terminal:

When you now execute this Python file it will it will print out all installed modules. Save the Python file and execute it:

After a few seconds you will see the following result in your terminal:
Installed modules

We now know which modules are installed, but wouldn’t it be great if we could install modules by command? Read on in chapter 1.3!

1.3 Installing a module

Create a new Python file and name it ‘install_module.py’. Import ERPpeek and create a client just like you did in the previous examples:

We can now get all modules with ‘client.Modules’ and we can check if it is installed with the setting installed=False. When the module is in the state uninstalled we can install it with client.install. In this example I will check for the module sale and if it isn’t installed I will install it. The code:

When you now execute this Python file it will install the module for you (if it isn’t installed yet). Save the Python file and execute it:

After a few seconds you will see the following result in your terminal:
New created database
Do you notice anything interesting? Odoo will automatically check all dependencies and if some aren’t met they will be installed by Odoo! So we have now created a new database, we’ve checked which modules are installed and installed one which wasn’t installed yet. Now let us continue by adding a new contact to the ‘res.partner’ model.

1.4 Adding new records to a model with XML-RPC

So we’ve seen a few examples with ERPpeek now but we do not really need this library. We can just as easy do simple XML-rpc calls! It involves a bit more coding but it gives you the same possibilities as with the ERPpeek library. From here on I will continue this tutorial with XML-RPC calls for learning purposes. Do know that you can also use the ERPpeek library if you’d like to!

So we no longer need the ERPpeek library but we now need the library ‘xmlrpclib’. Import it and create a connection to the Odoo environment. We first need a ServerProxy which refers to the URL ‘localhost:xxxx/xmlrpc/common’ we then need an uid and then we need a socket to connect to. The code will look like this:

Do you see the similarities between the previous examples where we instantiated a client in one line? The working method is in essence the same, the code just looks slightly different and it is a bit more complex. What we did in one line in the previous examples now takes 6 lines but there is no difference in the possibilities.
We now have a client to connect with Odoo so let us now create a new record. The new record should be written in a dictionary format where you can add values for every field you would like to fill. For example I will create a new contact on the model ‘res.partner’ with the name ‘Fabien’ and the e-mail ‘example@odoo.com’. This translates in the following code:

The last part is to write the new record to Odoo (with XML-RPC) by using sock.execute. The code:

What does this code do? It connects to the database with the credentials you’ve provided at the top of your script, it calls the model ‘res.partner’, uses the method ‘create’ (which creates a new record) and eventually we pass the dictionary to Odoo to write it in the database.

When you now execute this Python file it will create a new record in the database, on the model ‘res.partner’. Save the Python file and execute it:

After a second or two you will see the following result in your terminal:
New inserted contact
The new contact has been created with the id 7. When you now open up your Odoo and go to sales > contacts you will see the new contact:
New contact
Congratulations, you can now also connect with Odoo through XML-RPC calls and you can write data in the Odoo database! Now, let us learn how to fetch data from the database.

1.5 Searching for data in the database

Create a new Python file named ‘search_contact.py’, import the xmlrpclib, create credentials and create a socket connection:

Create a new sock.execute, just like you did in the previous example, but this time use the search method on the ‘res.partner’ model to get all contacts:

When we would execute the code at this point it would print us all the ID’s from all records on the model ‘res.partner’, but we as humans don’t know a lot by ids so let us translate these ids in to readable content! Loop over all the ids and create another sock.execute with the read function to get all the details. Finally print out the details of all contacts:

When you now execute this Python file it will search all records in the database, on the model ‘res.partner’. Save the Python file and execute it:

After a second or two you will see the following result in your terminal:
Details from inserted contacts

Congratulations! You can now also search for data in Odoo with XML-RPC!
There is one more thing I would like to learn you: how to create new models through XML-RPC calls.

1.6 Creating new models with XML-RPC calls

A rather often needed thing in Odoo is to create new models and this can also be done through XML-RPC calls! There is one downside though: you will need to create the model and field names starting with x_. This is not the case when you create a new model in a new module so it is probably better to create models that way.
In case you should do it from somewhere remote you can do it with calls though! This could be done with the XML-RPC library but we would need a lot more code so for this example I’m going back to ERPpeek!
Create a new Python file named ‘create_new_model.py’, import ERPpeek and create a client:

In order to create a new model you will need access to the models ‘ir.model’ and ‘ir.model.fields’ so create two variables for this:

The first step is to create a new model, which will be empty, and after this is done you can add fields to it. First create a new model:

So what does this do? It creates a new model with the name ‘x_example_tutorial’ and the text will be ‘Example’ model. The model_proxy.create will finally create the new model.
Now that we have a new model the last thing we need to do is add fields to the model. Create a new dictionary with the field and all settings you want to specify:

This will create a new field with the name ‘x_name’, the type is a char with a length of 64 characters, the description of the field is ‘Firstname’ and we will simply add it on the just created model. field_proxy.create will eventually insert the new field in the database!

Thats it, congratulations! You now know quite a lot about XML-RPC calls and ERPpeek to access Odoo databases.

2. Conclusion

Using simple XML-RPC calls is a very powerfull tool to access and manage your Odoo database but when you use the ERPpeek library life gets even more easy. You can create new databases in a few lines of code, you can install modules and write to the database remotely.

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




PayPal

Installing Odoo 9 enterprise on Ubuntu 14.04

Hi guys,

1. Introduction

In this tutorial I will learn you how to install Odoo 9 Enterprise on Ubuntu 14.04. The script that you will use is based on the code from André Schenkels but has been updated, improved and I’ve added Enterprise support. Do notice that you will need to be an official partner or that you need to have bought the Enterprise subscription from Odoo or you will have no access to the Github repository for the Enterprise code!

2. Downloading the script

The first step is to download my script from Github and to add the code in a new .sh file on your Ubuntu machine, wherever you’d like this.
For example right under /home. Open up an Ubuntu terminal and cd to the directory where you’d like to keep the script and then create the file:

If you’re curious about how the whole code looks and works you can find it on my Github account.

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!

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.
do this with the following command:

4.Running the 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 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:
Odoo 9 Enterprise authentication
Give the script a few minutes to configure and install everything and eventually you will see something like this:
Result install script

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

5. Extra information about Odoo 9 Enterprise

Since Odoo Enterprise uses code from both http://github.com/odoo/odoo and github.com/odoo/enterprise we will separate the code with this script. This will make future upgrades easier and the code is nicely separated. This means that the default V9 code will be under /odoo/odoo-server/ and all the enterprise code will be under /odoo/enterprise/.

In the script you saw there was an option to change the Odoo port (OE_PORT). When you’d change this port number to 8070 in the install script it would be applied to /etc/your-config-file.conf and this would give you the ability to change the default port.
To apply these changes you should do the following:
Changing Odoo settings
The -c will change the configuration and memorize what you’ve changed under /etc/your-config-file.conf. Because my port was set to 8070 this is telling the Odoo that it should run on port 8070. When you would now open up your browser and navigate to http://localhost:8070/ you will see it is running there:
Odoo V9 alternative port

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




PayPal

Adding static resources (CSS and JavaScript) to Odoo

Hi guys,

In this tutorial I will learn you how to add CSS and JavaScript to your custom Odoo module. This gives you the ability to change the design of things in Odoo, to execute JavaScript (and jQuery) and opens up a whole new set of possibilities to add extra options in Odoo.

In this example I will learn you how to add new JS/CSS files to Odoo and how they should be added to Odoo by the official guidelines. At the end you can also find a link to a demo module so you can always look at a code example too!

1. Creating the XML

The first step is to create a new XML file in your custom module. Create a new XML file under yourModuleName/views, like this:
New XML file
Now the first thing to do is to tell __openerp__.py that you’ve added this new XML file, otherwise Odoo will not load it. Open up __openerp__.py and add in a line in the data block. Be sure to give it the correct path and filename:
Add the XML to __openerp__.py

Now return to your XML file. All CSS and JavaScript in Odoo is added to the assets so the first step is to create an inherit_id calling this, then we’ll use an xpath to add in our custom coded files. Adding resources is much like adding a field to a view with inheritance, you’re just referring to a file and not a field. So how does this look in code?

Let us break this down! We’ve created a new template called ‘assets_backend’, which has a name ‘static_resource_demo assets’ and which inherits ‘web.assets_backend’. The ‘web.assets_backend’ is already created by Odoo and contains all the CSS/JavaScript code so we’ll simply inherit it to add our files to it. The xpath expression is simply telling Odoo let us add those files to the main path of ‘web.assets_backend’. Simple, isn’t it?

2. Creating your CSS / JavaScript files

Alright so now that we have the basis done for adding in CSS or JavaScript files we should first create them. The guidelines of Odoo say that you should create a new folder named ‘static’, so do so. Open up this folder and create a new folder ‘src’. After you’ve created the folder ‘src’ you should create a new one named ‘css’, ‘js’ or ‘images’ depending on which file you need to add. In this example I will explain both. If you want to add CSS files read part 2.1, if you want to add JavaScript files read part 2.2 and if you want to do both, well read both.

2.1 Creating your CSS files

Create a new folder ‘css’ for this example and open it up. This is now the official structure for adding CSS files to Odoo! Now create a new CSS file in this folder. I’ve named it ‘cssfile.css’ just for demo purposes, but you can name this whatever you like. The final result will now look like this:
Folder structure for custom CSS file
So in my example I’ve created a custom module named ‘static_resources_demo’ in which I created the folders. From here on you can add all the CSS in your CSS file as you like!

This leaves us with one more thing to do. Odoo doesn’t know the path to the CSS file yet so open up your XML file again (in my case named resources.xml) and add the path to your CSS file:

An important note is that you should also add the name of your module in! The xpath and template know the path up in to the folder addons but from there on Odoo is lost if you don’t tell it where to look. So first add your module name and then continue with /static/src/css/yourFileName.css.

2.2 Creating your JavaScript files

Create a new folder ‘js’ for this example and open it up. This is now the official structure for adding JavaScript files to Odoo! Now create a new JS file in this folder. I’ve named it ‘javascriptfile.js’ just for demo purposes, but you can name this whatever you like. The final result will now look like this:
Folder structure for custom JS file
So in my example I’ve created a custom module named ‘static_resources_demo’ in which I created the folders. From here on you can add all the JS in your JS file as you like!

This leaves us with one more thing to do. Odoo doesn’t know the path to the JS file yet so open up your XML file again (in my case named resources.xml) and add the path to your JS file:

3. Conclusion

Adding CSS or JavaScript to your custom module is very easy and has a lot of possibilities. This is ideal if you want to change the layout of your Odoo, if you want to create another theme or if you want to have extra control with JavaScript. Adding extra resources gives you almost unlimited possibilties such as creating new widgets or adding external JS libraries in!

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




Creating automated actions in Odoo

Hi guys,

In this tutorial I will learn you how to create and manage automated actions, which are also known as schedulers. Schedulers are automated actions that run automatically over a time period and can do a lot of things. They give you the ability to execute actions on your database without needing manual interaction.

In this example I will create an automated action that runs every 2 minutes and which loops over all records on the database table (scheduler.demo). Every record on this model will keep track of how many times the scheduler ran over it, just to show to you how automated actions work in combination with database actions.

1. Creating the model and fields

The first step is to create a model (if you don’t have one yet) and to create some fields on this model:

This will create a new model named ‘scheduler.demo’ which will contain three fields: a text field for the name, an integer field to keep track of how many times the record was updated and a date field. This is all you need on the database level. You have your fields in the database so you only need to show them to the user now!

2. Creating the views

Now that you have the database part ready it is time to create your view. In this example I will create a simple form view and tree view which shows the fields ‘name’, ‘numberOfUpdates’ and ‘lastModified’ that we just created in our model:

3. Creating the automated action

Now that we’ve created the models and the views we need an automated action that will run automatically with a specific interval.

Tip: If you want to build new modules in the guidelines from Odoo you should add the code for an automated action under yourDefaultModule/data/ in a separate XML file.

An important thing to note with automated actions is that they should always be defined within a noupdate field since this shouldn’t be updated when you update your module. So let us create an automated action that runs every 2 minutes that keeps running over and over again for the model ‘schedule.demo’. A last criteria is that the automated action calls a Python function from where we can do our operations on the database. The result:

When you would install your module this would give the following automated action:
Automated action example

3.1 Diving deeper into the automated action

Lets break down the code step by step so that we know exactly what it does. If you don’t want to read this you can scroll down to chapter 4.
The first thing you’ll notice is the data noupdate=”1″, this is telling Odoo that all code within this tag shouldn’t be updated when you update your module. The next thing you’ll notice is the record id and model.

The id is an unique identifier for Odoo to know what record is linked to which id. The model called (‘ir.cron’) is the model specifically made by Odoo for all automated actions. This model contains all automated actions and should always be specified.

The next line is the name. This name will be shown on the automated action and is what the user will see.

The next line is the ‘user_id’. This user id is referring to a specific user, in most cases this will be base.user_root. Why? This is the user where the automated action is executed with and when you use the root account you’re always sure you have acces rights to everything!

The next line is the ‘interval_number’. The interval_number is always a number and will tell you how often the automated action should run. In this example it will run every 2 … The next line then specified is the ‘interval_type’ which is telling you how often you want to run the ‘interval_number’. When we look at the next line:

The line ‘interval_number’ and ‘interval_type’ are always combined and give you an exact timing on how often the automated action should be run. In this example the automated action will run every 2 minutes.
Tip: the options for ‘interval_type’ are ‘minutes’,’hours’,’days’, ‘work_days’,’weeks’ and ‘months’.
The next line is the field ‘numbercall’:

This field tells Odoo how often this automated action should be run. If you would fill in ’10’ then the action would run 10 times. When you want it to run forever you simply fill in ‘-1’.

The next line, ‘doall’ has two options: True or False. When the field is set to False you’re telling Odoo that missed automated actions should be executed when the server restarts if they where missed (for example when a server has been down for 24 hours then the planner would need to run the scheduler for every time it was missed in those 24 hours).

The field ‘model’ specifies on which model the automated action should be called, it is as simple as that! Now for the last line:

This line is telling Odoo that it should call a function with the name ‘process_demo_scheduler_queue’ from the Python file. So this is the link to your Python and this again gives us the ability to acces the database.

Congratulations, you now know how automated actions work!

4. Adding the Python function

Alright, we’re almost done! There is just one more thing to do: create the Python function that is called by the automated action. On the code from the automated action you could see it is calling a Python function ‘process_demo_scheduler_queue’, so let us create this in the Python file that uses the model ‘scheduler.demo’:

From here on you can do anything you like! You can call models, search values, update values, do calls to external systems, … In this example I will loop over all records on the table ‘schedule.demo’ and I will increase the value from the field ‘numberOfUpdates’ by 1 every time the scheduler is run. The code:

So, what does this function exactly do? It will get all the ids from the table ‘schedule.demo’, we will then loop over one record at a time and by the id from the record we will find all the values from the specific record. After having the record details we will update the value from the field ‘numberOfUpdates’ and we will finally update the value in the database!

5. Conclusion

Automated actions are a very powerful tool in Odoo and the options are almost unlimited. Automated actions are ideal for automatically doing things in Odoo without having to do them manually. It is a great feature in Odoo to save you time and to make things go smoother.

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




Creating and managing statusbars (selections) in Odoo

Hi guys,

In this tutorial I will learn you how to create and manage statusbars. Statusbars are selections (on database level) which are visually respresented by Odoo. They give you the ability to follow a specific flow and can be very handy to show you the progress that has been made. After this tutorial you can create and manage your own statusbars.

In this example I will create a new form view which contains a statusbar and I’ll add buttons on the form which change the state of the record.

1. Creating the model and fields

The first step is to create a model (if you don’t have one yet) and to add a selection field:

This creates a new model with the name ‘statusbar.demo’ which contains two fields, the field ‘name’ and the field ‘state’. The state field is of the type selection, which means it will contain multiple values which can be chosen. A selection is always built up in the same way, the first part if the database name and the second part is the text that will be shown to the user. For example:

If you wish to change the state to ‘In progress’ you will need to tell the database about ‘progress’, since this is the name the database listens to, while the name shown to the user will be ‘In progress’.
This is all you need on the database level. You have your selection values in the database so you only need to show them to the user now!

2. Creating the view

Now that you have the database part ready it is time to create your view. In this example I will create a simple form view which shows the field ‘name’ and the statusbar with some buttons:

So what does this tell us? Let us break this down part by part. The first thing you will notice is the field header.

Everything within these tags will be shown at the top of your Odoo form.
In this code example it will create four buttons and it will also show the field ‘state’. The field ‘state’ has a widget set to it that is made by Odoo and which will make the typical statusbar, just by adding a widget! This will generate the following bar at the top of your Odoo screen:
Statusbar and button example
Now there is one more thing I should explain, the buttons. By using those buttons we will modify in which state we are on the statusbar (selection) and we will control the whole flow. Let us take the button ‘In progress’ as an example to explain the code:

So what does this do exactly? It gives the button the text ‘In progress’ and we’ll give the button the type object, this will make it accessible in a function from the Python code later on. The name of the button will be the name of the function in your Python code and last but not least is the attrs. The attrs is a handy feature to make buttons visible/invisible when you’re in a state for example. In this example the button ‘In progress’ will not be shown when the statusbar is in the state ‘progress’.
Tip: If you have multiple states where buttons should be hidden you can use domains! For example:[(‘state’,’=’,[‘progress’,’finished’])]
Makes sense right? Let us continue and create the button functions!

3. Adding functions for the buttons

Now that the view is ready and you have buttons they should also trigger something. Open up your Python file and create a function for all of those button names you have. Every button will have a separate Python function and will modify the current record with a write. In this tutorial I will create four functions that simply change the state of what the current record is in:

When you now click on the button ‘In progress’ it will trigger the function ‘progress_progressbar’. The self.write will be triggered, which is telling Odoo that it should write on the current record, and it will modify the state to ‘finished’. Depending on in which state you are you’ll see buttons showing up and dissapearing. This is controller by the attrs value on the buttons. For example when I save my record and set it to in progress:
Statusbars in progress

4. Conclusion

Statusbars (selections) are very handy in Odoo and give you the ability to show the progress to the user in a visual way. Statusbars are ideal to use in combination with buttons which will modify the state where a record is in. Another great thing about statusbars is that they’re always at the exact same location and you can find out what the progress is within seconds.
Do you want to try a demo module and see the source code of this tutorial? You can view on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy




Installing Odoo 9 on Ubuntu 14.04

Hi guys,

1. Introduction

In this tutorial I will learn you how to install Odoo 9 on Ubuntu 14.04. The script that you will use is based on the code from André Schenkels but has been updated and improved.

2. Downloading the script

The first step is to download my script from Github and to add the code in a new .sh file on your Ubuntu machine, wherever you’d like this.
For example right under /home. Open up an Ubuntu terminal and cd to the directory where you’d like to keep the script and then create the file:

If you’re curious about how the whole code looks and works you can find it on my Github account.

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!

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.
do this with the following command:

4.Running the 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, …
Give the script a few minutes to configure and install everything and eventually you will see something like this:
Result install script

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

5. Extra information

In the script you saw there was an option to change the Odoo port (OE_PORT). When you’d change this port number to 8070 in the install script it would be applied to /etc/your-config-file.conf and this would give you the ability to change the default port.
To apply these changes you should do the following:
Changing Odoo settings
The -c will change the configuration and memorize what you’ve changed under /etc/your-config-file.conf. Because my port was set to 8070 this is telling the Odoo that it should run on port 8070. When you would now open up your browser and navigate to http://localhost:8070/ you will see it is running there:
Odoo V9 alternative port

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