diff --git a/datamodel/core.py b/datamodel/core.py index ee6c5a25e..c8749eef0 100644 --- a/datamodel/core.py +++ b/datamodel/core.py @@ -2,7 +2,6 @@ # Copyright 2019 ACSONE SA/NV # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) -import functools import logging from collections import OrderedDict, defaultdict from contextlib import ExitStack @@ -20,7 +19,6 @@ except ImportError: _logger.debug("Cannot import 'marshmallow_objects'.") - # The Cache size represents the number of items, so the number # of datamodels (include abstract datamodels) we will keep in the LRU # cache. We would need stats to know what is the average but this is a bit @@ -53,7 +51,7 @@ def _get_nested_schemas(schema): class DatamodelDatabases(dict): - """ Holds a registry of datamodels for each database """ + """Holds a registry of datamodels for each database""" class DatamodelRegistry(object): @@ -208,12 +206,11 @@ class AnotherDatamodel(Datamodel): _inherit = None def __init__(self, context=None, partial=None, env=None, **kwargs): - self._env = env + self._env = env or type(self)._env super().__init__(context=context, partial=partial, **kwargs) @property def env(self): - """ Current datamodels registry""" return self._env @classmethod @@ -408,10 +405,7 @@ def __init__(self, env, registry): def __getitem__(self, key): model = self.registry[key] - if hasattr(model, "__datamodel_init_patched"): - return model - - model.__init__ = functools.partialmethod(model.__init__, env=self.env) + model._env = self.env @classmethod def __get_schema_class__(cls, **kwargs): @@ -420,7 +414,6 @@ def __get_schema_class__(cls, **kwargs): return cls model.__get_schema_class__ = __get_schema_class__ - setattr(model, "__datamodel_init_patched", True) # noqa: B010 return model diff --git a/datamodel/tests/test_build_datamodel.py b/datamodel/tests/test_build_datamodel.py index f36cb8fdc..697fc16ea 100644 --- a/datamodel/tests/test_build_datamodel.py +++ b/datamodel/tests/test_build_datamodel.py @@ -5,6 +5,8 @@ import mock from marshmallow_objects.models import Model as MarshmallowModel +from odoo import SUPERUSER_ID, api + from .. import fields from ..core import Datamodel from .common import DatamodelRegistryCase, TransactionDatamodelCase @@ -40,7 +42,7 @@ class Datamodel2(Datamodel): self.assertIsInstance(Datamodel2(), MarshmallowModel) def test_no_name(self): - """ Ensure that a datamodel has a _name """ + """Ensure that a datamodel has a _name""" class Datamodel1(Datamodel): pass @@ -50,7 +52,7 @@ class Datamodel1(Datamodel): Datamodel1._build_datamodel(self.datamodel_registry) def test_register(self): - """ Able to register datamodels in datamodels registry """ + """Able to register datamodels in datamodels registry""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -67,7 +69,7 @@ class Datamodel2(Datamodel): ) def test_inherit_bases(self): - """ Check __bases__ of Datamodel with _inherit """ + """Check __bases__ of Datamodel with _inherit""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -90,7 +92,7 @@ class Datamodel3(Datamodel): ) def test_prototype_inherit_bases(self): - """ Check __bases__ of Datamodel with _inherit and different _name """ + """Check __bases__ of Datamodel with _inherit and different _name""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -188,7 +190,7 @@ class Datamodel4(Datamodel): ) def test_custom_build(self): - """ Check that we can hook at the end of a Datamodel build """ + """Check that we can hook at the end of a Datamodel build""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -204,7 +206,7 @@ def _complete_datamodel_build(cls): self.assertTrue(self.env.datamodels["datamodel1"]._build_done) def test_inherit_attrs(self): - """ Check attributes inheritance of Datamodels with _inherit """ + """Check attributes inheritance of Datamodels with _inherit""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -236,7 +238,7 @@ def say(self): self.assertEqual("foo bar", datamodel2.say()) def test_duplicate_datamodel(self): - """ Check that we can't have 2 datamodels with the same name """ + """Check that we can't have 2 datamodels with the same name""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -250,7 +252,7 @@ class Datamodel2(Datamodel): Datamodel2._build_datamodel(self.datamodel_registry) def test_no_parent(self): - """ Ensure we can't _inherit a non-existent datamodel """ + """Ensure we can't _inherit a non-existent datamodel""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -261,7 +263,7 @@ class Datamodel1(Datamodel): Datamodel1._build_datamodel(self.datamodel_registry) def test_no_parent2(self): - """ Ensure we can't _inherit by prototype a non-existent datamodel """ + """Ensure we can't _inherit by prototype a non-existent datamodel""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -276,7 +278,7 @@ class Datamodel2(Datamodel): Datamodel2._build_datamodel(self.datamodel_registry) def test_add_inheritance(self): - """ Ensure we can add a new inheritance """ + """Ensure we can add a new inheritance""" class Datamodel1(Datamodel): _name = "datamodel1" @@ -297,7 +299,7 @@ class Datamodel2bis(Datamodel): Datamodel2bis, Datamodel2, self.env.datamodels["datamodel1"], - self.env.datamodels["base"], + self.env.datamodels.registry.get("base"), ), self.env.datamodels["datamodel2"].__bases__, ) @@ -339,7 +341,7 @@ class Datamodel1(Datamodel): self.env.datamodels["datamodel1"](field_str="1234") def test_nested_model(self): - """ Test nested model serialization/deserialization""" + """Test nested model serialization/deserialization""" class Parent(Datamodel): _name = "parent" @@ -366,7 +368,7 @@ class Child(Datamodel): self.assertEqual(new_instance.child.field_str, instance.child.field_str) def test_list_nested_model(self): - """ Test list model of nested model serialization/deserialization""" + """Test list model of nested model serialization/deserialization""" class Parent(Datamodel): _name = "parent" @@ -469,6 +471,9 @@ class Item(Datamodel): self.assertEqual(instance.items[0].env, self.env) schema = instance.items[0].get_schema() self.assertEqual(schema._env, self.env) + another_env = api.Environment(self.env.registry.cursor(), SUPERUSER_ID, {}) + new_p = another_env.datamodels["parent"]() + self.assertEqual(new_p.env, another_env) class TestRegistryAccess(TransactionDatamodelCase):