Disable invitation email when creating user - odoo

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

Related

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.

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

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

Django-rest-framework, nested objects in Serializers

I would like to have a nested object inside a serializer instead of just the foreignkey (or url).
As this documentation says, I just had to specify the serializer class of the nested object in the parent serializer:
# Models
class NestedSample(models.Model):
something = models.CharField(max_length=255)
class Sample(models.Model):
thing = models.BooleanField()
nested = models.ForeignKey(NestedSample)
# Serializers
class NestedSampleSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = api_models.NestedSample
class SampleSerializer(serializers.HyperlinkedModelSerializer):
nested = NestedSampleSerializer() # HERE!
class Meta:
model = api_models.Sample
# Views
class NestedSampleViewSet(viewsets.ModelViewSet):
queryset = api_models.NestedSample.objects.all()
serializer_class = api_serializers.NestedSampleSerializer
class SampleViewSet(viewsets.ModelViewSet):
queryset = api_models.Sample.objects.all()
serializer_class = api_serializers.SampleSerializer
This works very well when I get the objects, but it is not possible to create (=POST) Sample objects anymore, I get the error:
{u'non_field_errors': [u'Invalid data']}
I tried to overwrite the create method in the viewset to get the object using the pk:
class SampleViewSet(viewsets.ModelViewSet):
queryset = api_models.Sample.objects.all()
serializer_class = api_serializers.SampleSerializer
def create(self, request):
request.DATA['nested'] = get_object_or_404(api_models.NestedSample, pk=request.DATA['nested'])
return super(SampleViewSet, self).create(request)
But it doesn't work as well.
Any idea?
I also found this question I can relate with which of course solves the problem but do not let me expose the full nested object, so back to the beginning.
Thanks,
I can think of two solutions to this problem. I prefer the first one.
First solution:
Use a django model form to create objects. Override the create and update methods. A sample create method:
def create(self, request):
form = SampleForm(data=request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
return Response(dict(id=instance.pk), status=status.HTTP_201_CREATED)
return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)
this way you can create Sample objects with any kind of validation you like.
Second solution:
Override get_serializer_class method and return serializer class based on request method. Define two serializers one for post and put and one for list and retrieve.
Can you confirm that you're sending a JSON encoded request - i.e. the request has the content type set to JSON ?
If not, the post is most probably send using form format which doesn't support nested.