[FIX] custom contexts and domains in search view fields

* Remove own_value hack, use compounds instead
* Correctly handle compounds evaluation contexts in test eval_domain_and_context js evaluator

bzr revid: xmo@openerp.com-20120404062727-r5eoaw73pjw1izk0
This commit is contained in:
Xavier Morel 2012-04-04 08:27:27 +02:00
parent 6f24433aef
commit d70220631a
3 changed files with 21 additions and 27 deletions

View File

@ -45,29 +45,20 @@ class NonLiteralEncoder(simplejson.encoder.JSONEncoder):
raise TypeError('Could not encode unknown non-literal %s' % object)
_ALLOWED_KEYS = frozenset(['__ref', "__id", '__domains', '__debug',
'__contexts', '__eval_context', 'own_values'])
'__contexts', '__eval_context'])
def non_literal_decoder(dct):
""" Decodes JSON dicts into :class:`Domain` and :class:`Context` based on
magic attribute tags.
Also handles private context section for the domain or section via the
``own_values`` dict key.
"""
if '__ref' in dct:
for x in dct:
if x not in _ALLOWED_KEYS:
raise ValueError("'%s' key not allowed in non literal domain/context" % x)
if dct['__ref'] == 'domain':
domain = Domain(None, key=dct['__id'])
if 'own_values' in dct:
domain.own = dct['own_values']
return domain
return Domain(None, key=dct['__id'])
elif dct['__ref'] == 'context':
context = Context(None, key=dct['__id'])
if 'own_values' in dct:
context.own = dct['own_values']
return context
return Context(None, key=dct['__id'])
elif dct["__ref"] == "compound_domain":
cdomain = CompoundDomain()
for el in dct["__domains"]:
@ -111,7 +102,6 @@ class Domain(BaseDomain):
"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])
@ -131,8 +121,7 @@ class Domain(BaseDomain):
evaluated result.
"""
ctx = self.session.evaluation_context(context)
if self.own:
ctx.update(self.own)
try:
return eval(self.get_domain_string(), SuperDict(ctx))
except NameError as e:
@ -159,7 +148,7 @@ class Context(BaseContext):
"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])
@ -179,8 +168,7 @@ class Context(BaseContext):
evaluated result.
"""
ctx = self.session.evaluation_context(context)
if self.own:
ctx.update(self.own)
try:
return eval(self.get_context_string(), SuperDict(ctx))
except NameError as e:

View File

@ -620,17 +620,21 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
}), true);
}
},
test_eval_contexts: function (contexts) {
test_eval_contexts: function (contexts, evaluation_context) {
evaluation_context = evaluation_context || {};
var result_context = _.extend({}, this.user_context),
self = this;
_(contexts).each(function (ctx) {
switch(ctx.__ref) {
case 'context':
_.extend(result_context, py.eval(ctx.__debug));
_.extend(result_context, py.eval(
ctx.__debug, evaluation_context));
break;
case 'compound_context':
_.extend(
result_context, self.test_eval_contexts(ctx.__contexts));
result_context, self.test_eval_contexts(
ctx.__contexts, _.extend(
{}, evaluation_context, ctx.__eval_context)));
break;
default:
_.extend(result_context, ctx);
@ -649,7 +653,8 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
case 'compound_domain':
result_domain.push.apply(
result_domain, self.test_eval_domains(
dom.__domains, eval_context));
dom.__domains, _.extend(
{}, eval_context, dom.__eval_context)));
break;
default:
result_domain.push.apply(
@ -667,7 +672,8 @@ openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.
group = py.eval(ctx.__debug).group_by;
break;
case 'compound_context':
group = self.test_eval_contexts(ctx.__contexts).group_by;
group = self.test_eval_contexts(
ctx.__contexts, ctx.__eval_context).group_by;
break;
default:
group = ctx.group_by

View File

@ -787,9 +787,8 @@ openerp.web.search.Field = openerp.web.search.Input.extend( /** @lends openerp.w
if (!(has_value && context)) {
return;
}
return _.extend(
{}, context,
{own_values: {self: val}});
return new openerp.web.CompoundContext(context)
.set_eval_context({self: val});
},
/**
* Function creating the returned domain for the field, override this
@ -818,7 +817,8 @@ openerp.web.search.Field = openerp.web.search.Input.extend( /** @lends openerp.w
this.attrs.operator || this.default_operator,
val);
}
return _.extend({}, domain, {own_values: {self: val}});
return new openerp.web.CompoundDomain(domain)
.set_eval_context({self: val});
}
});
/**