Skip to main content

Extending Python methods

Extending Python methods

The business logic that's coded into Python methods can also be extended. For this, Odoo borrows the mechanism that Python already provides for inherited objects to extend their parent class behavior.
As a practical example, we will extend the Library Book ISBN validation logic. The logic provided by the base Library app only validates modern 13-digit ISBNs, but some older titles might have a 10-digit ISBN. We will extend the _check_isbn() method to also validate these cases.
Add this additional method in the library_member/models/library_book.py file:
from odoo import api, fields, models


class Book(models.Model):
    _inherit = 'library.book'
    is_available = fields.Boolean('Is Available?', readonly=False)

    @api.multi
    def _check_isbn(self):
        self.ensure_one()
        digits = [int(x) for x in self.isbn if x.isdigit()]
        if len(digits) == 10:
            ponderators = [1, 2, 3, 4, 5, 6, 7, 8, 9]
            total = sum(a * b for a, b in zip(digits[:9], ponderators))
            check = total % 11
            return digits[-1] == check
        else:
            return super()._check_isbn()
To extend a method, we define it again, and at some point, can use super() to call the method's existing implementation. In our method, we check whether it is a 10-digit ISBN, and in that case, perform the missing validation logic. Otherwise, we fall back to the original ISBN checking logic that can handle the 13-digit case.


If you want to try it, or even better, write a test case, here is an example of a 10-digit ISBN: 0-571-05686-5. It is the original ISBN of William Golding's Lord of the Flies.

Note

Changes in Odoo 11 Since Odoo 11, the main Python version that's supported is Python 3 (Python 3.5 in Odoo 12). For previous Odoo versions, using Python 2, super() needed two arguments, passing the class name and self. In that case, the last statement would use super(Book, self)._check_isbn().

Comments

Popular posts from this blog

The message and activity features

The message and activity features Odoo has available global  messaging  and activity planning features, provided by the  Discuss  application, with technical name mail. The mail module provides the  mail.thread  abstract class that makes it simple to add the messaging features to any model, and the  mail.activity.mixin  that adds planned activity features. This was done in  Chapter 4 ,  Extending Modules , to explain how to inherit features from mixin abstract classes. To add these features, we need to add the mail dependency to the add-on module,  library_checkout , and then have the library checkout model class inherit from the abstract classes providing the following features. Edit the  'depends'  key in the  library_checkout/__manifest__.py   file, to add the mail module, shown as follows: Copy 'depends': ['library_member' , 'mail' ], And edit the  library_checkout/m...

Setting up an nginx reverse proxy

Setting up an nginx reverse proxy While Odoo itself can serve web pages, it's strongly recommended that there is a  reverse  proxy positioned in front of it. A reverse proxy acts as an intermediary that manages the traffic between clients sending requests and the Odoo servers responding to them. Using a reverse proxy has several benefits. On the security side, it can do the following: Handle (and enforce) HTTPS protocols to encrypt traffic Hide the internal network characteristics Act as an application firewall, limiting the URLs accepted for processing Also, on the performance side, it can provide the following significant improvements: Cached static content, hence reducing the load on the Odoo servers Compressed content to speed up loading time Act as a load balancer, distributing load between several servers Apache is a popular choice when considering a reverse proxy, although  nginx  is a recent alternative with good technical argumen...

The QWeb template language

The QWeb template language The  QWeb  parser looks for special directives in the templates and replaces them with dynamically generated HTML. These directives are XML element attributes and can be used in any valid tag or element, such as  <div> ,  <span> , or  <field> . Sometimes, we may want to use a QWeb directive but we don't want to place it in any of the XML elements in our template. For those cases, we have a  <t>  special element that can have QWeb directives, such as  t-if  or  t-foreach , but is silent and won't have any output on the final XML/HTML produced. The QWeb directives will frequently make use of evaluated expressions to produce different results, depending on the current record values. There are two different QWeb implementations: client-side JavaScript and server-side Python. The reports and website pages use the server-side Python implementation of QWeb. Kanban views us...