Call function from other class odoo 9 - odoo

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

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

On odoo 8, init() method on inherited model does not get called whatsoever

On odoo 8.0 I need to override parent model init() method, but the new init() is never called upon upgrading the module
Is there some obscure undocumented odoo trick for this?
class A(models.Model):
_name = "A"
_auto = False
columnA = ....
def init(self, cr):
# ... here there is a CREATE VIEW ....
# in another module...
class A(models.Model)
_inherit = "A"
_auto = False
columnB = ....
def init(self, cr):
#... NEW VIEW DEFINITION ...
Upon upgrading the module, inherited init() never gets called.
It always calls parent (class A) init method.
Amazingly, columnB is created in the modelA, hence inherited class is considered.... but at run time, its init() method is not called*
Verified in debug mode and with breakpoints set

Odoo add function to defaul create action

I've got this function
def add_default_docs(self):
for r in self:
id = self.id
labs_archive_journal_type_id = r.journal_type_id.id
archive_doc_name_ids = self.env['labs.archive.journal_type'].search([('id', '=', labs_archive_journal_type_id)]).archive_doc_name_ids
for n in archive_doc_name_ids:
self.env['labs.archive.document'].create({
"archive_journal_id": id,
"name": n.id
})
How could I call it when I press 'Create' button and create new record?
Whichever model you want to fire this function on creation on new model, inherit that model if that model is odoo built-in or if your own model, within that model definition inherit create method as following:
class ClassName(models.Model)
_inherit = 'model.name'
#api.multi
def create(self, vals):
records = super(ClassName, self).create(vals)
records.add_default_docs()
return records
If your model is defined in your own custom module, you can just insert this create function within that model definition, you won't need to inherit in new class.

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

Disable invitation email when creating user

I don't know how to disable sending email invitation when i create or import user.
I tried to override auth_signup module with this code but i have a recursion error: Unknown error during import: : maximum recursion depth exceeded at row 2
And the code :
class res_users(models.Model):
_inherit = 'res.users'
#api.model
def create(self, vals):
user = super(res_users, self).with_context(no_reset_password=True).create(vals)
return user
with_context will cause a recursion error when applied with super. super calls the base class, which is not what you need. What you need is to update the context of the current instance of the class, which is self.
Therefore this should work:
class res_users(models.Model):
_inherit = 'res.users'
#api.model
def create(self, vals):
user = super(res_users, self.with_context(no_reset_password=True)).create(vals)
return user