From 858f61c6bed22a117a219b2b46f3d543fded51d3 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Fri, 17 Jun 2011 11:58:35 +0200 Subject: [PATCH] [imp] added compound domains and contexts bzr revid: nicolas.vanhoren@openerp.com-20110617095835-oymrh9u459hhwqa1 --- addons/base/static/src/js/data.js | 28 ++++++++++++ openerpweb/nonliterals.py | 73 +++++++++++++++++++++++++++++++ openerpweb/openerpweb.py | 4 +- 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/addons/base/static/src/js/data.js b/addons/base/static/src/js/data.js index 76a803fa0ff..2021f13f092 100644 --- a/addons/base/static/src/js/data.js +++ b/addons/base/static/src/js/data.js @@ -431,6 +431,34 @@ openerp.base.DataSetSearch = openerp.base.DataSet.extend({ } }); +openerp.base.CompoundContext = function(source_context) { + this.__ref = "compound_context"; + this.__contexts = []; + if (source_context === undefined) + return; + else if (source_context.__ref === "compound_context") + this.__contexts.concat(source_context.__contexts); + else + this.add(source_context); +}; +openerp.base.CompoundContext.prototype.add = function(context) { + this.__contexts.push(context); +}; + +openerp.base.CompoundDomain = function(source_domain) { + this.__ref = "compound_domain"; + this.__domains = []; + if (source_domain === undefined) + return; + else if (source_domain.__ref === "compound_domain") + this.__domains.concat(source_domain.__domains); + else + this.add(source_domain); +}; +openerp.base.CompoundDomain.prototype.add = function(domain) { + this.__domains.push(domain); +}; + }; // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: diff --git a/openerpweb/nonliterals.py b/openerpweb/nonliterals.py index 1584628f777..fe7a5e3cdd9 100644 --- a/openerpweb/nonliterals.py +++ b/openerpweb/nonliterals.py @@ -27,6 +27,16 @@ class NonLiteralEncoder(simplejson.encoder.JSONEncoder): '__ref': 'context', '__id': object.key } + elif isinstance(object, CompoundDomain): + return { + '__ref': 'compound_domain', + '__domains': [self.default(el) for el in object.domains] + } + elif isinstance(object, CompoundContext): + return { + '__ref': 'compound_context', + '__contexts': [self.default(el) for el in object.contexts] + } return super(NonLiteralEncoder, self).default(object) def non_literal_decoder(dct): @@ -47,6 +57,16 @@ def non_literal_decoder(dct): if 'own_values' in dct: context.own = dct['own_values'] return context + elif dct["__ref"] == "compound_domain": + cdomain = CompoundDomain() + for el in dct["__domains"]: + cdomain.domains.append(non_literal_decoder(el)) + return cdomain + elif dct["__ref"] == "compound_context": + ccontext = CompoundContext() + for el in dct["__contexts"]: + ccontext.contexts.append(non_literal_decoder(el)) + return ccontext return dct class Domain(object): @@ -138,3 +158,56 @@ class Context(object): ctx.update(self.own) return eval(self.get_context_string(), ctx) + +class CompoundDomain: + def __init__(self): + self.domains = [] + self.session = None + + def evaluate(self, context=None): + final_domain = [] + for domain in self.domains: + if not isinstance(domain, (list, nonliterals.Domain, nonliterals.CompoundDomain)): + raise TypeError("Domain %r is not a list or a nonliteral Domain", + domain) + + if isinstance(domain, list): + final_domain.extend(domain) + break + + ctx = dict(context or {}) + ctx['context'] = ctx + + domain.session = self.session + domain.extend(domain.evaluate(ctx)) + + def add(self, domain): + self.domains.append(domain) + return self + +class CompoundContext: + def __init__(self): + self.contexts = [] + self.session = None + + def evaluate(self, context=None): + final_context = dict(context or {}) + for context_to_eval in self.contexts: + if not isinstance(context_to_eval, (dict, nonliterals.Context, nonliterals.CompoundContext)): + raise TypeError("Context %r is not a dict or a nonliteral Context", + context_to_eval) + + if isinstance(context_to_eval, dict): + final_context.update(context_to_eval) + break + + ctx = dict(final_context) + ctx["context"] = ctx + + context_to_eval.session = self.session + final_context.update(context_to_eval.evaluate(ctx)) + + def add(self, context): + self.contexts.append(context) + return self + diff --git a/openerpweb/openerpweb.py b/openerpweb/openerpweb.py index 5ef4369dbb5..2ee59779cb3 100644 --- a/openerpweb/openerpweb.py +++ b/openerpweb/openerpweb.py @@ -194,7 +194,7 @@ class OpenERPSession(object): :raises: ``TypeError`` if ``context_to_eval`` is neither a dict nor a Context """ - if not isinstance(context_to_eval, (dict, nonliterals.Domain)): + if not isinstance(context_to_eval, (dict, nonliterals.Context, nonliterals.CompoundContext)): raise TypeError("Context %r is not a dict or a nonliteral Context", context_to_eval) @@ -250,7 +250,7 @@ class OpenERPSession(object): :raises: ``TypeError`` if ``domain`` is neither a list nor a Domain """ - if not isinstance(domain, (list, nonliterals.Domain)): + if not isinstance(domain, (list, nonliterals.Domain, nonliterals.CompoundDomain)): raise TypeError("Domain %r is not a list or a nonliteral Domain", domain)