Book Image

Odoo Development Essentials

Book Image

Odoo Development Essentials

Overview of this book

Table of Contents (17 chapters)
Odoo Development Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding business logic


Now we will add some logic to our buttons. Edit the todo_model.py Python file to add to the class the methods called by the buttons.

We will use the new API introduced in Odoo 8.0. For backward compatibility, by default Odoo expects the old API, and to create methods using the new API we need to use Python decorators on them. First we need to import the new API, so add it to the import statement at the top of the Python file:

from openerp import models, fields, api

The Toggle Done button's action will be very simple: just toggle the Is Done? flag. For logic on a record, the simplest approach is to use the @api.one decorator. Here self will represent one record. If the action was called for a set of records, the API would handle that and trigger this method for each of the records.

Inside the TodoTask class add:

@api.one
def do_toggle_done(self):
    self.is_done = not self.is_done
    return True

As you can see, it simply modifies the is_done field, inverting its value...