Enable Dark Mode!
how-to-use-function-wrappers-in-odoo.jpg
By: Sruthi C Nair

How to Use Function Wrappers in Odoo?

Technical

In order to extend a function's behavior, wrappers enclose it within another function. Its functionality is available in Python. Make a function call inside a wrapper function inside function b, passing as an argument a function assumed to be function 'a' inside another function assumed to be function 'b'. So the possibility is that we can reuse the code.
Syntax:
@wrapper
def function(n):
    statements(s)
This is also similar to
def function(n):
    statement(s)
function = wrapper(function)
def button_conform(self,):
   amount = []
   for recs in self:
       for rec in recs.line_ids:
           amount.append(rec.amount)
   if sum(amount) > self.petty_limit:
       self.limit = True
   self.write({'state': 'requested', }
              )
def partner_name():
   partner = self.env['res.partner'].search([])
   print(partner)
   product = self.env['product.template'].search([])
   print(product)
return partner_name()
def users(*logins):
   """ Decorate a method to execute it once for each given user. """
   @decorator
   def wrapper(func, *args, **kwargs):
       self = args[0]
       old_uid = self.uid
       try:
           Users = self.env['res.users'].with_context(active_test=False)
           user_id = {
               user.login: user.id
               for user in Users.search([('login', 'in', list(logins))])
           }
           for login in logins:
               with self.subTest(login=login):
                   self.uid = user_id[login]
                   func(*args, **kwargs)
                       self.env.cache.invalidate()
       finally:
           self.uid = old_uid
   return wrapper

Wrapper Class in Python

The way to specify the management code of functions and classes is by Decorators.

The callable object is taken from the decorators themself as a form. And that process callable objects. A Class Decorator act similarly to function decorators, but the difference is the end of a class statement; they are run to rebind a class name to a callable. Similarly, when they are later created, a layer of wrapper logic manages instances just after they are created or inserted.

We can able to use directly class objects in a class decorator to manage instead of instance calls–by using nearly the same syntax and very similar coding patterns to increase/modify a class with new methods. And class decorators are strongly related to function decorators, but somewhere logic differs.

Syntax:

Syntactically, class decorators appear just before class statements.

@decorator
class Class_Name:
   ...
inst = Class_Name(50)

This part of the code is equivalent to

class Class_Name:

      ...

Class_Name = decorator(Class_Name)

inst = Class_Name(50);

   # decorator accepts a class as
   # a parameter
def decorator(cls):
   class Wrapper:
       def __init__(self, x):
           self.wrap = cls(x)
       def get_name(self):
           # fetches the name attribute
           return self.wrap.name
   return Wrapper
@decorator
class C:
   def __init__(self, y):
       self.name = y
# its equivalent to saying
# C = decorator(C)
x = C("test")
print(x.get_name())

That’s how we can wrap the functions in python.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message