From 6f4abdfbd519e8f8a416e24fdb541a88ef3104c6 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 6 Aug 2012 11:46:27 +0200 Subject: [IMP] move eval functions for domains, groupbys and contexts outside of corelib, as well as the standard evaluation context building (and related objects) bzr revid: xmo@openerp.com-20120806094627-8c9lwtfkbfws2bmj --- addons/web/static/src/js/pyeval.js | 244 +++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 addons/web/static/src/js/pyeval.js (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js new file mode 100644 index 00000000000..8c498c3f885 --- /dev/null +++ b/addons/web/static/src/js/pyeval.js @@ -0,0 +1,244 @@ +/* + * py.js helpers and setup + */ +openerp.web.pyeval = function (instance) { + instance.web.pyeval = {}; + + var asJS = function (arg) { + if (arg instanceof py.object) { + return arg.toJSON(); + } + return arg; + }; + + var datetime = new py.object(); + datetime.datetime = new py.type(function datetime() { + throw new Error('datetime.datetime not implemented'); + }); + var date = datetime.date = new py.type(function date(y, m, d) { + if (y instanceof Array) { + d = y[2]; + m = y[1]; + y = y[0]; + } + this.year = asJS(y); + this.month = asJS(m); + this.day = asJS(d); + }, py.object, { + strftime: function (args) { + var f = asJS(args[0]), self = this; + return new py.str(f.replace(/%([A-Za-z])/g, function (m, c) { + switch (c) { + case 'Y': return self.year; + case 'm': return _.str.sprintf('%02d', self.month); + case 'd': return _.str.sprintf('%02d', self.day); + } + throw new Error('ValueError: No known conversion for ' + m); + })); + } + }); + date.__getattribute__ = function (name) { + if (name === 'today') { + return date.today; + } + throw new Error("AttributeError: object 'date' has no attribute '" + name +"'"); + }; + date.today = new py.def(function () { + var d = new Date(); + return new date(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()); + }); + datetime.time = new py.type(function time() { + throw new Error('datetime.time not implemented'); + }); + + var time = new py.object(); + time.strftime = new py.def(function (args) { + return date.today.__call__().strftime(args); + }); + + var relativedelta = new py.type(function relativedelta(args, kwargs) { + if (!_.isEmpty(args)) { + throw new Error('Extraction of relative deltas from existing datetimes not supported'); + } + this.ops = kwargs; + }, py.object, { + __add__: function (other) { + if (!(other instanceof datetime.date)) { + return py.NotImplemented; + } + // TODO: test this whole mess + var year = asJS(this.ops.year) || asJS(other.year); + if (asJS(this.ops.years)) { + year += asJS(this.ops.years); + } + + var month = asJS(this.ops.month) || asJS(other.month); + if (asJS(this.ops.months)) { + month += asJS(this.ops.months); + // FIXME: no divmod in JS? + while (month < 1) { + year -= 1; + month += 12; + } + while (month > 12) { + year += 1; + month -= 12; + } + } + + var lastMonthDay = new Date(year, month, 0).getDate(); + var day = asJS(this.ops.day) || asJS(other.day); + if (day > lastMonthDay) { day = lastMonthDay; } + var days_offset = ((asJS(this.ops.weeks) || 0) * 7) + (asJS(this.ops.days) || 0); + if (days_offset) { + day = new Date(year, month-1, day + days_offset).getDate(); + } + // TODO: leapdays? + // TODO: hours, minutes, seconds? Not used in XML domains + // TODO: weekday? + return new datetime.date(year, month, day); + }, + __radd__: function (other) { + return this.__add__(other); + }, + + __sub__: function (other) { + if (!(other instanceof datetime.date)) { + return py.NotImplemented; + } + // TODO: test this whole mess + var year = asJS(this.ops.year) || asJS(other.year); + if (asJS(this.ops.years)) { + year -= asJS(this.ops.years); + } + + var month = asJS(this.ops.month) || asJS(other.month); + if (asJS(this.ops.months)) { + month -= asJS(this.ops.months); + // FIXME: no divmod in JS? + while (month < 1) { + year -= 1; + month += 12; + } + while (month > 12) { + year += 1; + month -= 12; + } + } + + var lastMonthDay = new Date(year, month, 0).getDate(); + var day = asJS(this.ops.day) || asJS(other.day); + if (day > lastMonthDay) { day = lastMonthDay; } + var days_offset = ((asJS(this.ops.weeks) || 0) * 7) + (asJS(this.ops.days) || 0); + if (days_offset) { + day = new Date(year, month-1, day - days_offset).getDate(); + } + // TODO: leapdays? + // TODO: hours, minutes, seconds? Not used in XML domains + // TODO: weekday? + return new datetime.date(year, month, day); + }, + __rsub__: function (other) { + return this.__sub__(other); + } + }); + + var eval_contexts = function (contexts, evaluation_context) { + evaluation_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; } + var evaluated = ctx; + switch(ctx.__ref) { + case 'context': + evaluated = py.eval(ctx.__debug, 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); + }, _.extend({}, instance.connection.user_context)); + }; + var eval_domains = function (domains, evaluation_context) { + var result_domain = []; + _(domains).each(function (domain) { + switch(domain.__ref) { + case 'domain': + result_domain.push.apply( + result_domain, py.eval(domain.__debug, 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; + default: + result_domain.push.apply(result_domain, domain); + } + }); + return result_domain; + }; + var eval_groupbys = function (contexts, evaluation_context) { + var result_group = []; + _(contexts).each(function (ctx) { + var group; + var evaluated = ctx; + switch(ctx.__ref) { + case 'context': + evaluated = py.eval(ctx.__debug), 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; + } + group = evaluated.group_by; + if (!group) { return; } + if (typeof group === 'string') { + result_group.push(group); + } else if (group instanceof Array) { + result_group.push.apply(result_group, group); + } else { + throw new Error('Got invalid groupby {{' + + JSON.stringify(group) + '}}'); + } + _.extend(evaluation_context, evaluated); + }); + return result_group; + }; + + instance.web.pyeval.context = function () { + return { + uid: new py.float(instance.connection.uid), + datetime: datetime, + time: time, + relativedelta: relativedelta, + current_date: date.today.__call__().strftime(['%Y-%m-%d']), + }; + }; + + /** + * @param {String} type "domains", "contexts" or "groupbys" + * @param {Array} object domains or contexts to evaluate + * @param {Object} [context] evaluation context + */ + instance.web.pyeval.eval = function (type, object, context) { + if (!context) { context = instance.web.pyeval.context()} + switch(type) { + case 'contexts': return eval_contexts(object, context); + case 'domains': return eval_domains(object, context); + case 'groupbys': return eval_groupbys(object, context); + } + throw new Error("Unknow evaluation type " + type) + }; +}; -- cgit v1.2.3 From 0e101c63a3355e2aebce2d38f985e9ac6dc96376 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 5 Oct 2012 15:12:05 +0200 Subject: [FIX] evaluator: connection -> session bzr revid: xmo@openerp.com-20121005131205-ffk746ac5xrua62j --- addons/web/static/src/js/pyeval.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 8c498c3f885..007d73081ce 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -164,7 +164,7 @@ openerp.web.pyeval = function (instance) { // siblings _.extend(evaluation_context, evaluated); return _.extend(result_context, evaluated); - }, _.extend({}, instance.connection.user_context)); + }, _.extend({}, instance.session.user_context)); }; var eval_domains = function (domains, evaluation_context) { var result_domain = []; @@ -219,7 +219,7 @@ openerp.web.pyeval = function (instance) { instance.web.pyeval.context = function () { return { - uid: new py.float(instance.connection.uid), + uid: new py.float(instance.session.uid), datetime: datetime, time: time, relativedelta: relativedelta, -- cgit v1.2.3 From 10bc6ddfe4e12e792d856369b482ae74f73197d0 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 5 Oct 2012 16:28:08 +0200 Subject: [ADD] groupby eval tests bzr revid: xmo@openerp.com-20121005142808-yx6o8nfnn0o4ms56 --- addons/web/static/src/js/pyeval.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 007d73081ce..34ddf0e119b 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -194,7 +194,7 @@ openerp.web.pyeval = function (instance) { var evaluated = ctx; switch(ctx.__ref) { case 'context': - evaluated = py.eval(ctx.__debug), evaluation_context; + evaluated = py.eval(ctx.__debug, evaluation_context); break; case 'compound_context': var eval_context = eval_contexts([ctx.__eval_context]); -- cgit v1.2.3 From 7bcbadb099a71b731812ace299b98317edc49ae4 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 8 Oct 2012 14:06:19 +0200 Subject: [FIX] update py.js to 0.6, port existin classes to it, attempt to implement timedelta bzr revid: xmo@openerp.com-20121008120619-lhmexjnujjrigjn9 --- addons/web/static/src/js/pyeval.js | 195 +++++++++++++++++++++++++++---------- 1 file changed, 145 insertions(+), 50 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 34ddf0e119b..80aac87ecbe 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -4,66 +4,151 @@ openerp.web.pyeval = function (instance) { instance.web.pyeval = {}; + var obj = function () {}; + obj.prototype = py.object; var asJS = function (arg) { - if (arg instanceof py.object) { + if (arg instanceof obj) { return arg.toJSON(); } return arg; }; - var datetime = new py.object(); - datetime.datetime = new py.type(function datetime() { - throw new Error('datetime.datetime not implemented'); + var datetime = py.PY_call(py.object); + datetime.datetime = py.type('datetime', null, { + __init__: function () { + var zero = py.float.fromJSON(0); + var args = py.PY_parseArgs(arguments, [ + 'year', 'month', 'day', + ['hour', zero], ['minute', zero], ['second', zero], + ['microsecond', zero], ['tzinfo', py.None] + ]); + for(var key in args) { + if (!args.hasOwnProperty(key)) { continue; } + this[key] = asJS(args[key]); + } + }, + strftime: function () { + var self = this; + var args = py.PY_parseArgs(arguments, 'format'); + return py.str.fromJSON(args.format.toJSON() + .replace(/%([A-Za-z])/g, function (m, c) { + switch (c) { + case 'Y': return self.year; + case 'm': return _.str.sprintf('%02d', self.month); + case 'd': return _.str.sprintf('%02d', self.day); + case 'H': return _.str.sprintf('%02d', self.hour); + case 'M': return _.str.sprintf('%02d', self.minute); + case 'S': return _.str.sprintf('%02d', self.second); + } + throw new Error('ValueError: No known conversion for ' + m); + })); + }, + now: py.classmethod.fromJSON(function () { + var d = new Date(); + return py.PY_call(datetime.datetime, + [d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), + d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), + d.getUTCMilliseconds() * 1000]); + }), + today: py.classmethod.fromJSON(function () { + var d = new Date(); + return py.PY_call(datetime.datetime, + [d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()]); + }), + combine: py.classmethod.fromJSON(function () { + var args = py.PY_parseArgs(arguments, 'date time'); + return py.PY_call(datetime.datetime, [ + // FIXME: should use getattr + args.date.year, + args.date.month, + args.date.day, + args.time.hour, + args.time.minute, + args.time.second + ]); + }) }); - var date = datetime.date = new py.type(function date(y, m, d) { - if (y instanceof Array) { - d = y[2]; - m = y[1]; - y = y[0]; - } - this.year = asJS(y); - this.month = asJS(m); - this.day = asJS(d); - }, py.object, { - strftime: function (args) { - var f = asJS(args[0]), self = this; - return new py.str(f.replace(/%([A-Za-z])/g, function (m, c) { - switch (c) { - case 'Y': return self.year; - case 'm': return _.str.sprintf('%02d', self.month); - case 'd': return _.str.sprintf('%02d', self.day); - } - throw new Error('ValueError: No known conversion for ' + m); - })); - } + datetime.date = py.type('date', null, { + __init__: function () { + var args = py.PY_parseArgs(arguments, 'year month day'); + this.year = asJS(args.year); + this.month = asJS(args.month); + this.day = asJS(args.day); + }, + strftime: function () { + var self = this; + var args = py.PY_parseArgs(arguments, 'format'); + return py.str.fromJSON(args.format.toJSON() + .replace(/%([A-Za-z])/g, function (m, c) { + switch (c) { + case 'Y': return self.year; + case 'm': return _.str.sprintf('%02d', self.month); + case 'd': return _.str.sprintf('%02d', self.day); + } + throw new Error('ValueError: No known conversion for ' + m); + })); + }, + today: py.classmethod.fromJSON(function () { + var d = new Date(); + return py.PY_call( + datetime.date, [d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()]); + }) }); - date.__getattribute__ = function (name) { - if (name === 'today') { - return date.today; + datetime.time = py.type('time', null, { + __init__: function () { + var zero = py.float.fromJSON(0); + var args = py.PY_parseArgs(arguments, [ + ['hour', zero], ['minute', zero], ['second', zero], ['microsecond', zero], + ['tzinfo', py.None] + ]); + + for(var k in args) { + if (!args.hasOwnProperty(k)) { continue; } + this[k] = asJS(args[k]); + } } - throw new Error("AttributeError: object 'date' has no attribute '" + name +"'"); - }; - date.today = new py.def(function () { - var d = new Date(); - return new date(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()); }); - datetime.time = new py.type(function time() { - throw new Error('datetime.time not implemented'); + + datetime.timedelta = py.type('timedelta', null, { + __init__: function () { + var zero = py.float.fromJSON(0); + var args = py.PY_parseArgs(arguments, + [['days', zero], ['seconds', zero], ['microseconds', zero], + ['milliseconds', zero], ['minutes', zero], ['hours', zero], + ['weeks', zero]]); + var ms = args['microseconds'].toJSON() + + 1000 * args['milliseconds'].toJSON(); + this.milliseconds = ms % 1000000; + var sec = Math.floor(ms / 1000000) + + args['seconds'].toJSON() + + 60 * args['minutes'].toJSON() + + 3600 * args['hours'].toJSON(); + this.seconds = sec % 86400; + this.days = Math.floor(sec / 86400) + + args['days'].toJSON() + + 7 * args['weeks'].toJSON(); + } }); - var time = new py.object(); - time.strftime = new py.def(function (args) { - return date.today.__call__().strftime(args); + var time = py.PY_call(py.object); + time.strftime = py.PY_def.fromJSON(function () { + // FIXME: needs PY_getattr + var d = py.PY_call(datetime.__getattribute__('datetime') + .__getattribute__('now')); + var args = [].slice.call(arguments); + return py.PY_call.apply( + null, [d.__getattribute__('strftime')].concat(args)); }); - var relativedelta = new py.type(function relativedelta(args, kwargs) { - if (!_.isEmpty(args)) { - throw new Error('Extraction of relative deltas from existing datetimes not supported'); - } - this.ops = kwargs; - }, py.object, { + var relativedelta = py.type('relativedelta', null, { + __init__: function () { + this.ops = py.PY_parseArgs(arguments, + '* year month day hour minute second microsecond ' + + 'years months weeks days hours minutes secondes microseconds ' + + 'weekday leakdays yearday nlyearday'); + }, __add__: function (other) { - if (!(other instanceof datetime.date)) { + if (py.PY_call(py.isinstance, [datetime.date]) !== py.True) { return py.NotImplemented; } // TODO: test this whole mess @@ -96,14 +181,19 @@ openerp.web.pyeval = function (instance) { // TODO: leapdays? // TODO: hours, minutes, seconds? Not used in XML domains // TODO: weekday? - return new datetime.date(year, month, day); + // FIXME: use date.replace + return py.PY_call(datetime.date, [ + py.float.fromJSON(year), + py.float.fromJSON(month), + py.float.fromJSON(day) + ]); }, __radd__: function (other) { return this.__add__(other); }, __sub__: function (other) { - if (!(other instanceof datetime.date)) { + if (py.PY_call(py.isinstance, [datetime.date]) !== py.True) { return py.NotImplemented; } // TODO: test this whole mess @@ -136,7 +226,11 @@ openerp.web.pyeval = function (instance) { // TODO: leapdays? // TODO: hours, minutes, seconds? Not used in XML domains // TODO: weekday? - return new datetime.date(year, month, day); + return py.PY_call(datetime.date, [ + py.float.fromJSON(year), + py.float.fromJSON(month), + py.float.fromJSON(day) + ]); }, __rsub__: function (other) { return this.__sub__(other); @@ -219,11 +313,12 @@ openerp.web.pyeval = function (instance) { instance.web.pyeval.context = function () { return { - uid: new py.float(instance.session.uid), + uid: py.float.fromJSON(instance.session.uid), datetime: datetime, time: time, relativedelta: relativedelta, - current_date: date.today.__call__().strftime(['%Y-%m-%d']), + current_date: py.PY_call( + time.strftime, [py.str.fromJSON('%Y-%m-%d')]), }; }; -- cgit v1.2.3 From 894bd34e6275fe838bb0de30e25de4b0e09d4636 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 8 Oct 2012 14:55:23 +0200 Subject: [REM] timedelta, because pain bzr revid: xmo@openerp.com-20121008125523-ed1g42vjxbcfo62r --- addons/web/static/src/js/pyeval.js | 21 --------------------- 1 file changed, 21 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 80aac87ecbe..8b56d17c14e 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -109,27 +109,6 @@ openerp.web.pyeval = function (instance) { } }); - datetime.timedelta = py.type('timedelta', null, { - __init__: function () { - var zero = py.float.fromJSON(0); - var args = py.PY_parseArgs(arguments, - [['days', zero], ['seconds', zero], ['microseconds', zero], - ['milliseconds', zero], ['minutes', zero], ['hours', zero], - ['weeks', zero]]); - var ms = args['microseconds'].toJSON() - + 1000 * args['milliseconds'].toJSON(); - this.milliseconds = ms % 1000000; - var sec = Math.floor(ms / 1000000) - + args['seconds'].toJSON() - + 60 * args['minutes'].toJSON() - + 3600 * args['hours'].toJSON(); - this.seconds = sec % 86400; - this.days = Math.floor(sec / 86400) - + args['days'].toJSON() - + 7 * args['weeks'].toJSON(); - } - }); - var time = py.PY_call(py.object); time.strftime = py.PY_def.fromJSON(function () { // FIXME: needs PY_getattr -- cgit v1.2.3 From 1fa00288b269e68a47849eaa3fefc090759a357d Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 10 Oct 2012 16:59:45 +0200 Subject: [FIX] add 'context' free variable in evaluation contexts for domains and contexts bzr revid: xmo@openerp.com-20121010145945-16shdry3udum0mn1 --- addons/web/static/src/js/pyeval.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 8b56d17c14e..a00fd186d58 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -307,7 +307,9 @@ openerp.web.pyeval = function (instance) { * @param {Object} [context] evaluation context */ instance.web.pyeval.eval = function (type, object, context) { - if (!context) { context = instance.web.pyeval.context()} + context = _.extend(instance.web.pyeval.context(), context || {}); + context['context'] = py.dict.fromJSON(context); + switch(type) { case 'contexts': return eval_contexts(object, context); case 'domains': return eval_domains(object, context); -- cgit v1.2.3 From b94e9c30a78e0047457977bf8ff5bd6bdda81372 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 11 Oct 2012 09:57:59 +0200 Subject: [FIX] after discussions with odo, date/datetime/time for locally-evaluated contexts and domains should be local, not UTC bzr revid: xmo@openerp.com-20121011075759-qfwkfws5tu9yidab --- addons/web/static/src/js/pyeval.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index a00fd186d58..51fdc63e84c 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -46,14 +46,14 @@ openerp.web.pyeval = function (instance) { now: py.classmethod.fromJSON(function () { var d = new Date(); return py.PY_call(datetime.datetime, - [d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), - d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), - d.getUTCMilliseconds() * 1000]); + [d.getFullYear(), d.getMonth() + 1, d.getDate(), + d.getHours(), d.getMinutes(), d.getSeconds(), + d.getMilliseconds() * 1000]); }), today: py.classmethod.fromJSON(function () { var d = new Date(); return py.PY_call(datetime.datetime, - [d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()]); + [d.getFullYear(), d.getMonth() + 1, d.getDate()]); }), combine: py.classmethod.fromJSON(function () { var args = py.PY_parseArgs(arguments, 'date time'); @@ -91,7 +91,7 @@ openerp.web.pyeval = function (instance) { today: py.classmethod.fromJSON(function () { var d = new Date(); return py.PY_call( - datetime.date, [d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate()]); + datetime.date, [d.getFullYear(), d.getMonth() + 1, d.getDate()]); }) }); datetime.time = py.type('time', null, { -- cgit v1.2.3 From e6b77eb820a7670b416319f314808e4b90931a01 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 11 Oct 2012 12:05:32 +0200 Subject: [IMP] shortcut call to eval_domain_and_context to be evaluated on the JS side, also add some offline-evaluation of contexts and domains in rpc call methods bzr revid: xmo@openerp.com-20121011100532-5ihje0maslp37zpf --- addons/web/static/src/js/pyeval.js | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 51fdc63e84c..ca4d5a0a1e7 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -2,6 +2,7 @@ * py.js helpers and setup */ openerp.web.pyeval = function (instance) { + var _t = instance.web._t; instance.web.pyeval = {}; var obj = function () {}; @@ -317,4 +318,60 @@ openerp.web.pyeval = function (instance) { } throw new Error("Unknow evaluation type " + type) }; + + var eval_arg = function (arg) { + if (typeof arg !== 'object' || !arg.__ref) { return arg; } + switch(arg.__ref) { + case 'domain': case 'compound_domain': + return instance.web.pyeval.eval('domains', [arg]); + case 'context': case 'compound_context': + return instance.web.pyeval.eval('contexts', [arg]); + default: + throw new Error(_t("Unknown nonliteral type " + arg.__ref)); + } + }; + /** + * If args or kwargs are unevaluated contexts or domains (compound or not), + * evaluated them in-place. + * + * Potentially mutates both parameters. + * + * @param args + * @param kwargs + */ + instance.web.pyeval.ensure_evaluated = function (args, kwargs) { + for (var i=0; i Date: Thu, 22 Nov 2012 13:41:30 +0100 Subject: [FIX] put evaluation context in evaluation context so you can access the context while you're accessing the context bzr revid: xmo@openerp.com-20121122124130-t8j6sggtnl4f0b8r --- addons/web/static/src/js/pyeval.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index ca4d5a0a1e7..fe68d5652a5 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -2,7 +2,6 @@ * py.js helpers and setup */ openerp.web.pyeval = function (instance) { - var _t = instance.web._t; instance.web.pyeval = {}; var obj = function () {}; @@ -226,6 +225,7 @@ openerp.web.pyeval = function (instance) { var evaluated = ctx; switch(ctx.__ref) { case 'context': + evaluation_context.context = py.dict.fromJSON(evaluation_context); evaluated = py.eval(ctx.__debug, evaluation_context); break; case 'compound_context': @@ -245,6 +245,7 @@ openerp.web.pyeval = function (instance) { _(domains).each(function (domain) { switch(domain.__ref) { case 'domain': + evaluation_context.context = py.dict.fromJSON(evaluation_context); result_domain.push.apply( result_domain, py.eval(domain.__debug, evaluation_context)); break; @@ -268,6 +269,7 @@ openerp.web.pyeval = function (instance) { var evaluated = ctx; switch(ctx.__ref) { case 'context': + evaluation_context.context = py.dict.fromJSON(evaluation_context); evaluated = py.eval(ctx.__debug, evaluation_context); break; case 'compound_context': @@ -327,7 +329,7 @@ openerp.web.pyeval = function (instance) { case 'context': case 'compound_context': return instance.web.pyeval.eval('contexts', [arg]); default: - throw new Error(_t("Unknown nonliteral type " + arg.__ref)); + throw new Error(instance.web._t("Unknown nonliteral type " + arg.__ref)); } }; /** @@ -362,11 +364,11 @@ openerp.web.pyeval = function (instance) { } catch (e) { d.resolve({ error: { code: 400, - message: _t("Evaluation Error"), + message: instance.web._t("Evaluation Error"), data: { type: 'local_exception', debug: _.str.sprintf( - _t("Local evaluation failure\n%s\n\n%s"), + instance.web._t("Local evaluation failure\n%s\n\n%s"), e.message, JSON.stringify(source)) } }}); -- cgit v1.2.3 From 815b908759451efd2d74cdb8d16a46bb83fe25c0 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 08:59:38 +0100 Subject: [ADD] partial but sufficient (I think) implementation of datetime.timedelta bzr revid: xmo@openerp.com-20121123075938-oazwt4bn2l639r3a --- addons/web/static/src/js/pyeval.js | 187 ++++++++++++++++++++++++++++++++++++- 1 file changed, 184 insertions(+), 3 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index fe68d5652a5..e0f465279ef 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -14,6 +14,188 @@ openerp.web.pyeval = function (instance) { }; var datetime = py.PY_call(py.object); + + /** + * computes (Math.floor(a/b), a%b and passes that to the callback. + * + * returns the callback's result + */ + var divmod = function (a, b, fn) { + var mod = a%b; + // in python, sign(a % b) === sign(b). Not in JS. If wrong side, add a + // round of b + if (mod > 0 && b < 0 || mod < 0 && b > 0) { + mod += b; + } + return fn(Math.floor(a/b), mod); + }; + /** + * Passes the fractional and integer parts of x to the callback, returns + * the callback's result + */ + var modf = function (x, fn) { + var mod = x%1; + if (mod < 0) { + mod += 1; + } + return fn(mod, Math.floor(x)); + }; + var zero = py.float.fromJSON(0); + // Port from pypy/lib_pypy/datetime.py + datetime.timedelta = py.type('timedelta', null, { + __init__: function () { + var args = py.PY_parseArgs(arguments, [ + ['days', zero], ['seconds', zero], ['microseconds', zero], + ['milliseconds', zero], ['minutes', zero], ['hours', zero], + ['weeks', zero] + ]); + + var d = 0, s = 0, m = 0; + var days = args.days.toJSON() + args.weeks.toJSON() * 7; + var seconds = args.seconds.toJSON() + + args.minutes.toJSON() * 60 + + args.hours.toJSON() * 3600; + var microseconds = args.microseconds.toJSON() + + args.milliseconds.toJSON() * 1000; + + // Get rid of all fractions, and normalize s and us. + // Take a deep breath . + var daysecondsfrac = modf(days, function (dayfrac, days) { + d = days; + if (dayfrac) { + return modf(dayfrac * 24 * 3600, function (dsf, dsw) { + s = dsw; + return dsf; + }); + } + return 0; + }); + + var secondsfrac = modf(seconds, function (sf, s) { + seconds = s; + return sf + daysecondsfrac; + }); + divmod(seconds, 24*3600, function (days, seconds) { + d += days; + s += seconds + }); + // seconds isn't referenced again before redefinition + + microseconds += secondsfrac * 1e6; + divmod(microseconds, 1000000, function (seconds, microseconds) { + divmod(seconds, 24*3600, function (days, seconds) { + d += days; + s += seconds; + m += Math.round(microseconds); + }); + }); + + // Carrying still possible here? + + this.days = d; + this.seconds = s; + this.microseconds = m; + }, + __str__: function () { + var hh, mm, ss; + divmod(this.seconds, 60, function (m, s) { + divmod(m, 60, function (h, m) { + hh = h; + mm = m; + ss = s; + }); + }); + var s = _.str.sprintf("%d:%02d:%02d", hh, mm, ss); + if (this.days) { + s = _.str.sprintf("%d day%s, %s", + this.days, + (this.days != 1 && this.days != -1) ? 's' : '', + s); + } + if (this.microseconds) { + s = _.str.sprintf("%s.%06d", s, this.microseconds); + } + return py.str.fromJSON(s); + }, + __eq__: function (other) { + if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + return py.False; + } + return (this.days === other.days + && this.seconds === other.seconds + && this.microseconds === other.microseconds) + ? py.True : py.False; + }, + __add__: function (other) { + if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + return py.NotImplemented; + } + return py.PY_call(datetime.timedelta, [ + py.float.fromJSON(this.days + other.days), + py.float.fromJSON(this.seconds + other.seconds), + py.float.fromJSON(this.microseconds + other.microseconds) + ]); + }, + __radd__: function (other) { return this.__add__(other); }, + __sub__: function (other) { + if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + return py.NotImplemented; + } + return py.PY_call(datetime.timedelta, [ + py.float.fromJSON(this.days - other.days), + py.float.fromJSON(this.seconds - other.seconds), + py.float.fromJSON(this.microseconds - other.microseconds) + ]); + }, + __rsub__: function (other) { + if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + return py.NotImplemented; + } + return this.__neg__().__add__(other); + }, + __neg__: function () { + return py.PY_call(datetime.timedelta, [ + py.float.fromJSON(-this.days), + py.float.fromJSON(-this.seconds), + py.float.fromJSON(-this.microseconds) + ]); + }, + __pos__: function () { return this; }, + __mul__: function (other) { + if (py.PY_call(py.isinstance, [other, py.float]) !== py.True) { + return py.NotImplemented; + } + var n = other.toJSON(); + return py.PY_call(datetime.timedelta, [ + py.float.fromJSON(this.days * n), + py.float.fromJSON(this.seconds * n), + py.float.fromJSON(this.microseconds * n) + ]); + }, + __rmul__: function (other) { return this.__mul__(other); }, + __div__: function (other) { + if (py.PY_call(py.isinstance, [other, py.float]) !== py.True) { + return py.NotImplemented; + } + var usec = ((this.days * 24 * 3600) + this.seconds) * 1000000 + + this.microseconds; + return py.PY_call( + datetime.timedelta, [ + zero, zero, py.float.fromJSON(usec / other.toJSON())]); + }, + __floordiv__: function (other) { return this.__div__(other); }, + total_seconds: function () { + return py.float.fromJSON( + this.days * 86400 + + this.seconds + + this.microseconds / 1000000) + }, + __nonzero__: function () { + return (!!this.days || !!this.seconds || !!this.microseconds) + ? py.True + : py.False; + } + }); datetime.datetime = py.type('datetime', null, { __init__: function () { var zero = py.float.fromJSON(0); @@ -108,7 +290,6 @@ openerp.web.pyeval = function (instance) { } } }); - var time = py.PY_call(py.object); time.strftime = py.PY_def.fromJSON(function () { // FIXME: needs PY_getattr @@ -127,7 +308,7 @@ openerp.web.pyeval = function (instance) { + 'weekday leakdays yearday nlyearday'); }, __add__: function (other) { - if (py.PY_call(py.isinstance, [datetime.date]) !== py.True) { + if (py.PY_call(py.isinstance, [other, datetime.date]) !== py.True) { return py.NotImplemented; } // TODO: test this whole mess @@ -172,7 +353,7 @@ openerp.web.pyeval = function (instance) { }, __sub__: function (other) { - if (py.PY_call(py.isinstance, [datetime.date]) !== py.True) { + if (py.PY_call(py.isinstance, [other, datetime.date]) !== py.True) { return py.NotImplemented; } // TODO: test this whole mess -- cgit v1.2.3 From 328537a7ba845d3cdf90826998d066dddec5c0de Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 10:52:06 +0100 Subject: [ADD] arithmetics between date and timedelta bzr revid: xmo@openerp.com-20121123095206-dtvoiignb7xnzsl0 --- addons/web/static/src/js/pyeval.js | 205 ++++++++++++++++++++++++++++++++++++- 1 file changed, 204 insertions(+), 1 deletion(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index e0f465279ef..df8f4132af5 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -41,7 +41,177 @@ openerp.web.pyeval = function (instance) { return fn(mod, Math.floor(x)); }; var zero = py.float.fromJSON(0); + // Port from pypy/lib_pypy/datetime.py + var DAYS_IN_MONTH = [null, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + var DAYS_BEFORE_MONTH = [null]; + var dbm = 0; + for (var i=1; i 2 && is_leap(year); + return DAYS_BEFORE_MONTH[month] + + (post_leap_feb ? 1 : 0); + }; + var ymd2ord = function (year, month, day) { + var dim = days_in_month(year, month); + if (!(1 <= day && day <= dim)) { + throw new Error("ValueError: day must be in 1.." + dim); + } + return days_before_year(year) + + days_before_month(year, month) + + day; + }; + var DI400Y = days_before_year(401); + var DI100Y = days_before_year(101); + var DI4Y = days_before_year(5); + var assert = function (bool) { + if (!bool) { + throw new Error("AssertionError"); + } + }; + var ord2ymd = function (n) { + --n; + var n400, n100, n4, n1, n0; + divmod(n, DI400Y, function (_n400, n) { + n400 = _n400; + divmod(n, DI100Y, function (_n100, n) { + n100 = _n100; + divmod(n, DI4Y, function (_n4, n) { + n4 = _n4; + divmod(n, 365, function (_n1, n) { + n1 = _n1; + n0 = n; + }) + }); + }); + }); + + n = n0; + var year = n400 * 400 + 1 + n100 * 100 + n4 * 4 + n1; + if (n1 == 4 || n100 == 100) { + assert(n0 === 0); + return { + year: year - 1, + month: 12, + day: 31 + }; + } + + var leapyear = n1 === 3 && (n4 !== 24 || n100 == 3); + assert(leapyear == is_leap(year)); + var month = (n + 50) >> 5; + var preceding = DAYS_BEFORE_MONTH[month] + ((month > 2 && leapyear) ? 1 : 0); + if (preceding > n) { + --month; + preceding -= DAYS_IN_MONTH[month] + ((month === 2 && leapyear) ? 1 : 0); + } + n -= preceding; + return { + year: year, + month: month, + day: n+1 + }; + }; + + /** + * Converts the stuff passed in into a valid date, applying overflows as needed + */ + var tmxxx = function (year, month, day, hour, minute, second, microsecond) { + hour = hour || 0; minute = minute || 0; second = second || 0; + microsecond = microsecond || 0; + + if (microsecond < 0 || microsecond > 999999) { + divmod(microsecond, 1000000, function (carry, ms) { + microsecond = ms; + second += carry + }); + } + if (second < 0 || second > 59) { + divmod(second, 60, function (carry, s) { + second = s; + minute += carry; + }); + } + if (minute < 0 || minute > 59) { + divmod(minute, 60, function (carry, m) { + minute = m; + hour += carry; + }) + } + if (hour < 0 || hour > 23) { + divmod(hour, 24, function (carry, h) { + hour = h; + day += carry; + }) + } + // That was easy. Now it gets muddy: the proper range for day + // can't be determined without knowing the correct month and year, + // but if day is, e.g., plus or minus a million, the current month + // and year values make no sense (and may also be out of bounds + // themselves). + // Saying 12 months == 1 year should be non-controversial. + if (month < 1 || month > 12) { + divmod(month-1, 12, function (carry, m) { + month = m + 1; + year += carry; + }) + } + // Now only day can be out of bounds (year may also be out of bounds + // for a datetime object, but we don't care about that here). + // If day is out of bounds, what to do is arguable, but at least the + // method here is principled and explainable. + var dim = days_in_month(year, month); + if (day < 1 || day > dim) { + // Move day-1 days from the first of the month. First try to + // get off cheap if we're only one day out of range (adjustments + // for timezone alone can't be worse than that). + if (day === 0) { + --month; + if (month > 0) { + day = days_in_month(year, month); + } else { + --year; month=12; day=31; + } + } else if (day == dim + 1) { + ++month; + day = 1; + if (month > 12) { + month = 1; + ++year; + } + } else { + var r = ord2ymd(ymd2ord(year, month, 1) + (day - 1)); + year = r.year; + month = r.month; + day = r.day; + } + } + return { + year: year, + month: month, + day: day, + hour: hour, + minute: minute, + second: second, + microsecond: microsecond + }; + }; datetime.timedelta = py.type('timedelta', null, { __init__: function () { var args = py.PY_parseArgs(arguments, [ @@ -274,7 +444,40 @@ openerp.web.pyeval = function (instance) { var d = new Date(); return py.PY_call( datetime.date, [d.getFullYear(), d.getMonth() + 1, d.getDate()]); - }) + }), + __eq__: function (other) { + return (this.year === other.year + && this.month === other.month + && this.day === other.day) + ? py.True : py.False; + }, + __add__: function (other) { + if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + return py.NotImplemented; + } + var s = tmxxx(this.year, this.month, this.day + other.days); + console.log(this, other, s); + return datetime.date.fromJSON(s.year, s.month, s.day); + }, + __radd__: function (other) { return this.__add__(other); }, + __sub__: function (other) { + if (py.PY_call(py.isinstance, [other, datetime.timedelta]) === py.True) { + return this.__add__(other.__neg__()); + } + if (py.PY_call(py.isinstance, [other, datetime.date]) === py.True) { + // FIXME: getattr and sub API methods + return py.PY_call(datetime.timedelta, [ + this.toordinal().__sub__(other.toordinal()) + ]); + } + return py.NotImplemented; + }, + toordinal: function () { + return py.float.fromJSON(ymd2ord(this.year, this.month, this.day)); + }, + fromJSON: function (year, month, day) { + return py.PY_call(datetime.date, [year, month, day]) + } }); datetime.time = py.type('time', null, { __init__: function () { -- cgit v1.2.3 From 242a5da5a9f4b71b3ced9f583a461c397f6be22a Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 10:58:24 +0100 Subject: [REM] stray logging bzr revid: xmo@openerp.com-20121123095824-xg6afc1l01t3y48y --- addons/web/static/src/js/pyeval.js | 1 - 1 file changed, 1 deletion(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index df8f4132af5..2b4d1ef5eea 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -456,7 +456,6 @@ openerp.web.pyeval = function (instance) { return py.NotImplemented; } var s = tmxxx(this.year, this.month, this.day + other.days); - console.log(this, other, s); return datetime.date.fromJSON(s.year, s.month, s.day); }, __radd__: function (other) { return this.__add__(other); }, -- cgit v1.2.3 From 7c09e23de7fb106f087c71bd4cbd6844b8ab3e57 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 11:16:58 +0100 Subject: [ADD] todo notes bzr revid: xmo@openerp.com-20121123101658-xoynynruwnuzyrv3 --- addons/web/static/src/js/pyeval.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 2b4d1ef5eea..ed9c1ce9e82 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -602,6 +602,7 @@ openerp.web.pyeval = function (instance) { var eval_contexts = function (contexts, evaluation_context) { evaluation_context = evaluation_context || {}; return _(contexts).reduce(function (result_context, ctx) { + // TODO: wrap string contexts in nonlit? // __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; } @@ -626,6 +627,7 @@ openerp.web.pyeval = function (instance) { var eval_domains = function (domains, evaluation_context) { var result_domain = []; _(domains).each(function (domain) { + // TODO: wrap string domains in nonlit? switch(domain.__ref) { case 'domain': evaluation_context.context = py.dict.fromJSON(evaluation_context); @@ -648,6 +650,7 @@ openerp.web.pyeval = function (instance) { var eval_groupbys = function (contexts, evaluation_context) { var result_group = []; _(contexts).each(function (ctx) { + // TODO: wrap string contexts in nonlit? var group; var evaluated = ctx; switch(ctx.__ref) { -- cgit v1.2.3 From 49bd834725989b1b59221cd335db53094ec88479 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 11:18:22 +0100 Subject: [IMP] deferred init style bzr revid: xmo@openerp.com-20121123101822-inox7gkxvd0gtqbp --- addons/web/static/src/js/pyeval.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index ed9c1ce9e82..d00654fc8ef 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -737,8 +737,7 @@ openerp.web.pyeval = function (instance) { } }; instance.web.pyeval.eval_domains_and_contexts = function (source) { - var d = new $.Deferred(); - setTimeout(function () { + return new $.Deferred(function (d) {setTimeout(function () { try { var contexts = ([instance.session.context] || []).concat(source.contexts); // see Session.eval_context in Python @@ -759,7 +758,6 @@ openerp.web.pyeval = function (instance) { } }}); } - }, 0); - return d; + }, 0); }); } }; -- cgit v1.2.3 From 709f2506ff40d799aada58df74dd78522d965868 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 12:39:32 +0100 Subject: [REM] server-side eval_domain_and_context bzr revid: xmo@openerp.com-20121123113932-siz0u2vp2u7z6wk2 --- addons/web/static/src/js/pyeval.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index d00654fc8ef..e16880889d2 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -739,13 +739,13 @@ openerp.web.pyeval = function (instance) { instance.web.pyeval.eval_domains_and_contexts = function (source) { return new $.Deferred(function (d) {setTimeout(function () { try { - var contexts = ([instance.session.context] || []).concat(source.contexts); + var contexts = ([instance.session.user_context] || []).concat(source.contexts); // see Session.eval_context in Python - d.resolve({result: { + d.resolve({ context: instance.web.pyeval.eval('contexts', contexts), domain: instance.web.pyeval.eval('domains', source.domains), - group_by: instance.web.pyeval.eval('groupbys', source.group_by_seq) - }}, 'success', {}); + group_by: instance.web.pyeval.eval('groupbys', source.group_by_seq || []) + }); } catch (e) { d.resolve({ error: { code: 400, -- cgit v1.2.3 From 6b2b2d889c94b0cbab5fbe14a4555f4c8dbe179c Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Fri, 23 Nov 2012 12:49:23 +0100 Subject: [IMP] move nonliteral domains and contexts to JS test case bzr revid: xmo@openerp.com-20121123114923-t4047w6jztria91x --- addons/web/static/src/js/pyeval.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index e16880889d2..3c6b6619513 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -602,10 +602,13 @@ openerp.web.pyeval = function (instance) { var eval_contexts = function (contexts, evaluation_context) { evaluation_context = evaluation_context || {}; return _(contexts).reduce(function (result_context, ctx) { - // TODO: wrap string contexts in nonlit? // __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': @@ -627,7 +630,10 @@ openerp.web.pyeval = function (instance) { var eval_domains = function (domains, evaluation_context) { var result_domain = []; _(domains).each(function (domain) { - // TODO: wrap string domains in nonlit? + if (_.isString(domain)) { + // wrap raw strings in domain + domain = { __ref: 'domain', __debug: domain }; + } switch(domain.__ref) { case 'domain': evaluation_context.context = py.dict.fromJSON(evaluation_context); @@ -650,7 +656,10 @@ openerp.web.pyeval = function (instance) { var eval_groupbys = function (contexts, evaluation_context) { var result_group = []; _(contexts).each(function (ctx) { - // TODO: wrap string contexts in nonlit? + if (_.isString(ctx)) { + // wrap raw strings in context + ctx = { __ref: 'context', __debug: ctx }; + } var group; var evaluated = ctx; switch(ctx.__ref) { -- cgit v1.2.3 From 929a656619578bbf5c91add35f890bfc623e3a25 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 26 Nov 2012 09:42:29 +0100 Subject: [IMP] allow eval'ing a single context or domain bzr revid: xmo@openerp.com-20121126084229-wgpjszmquhibbrpf --- addons/web/static/src/js/pyeval.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 3c6b6619513..4a77eead90f 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -708,8 +708,11 @@ openerp.web.pyeval = function (instance) { context = _.extend(instance.web.pyeval.context(), context || {}); context['context'] = py.dict.fromJSON(context); + //noinspection FallthroughInSwitchStatementJS switch(type) { + case 'context': object = [object]; case 'contexts': return eval_contexts(object, context); + case 'domain': object = [object]; case 'domains': return eval_domains(object, context); case 'groupbys': return eval_groupbys(object, context); } -- cgit v1.2.3 From 89d9cd5910cd72009e5eff32214a1568f5acede6 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 28 Nov 2012 12:05:42 +0100 Subject: [FIX] use py.js 0.7 API bzr revid: xmo@openerp.com-20121128110542-nplg9o3e5jpxmqic --- addons/web/static/src/js/pyeval.js | 49 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 25 deletions(-) (limited to 'addons/web/static/src/js/pyeval.js') diff --git a/addons/web/static/src/js/pyeval.js b/addons/web/static/src/js/pyeval.js index 4a77eead90f..265449f9f64 100644 --- a/addons/web/static/src/js/pyeval.js +++ b/addons/web/static/src/js/pyeval.js @@ -288,7 +288,7 @@ openerp.web.pyeval = function (instance) { return py.str.fromJSON(s); }, __eq__: function (other) { - if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + if (!py.PY_isInstance(other, datetime.timedelta)) { return py.False; } return (this.days === other.days @@ -297,7 +297,7 @@ openerp.web.pyeval = function (instance) { ? py.True : py.False; }, __add__: function (other) { - if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + if (!py.PY_isInstance(other, datetime.timedelta)) { return py.NotImplemented; } return py.PY_call(datetime.timedelta, [ @@ -308,7 +308,7 @@ openerp.web.pyeval = function (instance) { }, __radd__: function (other) { return this.__add__(other); }, __sub__: function (other) { - if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + if (!py.PY_isInstance(other, datetime.timedelta)) { return py.NotImplemented; } return py.PY_call(datetime.timedelta, [ @@ -318,7 +318,7 @@ openerp.web.pyeval = function (instance) { ]); }, __rsub__: function (other) { - if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + if (!py.PY_isInstance(other, datetime.timedelta)) { return py.NotImplemented; } return this.__neg__().__add__(other); @@ -332,7 +332,7 @@ openerp.web.pyeval = function (instance) { }, __pos__: function () { return this; }, __mul__: function (other) { - if (py.PY_call(py.isinstance, [other, py.float]) !== py.True) { + if (!py.PY_isInstance(other, py.float)) { return py.NotImplemented; } var n = other.toJSON(); @@ -344,7 +344,7 @@ openerp.web.pyeval = function (instance) { }, __rmul__: function (other) { return this.__mul__(other); }, __div__: function (other) { - if (py.PY_call(py.isinstance, [other, py.float]) !== py.True) { + if (!py.PY_isInstance(other, py.float)) { return py.NotImplemented; } var usec = ((this.days * 24 * 3600) + this.seconds) * 1000000 @@ -410,13 +410,12 @@ openerp.web.pyeval = function (instance) { combine: py.classmethod.fromJSON(function () { var args = py.PY_parseArgs(arguments, 'date time'); return py.PY_call(datetime.datetime, [ - // FIXME: should use getattr - args.date.year, - args.date.month, - args.date.day, - args.time.hour, - args.time.minute, - args.time.second + py.PY_getAttr(args.date, 'year'), + py.PY_getAttr(args.date, 'month'), + py.PY_getAttr(args.date, 'day'), + py.PY_getAttr(args.time, 'hour'), + py.PY_getAttr(args.time, 'minute'), + py.PY_getAttr(args.time, 'second') ]); }) }); @@ -452,7 +451,7 @@ openerp.web.pyeval = function (instance) { ? py.True : py.False; }, __add__: function (other) { - if (py.PY_call(py.isinstance, [other, datetime.timedelta]) !== py.True) { + if (!py.PY_isInstance(other, datetime.timedelta)) { return py.NotImplemented; } var s = tmxxx(this.year, this.month, this.day + other.days); @@ -460,13 +459,15 @@ openerp.web.pyeval = function (instance) { }, __radd__: function (other) { return this.__add__(other); }, __sub__: function (other) { - if (py.PY_call(py.isinstance, [other, datetime.timedelta]) === py.True) { + if (py.PY_isInstance(other, datetime.timedelta)) { return this.__add__(other.__neg__()); } - if (py.PY_call(py.isinstance, [other, datetime.date]) === py.True) { + if (py.PY_isInstance(other, datetime.date)) { // FIXME: getattr and sub API methods return py.PY_call(datetime.timedelta, [ - this.toordinal().__sub__(other.toordinal()) + py.PY_subtract( + py.PY_call(py.PY_getAttr(this, 'toordinal')), + py.PY_call(py.PY_getAttr(other, 'toordinal'))) ]); } return py.NotImplemented; @@ -494,12 +495,10 @@ openerp.web.pyeval = function (instance) { }); var time = py.PY_call(py.object); time.strftime = py.PY_def.fromJSON(function () { - // FIXME: needs PY_getattr - var d = py.PY_call(datetime.__getattribute__('datetime') - .__getattribute__('now')); - var args = [].slice.call(arguments); - return py.PY_call.apply( - null, [d.__getattribute__('strftime')].concat(args)); + var args = py.PY_parseArgs(arguments, 'format'); + var dt_class = py.PY_getAttr(datetime, 'datetime'); + var d = py.PY_call(py.PY_getAttr(dt_class, 'now')); + return py.PY_call(py.PY_getAttr(d, 'strftime'), [args.format]); }); var relativedelta = py.type('relativedelta', null, { @@ -510,7 +509,7 @@ openerp.web.pyeval = function (instance) { + 'weekday leakdays yearday nlyearday'); }, __add__: function (other) { - if (py.PY_call(py.isinstance, [other, datetime.date]) !== py.True) { + if (!py.PY_isInstance(other, datetime.date)) { return py.NotImplemented; } // TODO: test this whole mess @@ -555,7 +554,7 @@ openerp.web.pyeval = function (instance) { }, __sub__: function (other) { - if (py.PY_call(py.isinstance, [other, datetime.date]) !== py.True) { + if (!py.PY_isInstance(other, datetime.date)) { return py.NotImplemented; } // TODO: test this whole mess -- cgit v1.2.3