diff --git a/openerpweb/nonliterals.py b/openerpweb/nonliterals.py index acca17a2c26..4dc6abfcba0 100644 --- a/openerpweb/nonliterals.py +++ b/openerpweb/nonliterals.py @@ -57,6 +57,7 @@ class Domain(object): "and a domain string") self.session = session + self.own = {} if domain_string: self.key = binascii.hexlify( hashlib.sha256(domain_string).digest()[:SHORT_HASH_BYTES_SIZE]) @@ -79,8 +80,10 @@ class Domain(object): context (as well as the session's base context), and returns the evaluated result. """ - return eval(self.get_domain_string(), - self.session.evaluation_context(context)) + ctx = self.session.evaluation_context(context) + if self.own: + ctx.update(self.own) + return eval(self.get_domain_string(), ctx) class Context(object): def __init__(self, session, context_string=None, key=None): @@ -103,10 +106,11 @@ class Context(object): "and a domain string") self.session = session + self.own = {} if context_string: self.key = binascii.hexlify( hashlib.sha256(context_string).digest()[:SHORT_HASH_BYTES_SIZE]) - self.session.domains_store[self.key] = context_string + self.session.contexts_store[self.key] = context_string elif key: self.key = key @@ -118,12 +122,15 @@ class Context(object): the first place. :type session: openerpweb.openerpweb.OpenERPSession """ - return self.session.domains_store[self.key] + return self.session.contexts_store[self.key] def evaluate(self, context=None): """ Forces the evaluation of the linked context, using the provided context (as well as the session's base context), and returns the evaluated result. """ + ctx = self.session.evaluation_context(context) + if self.own: + ctx.update(self.own) return eval(self.get_context_string(), - self.session.evaluation_context(context)) + ctx) diff --git a/openerpweb/openerpweb.py b/openerpweb/openerpweb.py index 20d6fc60788..1c3183bc394 100644 --- a/openerpweb/openerpweb.py +++ b/openerpweb/openerpweb.py @@ -70,6 +70,7 @@ class OpenERPSession(object): self.model_factory = model_factory self.context = {} + self.contexts_store = {} self.domains_store = {} def proxy(self, service): diff --git a/openerpweb/tests/test_nonliterals.py b/openerpweb/tests/test_nonliterals.py index c522bfcb6be..938b44ee728 100644 --- a/openerpweb/tests/test_nonliterals.py +++ b/openerpweb/tests/test_nonliterals.py @@ -44,15 +44,23 @@ class NonLiteralDomainTest(unittest2.TestCase): self.assertEqual( result, [('a', '=', 3)]) + def test_own_values(self): + self.session.evaluation_context.return_value = {} + domain = Domain(self.session, "[('a', '=', self)]") + domain.own = {'self': 3} + result = domain.evaluate() + self.assertEqual( + result, [('a', '=', 3)]) + class NonLiteralContextTest(unittest2.TestCase): def setUp(self): self.session = mock.Mock(spec=openerpweb.openerpweb.OpenERPSession) - self.session.domains_store = {} + self.session.contexts_store = {} def test_store_domain(self): c = Context(self.session, "some arbitrary string") self.assertEqual( - self.session.domains_store[c.key], + self.session.contexts_store[c.key], "some arbitrary string") def test_get_domain_back(self): @@ -68,7 +76,7 @@ class NonLiteralContextTest(unittest2.TestCase): key = Context(self.session, "some arbitrary string").key self.assertEqual( - Domain(self.session, key=key).get_domain_string(), + Context(self.session, key=key).get_context_string(), "some arbitrary string") def test_key_and_string(self): @@ -82,10 +90,19 @@ class NonLiteralContextTest(unittest2.TestCase): self.assertEqual( result, [('a', '=', 3)]) + def test_own_values(self): + self.session.evaluation_context.return_value = {} + context = Context(self.session, "{'a': self}") + context.own = {'self': 3} + result = context.evaluate() + self.assertEqual( + result, {'a': 3}) + class NonLiteralJSON(unittest2.TestCase): def setUp(self): self.session = mock.Mock(spec=openerpweb.openerpweb.OpenERPSession) self.session.domains_store = {} + self.session.contexts_store = {} def test_encode_domain(self): d = Domain(self.session, "some arbitrary string")