Skip to content

Commit

Permalink
Merge pull request odoo#5439 from guewen/test-inherits-3-levels
Browse files Browse the repository at this point in the history
Reload fields of parent inherits, fixes odoo#5398
  • Loading branch information
rco-odoo committed Feb 26, 2015
2 parents e9b391c + 608e58c commit 0c1b95d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions openerp/addons/test_inherits/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
from . import models
16 changes: 16 additions & 0 deletions openerp/addons/test_inherits/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
{
'name': 'test-inherits',
'version': '0.1',
'category': 'Tests',
'description': """A module to verify the inheritance using _inherits.""",
'author': 'Camptocamp',
'website': 'http://www.camptocamp.com',
'depends': ['base'],
'data': [
'ir.model.access.csv',
'demo_data.xml',
],
'installable': True,
'auto_install': False,
}
19 changes: 19 additions & 0 deletions openerp/addons/test_inherits/demo_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<openerp>
<data>
<record id="unit_a" model="test.unit">
<field name="name">Unit A</field>
<field name="state">a</field>
</record>

<record id="box_a" model="test.box">
<field name="unit_id" ref="unit_a"/>
<field name="field_in_box">A</field>
</record>

<record id="pallet_a" model="test.pallet">
<field name="box_id" ref="box_a"/>
<field name="field_in_pallet">A</field>
</record>

</data>
</openerp>
4 changes: 4 additions & 0 deletions openerp/addons/test_inherits/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_test_unit,access_test_unit,model_test_unit,,1,1,1,1
access_test_box,access_test_box,model_test_box,,1,1,1,1
access_test_pallet,access_test_pallet,model_test_pallet,,1,1,1,1
41 changes: 41 additions & 0 deletions openerp/addons/test_inherits/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
from openerp import models, fields, api, osv


# We just create a new model
class Unit(models.Model):
_name = 'test.unit'

_columns = {
'name': osv.fields.char('Name', required=True),
'state': osv.fields.selection([('a', 'A'), ('b', 'B')],
string='State'),
}

surname = fields.Char(compute='_compute_surname')

@api.one
@api.depends('name')
def _compute_surname(self):
self.surname = self.name or ''


# We want to _inherits from the parent model and we add some fields
# in the child object
class Box(models.Model):
_name = 'test.box'
_inherits = {'test.unit': 'unit_id'}

unit_id = fields.Many2one('test.unit', 'Unit', required=True,
ondelete='cascade')
field_in_box = fields.Char('Field1')


# We add a third level of _inherits
class Pallet(models.Model):
_name = 'test.pallet'
_inherits = {'test.box': 'box_id'}

box_id = fields.Many2one('test.box', 'Box', required=True,
ondelete='cascade')
field_in_pallet = fields.Char('Field2')
3 changes: 3 additions & 0 deletions openerp/addons/test_inherits/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from . import test_inherits
23 changes: 23 additions & 0 deletions openerp/addons/test_inherits/tests/test_inherits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from openerp.tests import common


class test_inherits(common.TransactionCase):

def test_create_3_levels_inherits(self):
""" Check that we can create an inherits on 3 levels """
pallet = self.env['test.pallet'].create({
'name': 'B',
'field_in_box': 'box',
'field_in_pallet': 'pallet',
})
self.assertTrue(pallet)
self.assertEqual(pallet.name, 'B')
self.assertEqual(pallet.field_in_box, 'box')
self.assertEqual(pallet.field_in_pallet, 'pallet')

def test_write_3_levels_inherits(self):
""" Check that we can create an inherits on 3 levels """
pallet = self.env.ref('test_inherits.pallet_a')
pallet.write({'name': 'C'})
self.assertEqual(pallet.name, 'C')
1 change: 1 addition & 0 deletions openerp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2897,6 +2897,7 @@ def _inherits_reload(cls):
cls._inherit_fields = struct = {}
for parent_model, parent_field in cls._inherits.iteritems():
parent = cls.pool[parent_model]
parent._inherits_reload()
for name, column in parent._columns.iteritems():
struct[name] = (parent_model, parent_field, column, parent_model)
for name, source in parent._inherit_fields.iteritems():
Expand Down

0 comments on commit 0c1b95d

Please sign in to comment.