[imp] added compound domains and contexts

bzr revid: nicolas.vanhoren@openerp.com-20110617095835-oymrh9u459hhwqa1
This commit is contained in:
niv-openerp 2011-06-17 11:58:35 +02:00
parent 4205717d3a
commit 858f61c6be
3 changed files with 103 additions and 2 deletions

View File

@ -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:

View File

@ -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

View File

@ -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)