Creating server actions

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.

Getting ready

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.

How to do it...

To create a server action and use it from the More menu, follow these steps:

  1. On the Settings top menu, select the Technical | Actions | Server Actions menu item, and click on the Create button at the top of the record list.
    How to do it...
  2. Fill out the server action form with these values:
    • Action Name: Set as Priority
    • Base Model: Task
    • Action To Do: Write On a Record
    • Update Policy: Update the Current Record
  3. In the server action, under the Value Mapping field, add the following lines:
    • As the first value, we will enter the following parameters:
      • Field: Deadline
      • Evaluation Type: Python expression
      • Value: datetime.date.today() + datetime.timedelta(days=3)
    • As the second value, we will enter the following parameters:
      • Field: Priority
      • Evaluation Type: Value
      • Value: 1

    The following screenshot shows the entered values:

    How to do it...
  4. Save the server action and click on the Add in the 'More' Button at the top right, to make it available in the Project task's More button.
  5. To try it out, go to the Project top menu, select the Search | Tasks menu item, and open a random task. By clicking on the More button, we should see the Set as Priority option. Selecting it will star the task and change the deadline date to three days from now.

How it works...

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:

  • Send Email allows choosing an e-mail template, and will use it to send out an e-mail when the action is triggered.
  • Trigger a Workflow Signal does just that. In Odoo Workflows, Signals can be triggered and used to fire workflow transitions.
  • Run a Client Action triggers a client or window action, just like when a menu item is clicked.
  • Create or Copy a new Record allows you to create a new record, on the current or on another Model.
  • Write on a Record allows you to set values on the current or on another record.
  • Execute Python Code allows you to write arbitrary code to execute, when none of the other options is flexible enough for what we need.

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.

There's more...

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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset