3
0
Fork 0

[MIG] web_domain_field: Migration to 13.0

16.0
laurent.corron 2019-11-11 20:24:52 +01:00 committed by arulbalamurugan
parent ae40850c55
commit d90dbe6d0a
4 changed files with 206 additions and 229 deletions

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017 ACSONE SA/NV # Copyright 2017 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
@ -6,11 +5,11 @@
"name": "Web Domain Field", "name": "Web Domain Field",
"summary": """ "summary": """
Use computed field as domain""", Use computed field as domain""",
"version": "10.0.1.0.0", "version": "13.0.1.0.0",
"license": "AGPL-3", "license": "AGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)", "author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://acsone.eu/", "website": "https://github.com/OCA/web",
"depends": ["web"], "depends": ["web"],
"data": ["views/web_domain_field.xml",], "data": ["views/web_domain_field.xml"],
"demo": [], "installable": True,
} }

View File

@ -4,9 +4,9 @@
# #
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Odoo Server 10.0\n" "Project-Id-Version: Odoo Server 13.0\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"Last-Translator: <>\n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
@ -15,8 +15,7 @@ msgstr ""
#. module: web_domain_field #. module: web_domain_field
#. openerp-web #. openerp-web
#: code:addons/web_domain_field/static/src/js/pyeval.js:160 #: code:addons/web_domain_field/static/src/js/pyeval.js:0
#, python-format #, python-format
msgid "Unknown nonliteral type " msgid "Unknown nonliteral type "
msgstr "" msgstr ""

View File

@ -1,20 +1,19 @@
odoo.define('web.domain_field', function (require) { odoo.define('web.domain_field', function (require) {
"use strict"; "use strict";
var pyeval = require('web.pyeval'); var py_utils = require('web.py_utils');
var session = require('web.session'); var session = require('web.session');
var original_pyeval = pyeval.eval; var original_pyeval = py_utils.eval;
var original_ensure_evaluated = pyeval.ensure_evaluated; var py = window.py;
var py = window.py;
/** copied from pyeval and not modified but required since not publicly /** Copied from py_utils and not modified but required since not publicly
exposed by web.pyeval**/ exposed by web.py_utils**/
// recursively wraps JS objects passed into the context to attributedicts // recursively wraps JS objects passed into the context to attributedicts
// which jsonify back to JS objects // which jsonify back to JS objects
function wrap(value) { function wrap(value) {
if (value === null) { return py.None; } if (value === null) { return py.None; }
switch (typeof value) { switch (typeof value) {
@ -30,9 +29,9 @@ function wrap(value) {
} }
throw new Error("ValueError: unable to wrap " + value); throw new Error("ValueError: unable to wrap " + value);
} }
var wrapping_dict = py.type('wrapping_dict', null, { var wrapping_dict = py.type('wrapping_dict', null, {
__init__: function () { __init__: function () {
this._store = {}; this._store = {};
}, },
@ -47,7 +46,7 @@ var wrapping_dict = py.type('wrapping_dict', null, {
return this.__getitem__(py.str.fromJSON(key)); return this.__getitem__(py.str.fromJSON(key));
}, },
__len__: function () { __len__: function () {
return Object.keys(this._store).length return Object.keys(this._store).length;
}, },
__nonzero__: function () { __nonzero__: function () {
return py.PY_size(this) > 0 ? py.True : py.False; return py.PY_size(this) > 0 ? py.True : py.False;
@ -66,9 +65,9 @@ var wrapping_dict = py.type('wrapping_dict', null, {
toJSON: function () { toJSON: function () {
return this._store; return this._store;
}, },
}); });
var wrapping_list = py.type('wrapping_list', null, { var wrapping_list = py.type('wrapping_list', null, {
__init__: function () { __init__: function () {
this._store = []; this._store = [];
}, },
@ -89,13 +88,17 @@ var wrapping_list = py.type('wrapping_list', null, {
toJSON: function () { toJSON: function () {
return this._store; return this._store;
}, },
}); });
function wrap_context (context) { function wrap_context(context) {
for (var k in context) { for (var k in context) {
if (!context.hasOwnProperty(k)) { continue; } if (!context.hasOwnProperty(k)) { continue; }
var val = context[k]; 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 === null) { continue; }
if (val.constructor === Array) { if (val.constructor === Array) {
context[k] = wrapping_list.fromJSON(val); context[k] = wrapping_list.fromJSON(val);
@ -105,9 +108,9 @@ function wrap_context (context) {
} }
} }
return context; return context;
} }
function ensure_evaluated (args, kwargs) { function ensure_evaluated(args, kwargs) {
for (var i=0; i<args.length; ++i) { for (var i=0; i<args.length; ++i) {
args[i] = eval_arg(args[i]); args[i] = eval_arg(args[i]);
} }
@ -115,43 +118,16 @@ function ensure_evaluated (args, kwargs) {
if (!kwargs.hasOwnProperty(k)) { continue; } if (!kwargs.hasOwnProperty(k)) { continue; }
kwargs[k] = eval_arg(kwargs[k]); 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; /** End of unmodified methods copied from pyeval **/
switch(ctx.__ref) {
case 'context': // We need to override the original method to be able to call our
evaluation_context.context = evaluation_context; // Specialized version of pyeval for domain fields
evaluated = py.eval(ctx.__debug, wrap_context(evaluation_context)); function eval_arg (arg) {
break; if (typeof arg !== 'object' || !arg.__ref) {
case 'compound_context': return arg;
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 switch (arg.__ref) {
// siblings
_.extend(evaluation_context, evaluated);
return _.extend(result_context, evaluated);
}, {});
}
/** 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
function eval_arg (arg) {
if (typeof arg !== 'object' || !arg.__ref) { return arg; }
switch(arg.__ref) {
case 'domain': case 'compound_domain': case 'domain': case 'compound_domain':
return domain_field_pyeval('domains', [arg]); return domain_field_pyeval('domains', [arg]);
case 'context': case 'compound_context': case 'context': case 'compound_context':
@ -159,75 +135,78 @@ function eval_arg (arg) {
default: default:
throw new Error(_t("Unknown nonliteral type ") + ' ' + arg.__ref); throw new Error(_t("Unknown nonliteral type ") + ' ' + arg.__ref);
} }
} }
// 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 // value as domain
function eval_domains (domains, evaluation_context) { function eval_domains(domains, evaluation_context) {
evaluation_context = _.extend(pyeval.context(), evaluation_context || evaluation_context = _.extend(py_utils.context(), evaluation_context || {});
{});
var result_domain = []; 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) { _(domains).each(function (domain) {
if (_.isString(domain)) { if (_.isString(domain)) {
// Modified part or the original method // Modified part or the original method
if(domain in evaluation_context) { if (domain in evaluation_context) {
result_domain.push.apply( result_domain.push.apply(
result_domain, $.parseJSON(evaluation_context[domain])); result_domain, $.parseJSON(evaluation_context[domain]));
return; return;
} }
// end of modifications // End of modifications
// wrap raw strings in domain // wrap raw strings in domain
domain = { __ref: 'domain', __debug: domain }; domain = { __ref: 'domain', __debug: domain };
} }
var domain_array_to_combine;
switch(domain.__ref) { switch(domain.__ref) {
case 'domain': case 'domain':
evaluation_context.context = evaluation_context; evaluation_context.context = evaluation_context;
result_domain.push.apply( domain_array_to_combine = py.eval(domain.__debug, wrap_context(evaluation_context));
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)));
break; break;
default: 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; return result_domain;
} }
// override pyeval in order to call our specialized implementation of
// eval_domains // Override pyeval in order to call our specialized implementation of
function domain_field_pyeval (type, object, context, options) { // eval_domains
switch(type) { function domain_field_pyeval (type, object, context, options) {
switch (type) {
case 'domain': case 'domain':
case 'domains': case 'domains':
if (type === 'domain') if (type === 'domain') {
object = [object]; object = [object];
}
return eval_domains(object, context); return eval_domains(object, context);
default: default:
return original_pyeval(type, object, context, options); return original_pyeval(type, object, context, options);
} }
} }
// override sync_eval in order to call our specialized implementation of function eval_domains_and_contexts(source) {
// eval_domains
function sync_eval_domains_and_contexts (source) {
var contexts = ([session.user_context] || []).concat(source.contexts);
// see Session.eval_context in Python // see Session.eval_context in Python
return { return {
context: domain_field_pyeval('contexts', contexts), context: domain_field_pyeval('contexts', source.contexts || [], source.eval_context),
domain: domain_field_pyeval('domains', source.domains), domain: domain_field_pyeval('domains', source.domains, source.eval_context),
group_by: domain_field_pyeval('groupbys', source.group_by_seq || []) group_by: domain_field_pyeval('groupbys', source.group_by_seq || [], source.eval_context),
}; };
} }
pyeval.eval = domain_field_pyeval;
pyeval.ensure_evaluated = ensure_evaluated; py_utils.eval = domain_field_pyeval;
pyeval.sync_eval_domains_and_contexts = sync_eval_domains_and_contexts; py_utils.ensure_evaluated = ensure_evaluated;
py_utils.eval_domains_and_contexts = eval_domains_and_contexts;
}); });