Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions datamodel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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


Expand Down
31 changes: 18 additions & 13 deletions datamodel/tests/test_build_datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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__,
)
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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):
Expand Down