Odoo : make computed field inherits permissions from dependencies - odoo

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

Related

How to get Child class in inherited parent method in Ruby?

I am writing classes for a SQL Database in Ruby, a few of the query methods are common throughout all my classes so I want to move these methods into a parent class. I can't figure out how to get the original class call to show up in the parent class. Since my classes are the names of my tables, I need them to have access to them in my parent method. I've tried using self.class but that just returns Class, not the actual class I'm using the method on.
Here is the code I want to move into the parent class:
require_relative 'questions_databse.rb'
class User
attr_accessor :id, :fname, :lname
def self.find_by_id(id)
user = QuestionDatabase.instance.execute(<<-SQL, id)
SELECT
*
FROM
users
WHERE
users.id = ?
SQL
user.map { |user| User.new(user) }.first
end
Now what I need to do is something like this:
require_relative 'questions_databse.rb'
require_relative 'modelbase.rb'
class User < ModelBase
attr_accessor :id, :fname, :lname
# no self.find_by_id(id)
Then I want the parent class to do something like this:
require 'active_support/inflector'
class ModelBase
def self.find_by_id(id)
object = QuestionDatabase.instance.execute(<<-SQL, id)
SELECT
*
FROM
#{self.chid_class.name.tableize}
WHERE
#{self.chid_class.name.tableize}.id = ?
SQL
object.map { |object| self.child_class.new(object) }.first
end
end
I have 4 other table classes that I use this method on, so I need to be able to have the code tell exactly what class called it so that the SQL query will run properly.
require 'active_support/inflector'
class ModelBase
def self.find_by_id(id)
p self.class.name
end
end
User.find_by_id(1) #=> "Class"
That is result when I use the self.class.name
I am learning how to code right now and this is the problem that the lesson is giving me. I know there may be easier ways to do this, but I probably haven't learned those yet.
In a class method like your find_by_id:
class ModelBase
def self.find_by_id(id)
p self.class.name
end
end
self is the class itself (so self.class is Class) so you want to look at self.name:
class ModelBase
def self.find_by_id(id)
p name
end
end

How to override wizard's method on odoo 12

I am trying to override a single method on wizard's class that gets executed when the user click submit.
account_consolidation_custom/wizard/CustomClass.py
class AccountConsolidationConsolidate(models.TransientModel):
_name = 'account.consolidation.consolidate_custom'
_inherit = 'account.consolidation.base'
def get_account_balance(self, account, partner=False, newParam=False):
....my custom code...
account_consolidation_custom/__manifest_.py
{
'name': "account_consolidation_custom",
'summary': """""",
'description': """
""",
'author': "My Company",
'website': "http://www.yourcompany.com",
'category': 'Uncategorized',
'version': '0.1',
'depends': ['base','account_consolidation'],
# always loaded
'data': [],
}
The method's name is exactly the same as the original, but when I click on the submit button, nothing seems to happen, is still calling the method from the base module instead of the custom.
Do you know how to get only one method overwritten instead of the whole wizard class?
You're creating a new wizard/transient model when giving different values to the private attributes _name and _inherit. Instead you should use the original odoo model name account.consolidation.consolidate to both attributes or just remove the _name attribute completely.
Odoo has it's own inheriting mechanism, which is managed by the three class attributes _name, _inherit and _inherits.
I was able to make it work using the following code:
class AccountConsolidationConsolidate(models.TransientModel):
_inherit = 'account.consolidation.consolidate'
def get_account_balance(self, account, partner=False, newParam=False):
....my custom code...
After that I was able to overwrite the base methods.

How to inherit or orverride #classmethod in 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

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