[IMP] tools: add DotDict helper

This function allow a quck dot.notation access to dictionary attributes

We need to make it re-usable for a future enterprise commit
This commit is contained in:
Jeremy Kersten
2019-06-05 05:56:33 +00:00
parent 72f7139f52
commit 7a3046a38f
2 changed files with 13 additions and 7 deletions
@@ -6,6 +6,7 @@ except ImportError:
from mock import patch
from odoo.tests import tagged
from odoo.tests.common import HttpCase, TransactionCase
from odoo.tools import DotDict
''' /!\/!\
Calling `get_pricelist_available` after setting `property_product_pricelist` on
@@ -157,13 +158,6 @@ def simulate_frontend_context(self, website_id=1):
self.addCleanup(patcher.stop)
class DotDict(dict):
"""dot.notation access to dictionary attributes"""
def __getattr__(*args):
val = dict.get(*args)
return DotDict(val) if type(val) is dict else val
@tagged('post_install', '-at_install')
class TestWebsitePriceListAvailable(TransactionCase):
# This is enough to avoid a mock (request.session/website do not exist during test)
+12
View File
@@ -1243,3 +1243,15 @@ def wrap_module(module, attr_list):
raise AttributeError(attrib)
# module and attr_list are in the closure
return WrappedModule()
class DotDict(dict):
"""Helper for dot.notation access to dictionary attributes
E.g.
foo = DotDict({'bar': False})
return foo.bar
"""
def __getattr__(self, attrib):
val = self.get(attrib)
return DotDict(val) if type(val) is dict else val