How to inherit or orverride #classmethod in odoo - odoo

I want to inherit #classmethod of class BaseModel(object)
How to inherit or override the #classmethod in our custom module ?

I just ran into this today :)
You can extend it in a couple of ways. It depends if you really need to extend BaseModel or if you need to extend a specific sub class of BaseModel.
Sub Class
For any sub class you can inherit it as you would normally:
from odoo import api, fields, models
class User(models.Model):
_inherit = 'res.users'
#classmethod
def check(cls, db, uid, passwd):
return super(User, cls).check(db, uid, passwd)
Extend BaseModel Directly
In the case of BaseModel itself you are going to need to monkey patch:
from odoo import models
def my_build_model(cls, pool, cr):
# Make any changes I would like...
# This the way of calling super(...) for a monkey-patch
return models.BaseModel._build_model(pool, cr)
models.BaseModel._build_model = my_build_model

Related

Odoo : make computed field inherits permissions from dependencies

In this case, I would like that permissions on something be inherited from account.move because it is dependent on it. How do I do that ?
class ResPartner(models.Model):
_inherit = "res.partner"
something = fields.Float('Something', compute="_compute_something")
def _compute_something(self):
self.env['account.move'].search(...)
You can use sudo() in the search method.
sudo() use for super admin permission. you can access all recodes of all modules.
class ResPartner(models.Model):
_inherit = "res.partner"
something = fields.Float('Something', compute="_compute_something")
def _compute_something(self):
self.env['account.move'].sudo().search(...)

How to access a parent class attribute without breaking data encapsulation?

In the 1994 book Design Patterns: Elements of Reusable Object-Oriented Software by the "Gang of Four", I noticed in the C++ code examples that all methods are either declared as public or protected (never as private) and that all attributes are declared as private (never as public or protected).
In the first case, I suppose that the authors used protected methods instead of private methods to allow implementation inheritance (subclasses can delegate to them).
In the second case, while I understand that avoiding public and protected attributes prevents breaking data encapsulation, how do you do without them if a subclass need access a parent class attribute?
For example, the following Python code would have raised an AttributeError at the get_salary() method call if the _age attribute was private instead of protected, that is to say if it was named __age:
class Person:
def __init__(self, age):
self._age = age # protected attribute
class Employee(Person):
def get_salary(self):
return 5000 * self._age
Employee(32).get_salary() # 160000
I have finally found out an obvious solution by myself: redeclaring the private attribute of the parent class in the subclass:
class Person:
def __init__(self, age):
self.__age = age # private attribute
class Employee(Person):
def __init__(self, age):
self.__age = age # private attribute
def get_salary(self):
return 5000 * self.__age
Employee(32).get_salary() # 160000

Call function from other class odoo 9

In a custom module I have two classes. How can class test in #api.one call test2_func on a button click?
What should I put in def call_test2_func(self)?
For example:
class test(models.Model):
_name = "test.class"
_description = "TEST"
#api.one
def call_test2_func(self):
"""call test2_func here"""
class test2(models.Model):
_name = "test2.class"
_description = "TEST 2"
#api.one
def test2_func(self):
print("TEST 2")
Maybe I should leave a reply instead of a comment. If you're using Odoo and the new OpenERP api you can can access the model dictionaty though self.env in your model classes. So to call the function test2_func in the model test2.class you should write
#api.one
def call_test2_func(self):
self.env["test2.class"].test2_func()

Inheritance of non model, core class in Odoo/OpenERP

In Odoo, I want to modify the addons/web/session/OpenERPSession class without modifying the core code. Meaning I want to subclass this class from my module so that the system will use my version of the OpenERPSession class instead of the core class. And specifically I want to alter only a method's implementation, and I do so by overriding it:
class ExtendedSession(session.OpenERPSession):
def model(self, model):
_logger = logging.getLogger(__name__)
_logger.info('OVERRIDEN ==================== OpenERPSession.model')
if self._db == False:
raise session.SessionExpiredException("Session expired")
return session.Model(self, model)
But unfortunately the 'OVERRIDEN ==================== OpenERPSession.model' statement is not print therefore the system does not call my implementation.
How can I instruct Odoo to use my implementation of the OpenERPSession?
Sorry for answering late...
For any non model class, you can inherit them by using full signature path of that class, for ex.
You can inherit session.OpenERPSession using the full path ...
class ExtendedSession(addons.web.sessions.OpenERPSession):
def model(self, model):
_logger = logging.getLogger(__name__)
_logger.info('OVERRIDEN ==================== OpenERPSession.model')
if self._db == False:
raise session.SessionExpiredException("Session expired")
return session.Model(self, model)
Try this......

How do I define in the model a class method, not an instance one?

I want to be able to call Activity.pull_latest from a controller, but if I do
class Activity < ActiveRecord::Base
def pull_latest [...]
I have to call it Activity.new.pull_latest.
How do I define in the model a class method, not an instance one?
Use self:
def self.pull_latest
Then you can call Activity.pull_latest