Inheritance of non model, core class in Odoo/OpenERP - odoo

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

Related

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

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

How to iterate Apache velocity template variable attributes

As title, is there any way to iterate or display Apache velocity template attributes?
for example, I have following code :
<code>
${ctx.messages.headerMessage}
</code>
And I want to know how many other attributes the variable ${ctx} has
It's only possible to discover and to loop on an object properties (that is, the ones with getters and/or setters) if you can add a new tool to your Velocity context. If you can't, you're rather stuck.
There are several ways to do this, I illustrate below how to do this with commons-beanutils.
First, add Apache commons-beanutils in your class path, and add it to your Velocity context from Java:
import org.apache.commons.beanutils.PropertyUtils;
...
context.put("beans", new PropertyUtils());
...
One remark: if you do not have access to the Java part, but if by chance commons-beanutils is already in the classpath, there is one hakish way of having access to it: #set($beans = $foo.class.forName('org.apache.commons.beanutils.PropertyUtils').newInstance()).
Then, let's say that I have the following object:
class Foo
{
public boolean isSomething() { return true; }
public String getName() { return "Nestor"; }
}
which is present in my context under $foo. Using your newly $beans properties introspector, you can do:
#set ($properties = $beans.getPropertyDescriptors($foo.class))
#foreach ($property in $properties)
$property.name ($property.propertyType) = $property.readMethod.invoke($foo)
#end
This will produce:
bar (boolean) = true
class (class java.lang.Class) = class Foo
name (class java.lang.String) = Robert
(you'll need to filter out the class property, of course)
One last remark, though. Templates are for coding the View layer of an MVC application, and doing such a generic introspection of objects in them is rather inadequate in the view layer. You're far better of moving all this introspection code on the Java side.

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

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.