Server actions underpin the Odoo automation tools. They allow us to describe the actions to perform. These actions are then available to be called by event triggers, or to be triggered automatically when certain time conditions are met.
The simplest case is to let the end user perform some action on a document by selecting it from the More button menu. We will create such an action for project tasks, to Set as Priority by starring the currently selected task and setting a deadline on it to three days from now.
We will need an Odoo instance with the Project app installed. We will also need the Developer Mode activated. If it's not, activate it in the Odoo About dialog.
To create a server action and use it from the More menu, follow these steps:
Deadline
Python expression
datetime.date.t
oday() + datetime.timedelta(days=3)
Priority
Value
1
Server actions work on a Model, so one of the first things to do is to pick the Base Model we want to work with. In our example, we used Project Tasks.
Next, we should select the type of action to perform. There are a few options available:
For our example, we used Write on a Record to set some values on the current record. We set Priority to 1
, to star the task, and set a value on the Deadline field. This one is more interesting, since the value to use is evaluated from a Python expression. Our example makes use of the datetime
Python module (see https://docs.python.org/2/library/datetime.html) to compute the date three days from today.
Arbitrary Python expression can be used there, as well as in several of the other action types available. For security reasons, the code is checked by the safe_eval
function, implemented in odoo/openerp/tools/safe_eval.py
. This means that some Python operations may not be allowed, but this rarely proves to be a problem.
The Python code is evaluated in a restricted context, where the following objects are available to use:
env
: This is a reference for the Environment object, just like self.env
in a class method.model:
This is a reference to the model class the server action acts upon. In our example, it is equivalent to self.env['project.task]
.workflow
: This is a reference to the Odoo workflow engine server object.Warning
: This is a reference to openerp.exceptions.Warning
, allowing for validations that block unintended actions. It can be used as: raise Warning('Message!')
.object
or obj
: This provides references to the current record, allowing you to access its field values and methods.log
: This is a function to log messages in the ir.logging
model, allowing for database side logging on actions.datetime
, dateutil,
and time
: These provide access to the Python libraries.