Skip to main content

The business logic layer

The business logic layer

The business logic layer encodes the application's business rules, such as validations and automatic computations. We will add the logic for our button now. This is done with Python code, using the methods in the Python class that represent the model.

Adding business logic

Previously, we added a button to the Book Form section to check whether the ISBN is valid. Modern ISBNs have 13 digits, where the last one is a check digit computed from the first 12.
There is little point getting into the details of the algorithm used, so here is a Python function that implements this validation. It should be added inside the class Book(...) object, before the name field:
    @api.multi
    def _check_isbn(self):
        self.ensure_one()
        digits = [int(x) for x in self.isbn if x.isdigit()]
        if len(digits) == 13:
            ponderations = [1, 3] * 6
            terms = [a * b for a, b in zip(digits[:12], ponderations)]
            remain = sum(terms) % 10 
            check = 10 - remain if remain != 0 else 0
            return digits[-1] == check
The button_check_isbn() method of the Book model should use this function to validate the number in the ISBN field. If the validation fails, we should present the users with a warning message.
First, we need to import the Odoo API library, adding the corresponding import, as well as the Odoo Warningexception. To do this, we need to edit the library_book.py Python file so that these are the first lines of the file:
from odoo import api, fields, models
from odoo.exceptions import Warning


Next, still in the models/library_book.py file, add the following to the Book class:
@api.multi
def button_check_isbn(self): 
    for book in self:
        if not book.isbn:
            raise Warning('Please provide an ISBN for %s' % book.name)
        if book.isbn and not book._check_isbn():
            raise Warning('%s is an invalid ISBN' % book.isbn) 
    return True 
For logic on records, we use the @api.multi decorator. Here, self will represent a recordset, and we should then loop through each record. In fact, this is the default for model methods, so the @api.multi decorator could be safely omitted here. We prefer to keep it for clarity.
The code loops through all the selected Book task records and, for each one, if the Book ISBN has a value, it checks whether it is a valid ISBN. If not, a warning message is raised for the user.
The Model method does not need to return anything, but we should have it at least return a True value. The reason is that not all client implementations of the XML-RPC protocol support None/Null values, and may raise errors when such a value is returned by a method.
This a good moment to upgrade the module and run the tests again, adding the --test-enable option to confirm that tests are now passing. You can also try it live, going into a Book Form and trying the button with both correct and incorrect ISBNs.

Comments

  1. Really good work there. Informative and helpful. Appreciate it. But might be looking for Odoo ERP Development Services in Alabama

    ReplyDelete

Post a Comment

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...