3
0
Fork 0

[MIG] web_domain_field. Adapt js code to 12.0

12.0
Ronald Portier 2020-11-18 16:05:12 +01:00
parent f273ac7459
commit feb24f41cb
1 changed files with 196 additions and 216 deletions

View File

@ -1,16 +1,15 @@
odoo.define('web.domain_field', function (require) {
"use strict";
var pyeval = require('web.pyeval');
var py_utils = require('web.py_utils');
var session = require('web.session');
var original_pyeval = pyeval.eval;
var original_ensure_evaluated = pyeval.ensure_evaluated;
var original_pyeval = py_utils.eval;
var py = window.py;
/** copied from pyeval and not modified but required since not publicly
exposed by web.pyeval**/
/** Copied from py_utils and not modified but required since not publicly
exposed by web.py_utils**/
// recursively wraps JS objects passed into the context to attributedicts
// which jsonify back to JS objects
@ -95,11 +94,16 @@ function wrap_context (context) {
for (var k in context) {
if (!context.hasOwnProperty(k)) { continue; }
var val = context[k];
// Don't add a test case like ``val === undefined``
// this is intended to prevent letting crap pass
// on the context without even knowing it.
// If you face an issue from here, try to sanitize
// the context upstream instead
if (val === null) { continue; }
if (val.constructor === Array) {
context[k] = wrapping_list.fromJSON(val);
} else if (val.constructor === Object && !py.PY_isInstance(val, py.object)) {
} else if (val.constructor === Object
&& !py.PY_isInstance(val, py.object)) {
context[k] = wrapping_dict.fromJSON(val);
}
}
@ -115,41 +119,14 @@ function ensure_evaluated (args, kwargs) {
kwargs[k] = eval_arg(kwargs[k]);
}
}
function eval_contexts (contexts, evaluation_context) {
evaluation_context = _.extend(pyeval.context(), evaluation_context || {});
return _(contexts).reduce(function (result_context, ctx) {
// __eval_context evaluations can lead to some of `contexts`'s
// values being null, skip them as well as empty contexts
if (_.isEmpty(ctx)) { return result_context; }
if (_.isString(ctx)) {
// wrap raw strings in context
ctx = { __ref: 'context', __debug: ctx };
}
var evaluated = ctx;
switch(ctx.__ref) {
case 'context':
evaluation_context.context = evaluation_context;
evaluated = py.eval(ctx.__debug, wrap_context(evaluation_context));
break;
case 'compound_context':
var eval_context = eval_contexts([ctx.__eval_context]);
evaluated = eval_contexts(
ctx.__contexts, _.extend({}, evaluation_context, eval_context));
break;
}
// add newly evaluated context to evaluation context for following
// siblings
_.extend(evaluation_context, evaluated);
return _.extend(result_context, evaluated);
}, {});
}
/** end of unmodified methods copied from pyeval **/
/** End of unmodified methods copied from pyeval **/
// We need to override the original method to be able to call our
//specialized version of pyeval for domain fields
// Specialized version of pyeval for domain fields
function eval_arg (arg) {
if (typeof arg !== 'object' || !arg.__ref) { return arg; }
if (typeof arg !== 'object' || !arg.__ref) {
return arg;
}
switch (arg.__ref) {
case 'domain': case 'compound_domain':
return domain_field_pyeval('domains', [arg]);
@ -160,12 +137,18 @@ function eval_arg (arg) {
}
}
// override eval_domains to add 3 lines in order to be able to use a field
// Override eval_domains to add 3 lines in order to be able to use a field
// value as domain
function eval_domains(domains, evaluation_context) {
evaluation_context = _.extend(pyeval.context(), evaluation_context ||
{});
evaluation_context = _.extend(py_utils.context(), evaluation_context || {});
var result_domain = [];
// Normalize only if the first domain is the array ["|"] or ["!"]
var need_normalization = (
domains &&
domains.length > 0 &&
domains[0].length === 1 &&
(domains[0][0] === "|" || domains[0][0] === "!")
);
_(domains).each(function (domain) {
if (_.isString(domain)) {
// Modified part or the original method
@ -174,59 +157,56 @@ function eval_domains (domains, evaluation_context) {
result_domain, $.parseJSON(evaluation_context[domain]));
return;
}
// end of modifications
// End of modifications
// wrap raw strings in domain
domain = { __ref: 'domain', __debug: domain };
}
var domain_array_to_combine;
switch(domain.__ref) {
case 'domain':
evaluation_context.context = evaluation_context;
result_domain.push.apply(
result_domain, py.eval(domain.__debug, wrap_context(evaluation_context)));
break;
case 'compound_domain':
var eval_context = eval_contexts([domain.__eval_context]);
result_domain.push.apply(
result_domain, eval_domains(
domain.__domains, _.extend(
{}, evaluation_context, eval_context)));
domain_array_to_combine = py.eval(domain.__debug, wrap_context(evaluation_context));
break;
default:
result_domain.push.apply(result_domain, domain);
domain_array_to_combine = domain;
}
if (need_normalization) {
domain_array_to_combine = get_normalized_domain(domain_array_to_combine);
}
result_domain.push.apply(result_domain, domain_array_to_combine);
});
return result_domain;
}
// override pyeval in order to call our specialized implementation of
// Override pyeval in order to call our specialized implementation of
// eval_domains
function domain_field_pyeval (type, object, context, options) {
switch (type) {
case 'domain':
case 'domains':
if (type === 'domain')
if (type === 'domain') {
object = [object];
}
return eval_domains(object, context);
default:
return original_pyeval(type, object, context, options);
}
}
// override sync_eval in order to call our specialized implementation of
// eval_domains
function sync_eval_domains_and_contexts (source) {
var contexts = ([session.user_context] || []).concat(source.contexts);
function eval_domains_and_contexts(source) {
// see Session.eval_context in Python
return {
context: domain_field_pyeval('contexts', contexts),
domain: domain_field_pyeval('domains', source.domains),
group_by: domain_field_pyeval('groupbys', source.group_by_seq || [])
context: domain_field_pyeval('contexts', source.contexts || [], source.eval_context),
domain: domain_field_pyeval('domains', source.domains, source.eval_context),
group_by: domain_field_pyeval('groupbys', source.group_by_seq || [], source.eval_context),
};
}
pyeval.eval = domain_field_pyeval;
pyeval.ensure_evaluated = ensure_evaluated;
pyeval.sync_eval_domains_and_contexts = sync_eval_domains_and_contexts;
py_utils.eval = domain_field_pyeval;
py_utils.ensure_evaluated = ensure_evaluated;
py_utils.eval_domains_and_contexts = eval_domains_and_contexts;
});