Creating security groups in Odoo

Hi guys,

In this tutorial I will teach you how to create security groups, how to add specific users to these groups and how to show/hide menu’s depending on in which group the user is in. An example of the result after this tutorial:
Example settings under user

1. Creating a model

The first thing to do is to create a model. If you already have an existing model you can skip this chapter and continue with part 2.
Still reading? Alright, let us create a new model. In this tutorial I will create a new model named ‘demo.access.rights’, with just one simple field. Create a new Python file under the ‘models’ directory and create a new model with a field:

2. Creating the security groups

Alright, now that we have a model, the next thing to do is to create the security groups with the specific rights that you would like to give to this group.
First of all open up the ir.model.access.csv file (under security/) and look at the top of the file:
Security CSV file
When you look at this file you’ll see that there are a few columns. Let me explain them a bit into detail:

  • Id: An unique identifier for the record (they should always be unique!)
  • Name: This is the description that is shown in the front-end and is the name for the security group.
  • model_id:id: The name of the model where you want to create a security rule for. Replace the ‘.’ by an ‘_’. If you have a model named ‘this.model’ it should become ‘this_model’ and it should always be prefixed with model_. So this would become ‘model_this_model’ in my example.
  • group_id:id: An unique name of the group.
  • perm_read: If this is set to 1 it means that all users that are in this group have read acces on this model. If it is set to 0 it means that the users don’t have read rights.
  • perm_write: If this is set to 1 it means that all users that are in this group have write acces on this model. If it is set to 0 it means that the users don’t have write rights.
  • perm_create: If this is set to 1 it means that all users that are in this group have create acces on this model. If it is set to 0 it means that the users don’t have create rights.
  • perm_unlink: If this is set to 1 it means that all users that are in this group have delete acces on this model. If it is set to 0 it means that the users don’t have delete rights.

Let us start writing security groups. In my example I will create two groups: one with full rights for reading, writing, creating and deleting (admin behaviour) and one group that can only read records. Let me show you the result and I will then explain it further!

Look at those two rules for a minute and try to figure out what they do.
I’ve created two lines, which means two groups, that are both for the model ‘demo.access.rights’, where the first group has all rights on the model and the second group can only read data (look at the 1,1,1,1 and the 1,0,0,0).
Do you notice something else? There is a group_id in the field named ‘user_access_rights_demo.group_manager’ on the first line and ‘user_access_rights_demo.group_user’ on the second line. What do these two mean? Well, the first part (‘user_access_rights_demo’) is the name of the module where you are creating groups for. The second part, ‘group_manager’ will link to an XML record in which we specify the rest of the details. Add those lines to your CSV file, save it and then close it.

3. Creating the groups and selection in XML

Now that you’ve written the groups in the CSV we have the rules but in the CSV file they link to ‘group_manager’ and ‘group_user’, which we still haven’t made anywhere. So let us make them!
Create a new XML file under the ‘security’ folder so that we can write the XML side. In my example I’ve named the file ‘user_groups.xml’ as you can see here:
XML security file
We now need three things:

  • A group named ‘group_user’
  • A group named ‘group_manager’
  • A record that will link these both groups in a dropdown, in order to show it in the user his form.

Let’s start by writing a record that will show our both groups in a dropdown:

This will create an option in the user his form that has the label ‘Demo module access’ and a description ‘User access level for this module’. The sequence can be used to specify where it should come in the view. When you’re done with this part it will show this selection:
Example security settings

One down, two to go. We now need to create two group records (‘group_user’ and ‘group_manager’) so that the CSV can find and use these groups. The code should look like this:

Let me explain the code a bit more. The ‘name’ field will be the text that is shown to the user in the front-end.
By setting the ‘users’ field (which links to another group user) you can say that by default users that belong to the other group should be added to this specific group. For example: with users set to ‘base.user_root’ I will by default enable this value for the user. By setting the field ‘implied_ids’ on the ‘group_manager’ I’m saying that if the user has the manager rights that he should also have rights as a user (ref(‘user_access_rights_demo.group_user’) does this).

That is all! You’ve just created your own security groups and made them available on the user his form so that you can configure this for every user on his own.

4. Applying groups to views

After you’ve created your own groups and rules you’ve got a broad set of options to configure Odoo depending on the user his rights.
Let me give you an example with two menuitems that are available for one of two groups. If you do not need to know this you can skip to the conclusion (part 5), otherwise read on!
Alright, I will create two menuitems, one of them will be available to the user group and both menu’s will be available to the manager group. How would this look in XML? Like this:

This will create a new top menu named ‘Access rights demo’, with a menu named ‘Rights demo’, with two menuitems named ‘User records’ and ‘Admin records’, which looks like this:
Example menuitems
By simply adding the ‘groups=’ tag on the menuitems I can specify which group can access which menuitem! It is as simple as that. When I would now create a new user with the ‘User’ rights he would only see the menuitem for the user and not the admin menu:
User menuitem

5. Conclusion

The security groups and security rules are not very hard to set-up but allow you a very broad range of configuration. You can specify what a user can see just by in which groups or security rules the user is in. This allows you to show or hide menuitems, fields and options with very little effort. The possibilities are endless!

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

Updating your Odoo from Github

Hi guys,

In this tutorial I will teach you how to update your Odoo code from Github. In this tutorial you will learn how to use terminal commands to update your local Odoo installation.

In this example I will learn you how to update your Odoo environment from Github and how you can update your database in order to use the newest code.

1. Navigating in to the correct folder

The first thing that you will need to do is to navigate in to the correct folder from your terminal. Usually your Odoo is installed right under the root folder and has a structure along the lines of /odoo/odoo-server. In my local example I’ve installed an Odoo 9 under /odoo9/odoo9-server:
Odoo folder location
So, how do you know in which folder you need to be? if you see the same structure as in my above screenshot you’re in the right place. You should see the odoo.py and addons folder.

2. Fetch content from Github

Now that you’re in the correct folder you should fetch the content from Github in order to apply it locally later on. Fetch the content from Github:

So, what does this line do? Sudo will execute this command with administrator priveleges, git fetch will tell the system that we want to fetch content from github, origin refers to the origin where Odoo comes from (http://github.com/odoo/odoo) and 9.0 is the Odoo version.
Tip: If you have Odoo version 8 you should replace 9.0 with 8.0.
After running this command you will see the following result:
Github fetch origin

3. Apply all changes

You now have everything fetched and you should now tell the system to apply this. At this point the system knows which files and changes need to be made, but you didn’t make them yet. You can do this with git reset. The code:

Tip: git reset –hard will apply all changes, no matter what. If you did add custom code in already existing modules (so in the source code and not in a self made module) you should not use this. Your custom code will be overwritten with this! This is however against all the rules, so if you do code in the source code please start with creating your own modules in place of adding in the source code.
Tip #2: Do you have another version? Simply change 9.0 with the Odoo version you have running. For example if you have Odoo 8:

After you’ve used this command you will see a result similar to this:
Github reset hard example
So, what does this refer to? If I open up Github and look at the latest change that has been made to Odoo you will see that these match up:
Github reset result on Github
As you can see your Odoo code is now up-to-date and is at the latest change that has been made by Odoo.

4. Updating the database

Congratulations, you’ve already updated your whole Odoo code! That was simple, right? There is just one more thing to do. You should now update your database so that the database knows about all changes and applies them in the database.
There are multiple ways to do this but the most simple one is to restart the Odoo service with the correct parameters to update the odoo. Every Odoo has a service running and usually has a name along the lines of odoo-server. Type the following command in your terminal:

Let us break down the whole command to know which part does what.
sudo service odoo9-server restart tells the system to restart a service (so, an Odoo environment). -u all will tell the Odoo to update all modules and -d YourDatabaseName will tell the Odoo to do these changes on the database named ‘YourDatabaseName’.
Tip: Want to know more about the possibilities of the command-line interface? Have a look at the official documentation!

That is all, you’re done! After you’ve run this command you’ve updated the whole database with the latest code.

5. Conclusion

Updating an Odoo environment from Github is actually really easy. Your users will not notice anything (besides of a minor delay when reloading the database) and your system will not need to go down when updating an environment.

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




PayPal

Odoo Experts

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!




PayPal

Odoo Experts

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!




Odoo Experts

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!




Odoo Experts

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!




Odoo Experts