[FIX] web: py.js evaluation of to_utc

The class PyDateTime did not have a method to_utc. For that reason, it was
impossible for the search model to evaluate a dynamic domains like
"[('date', '>=', (datetime.datetime.combine(context_today(), datetime.time(0,0,0)).to_utc()))]"

We implement that method.

X-original-commit: 3ad9a3be64da78d596ee3dc435d4b224728a9e76
Part-of: odoo/odoo#77035
This commit is contained in:
Mathieu Duckerts-Antoine
2021-09-23 11:26:09 +00:00
parent dfa90a7ffc
commit 31c9bcaaca
3 changed files with 63 additions and 0 deletions
@@ -362,6 +362,21 @@ export class PyDateTime {
toJSON() {
return this.strftime("%Y-%m-%d %H:%M:%S");
}
to_utc() {
const d = new Date(this.year, this.month, this.day, this.hour, this.minute, this.second);
const offset = d.getTimezoneOffset();
// previous implementation did use timedelta
const s = tmxxx(
this.year,
this.month,
this.day,
this.hour,
this.minute,
this.second + 60 * offset
);
return new PyDateTime(s.year, s.month, s.day, s.hour, s.minute, s.second);
}
}
export class PyTime extends PyDate {
@@ -1,6 +1,7 @@
/** @odoo-module **/
import { evaluateExpr } from "@web/core/py_js/py";
import { patchDate, patchWithCleanup } from "@web/../tests/helpers/utils";
QUnit.module("py", {}, () => {
QUnit.module("date stuff", () => {
@@ -55,6 +56,20 @@ QUnit.module("py", {}, () => {
assert.strictEqual(evaluateExpr(expr3), "2001-04-03 00:32:12");
});
QUnit.test("to_utc", (assert) => {
patchDate(2021, 8, 17, 10, 0, 0);
patchWithCleanup(Date.prototype, {
getTimezoneOffset() {
return -360;
},
});
const expr =
"datetime.datetime.combine(context_today(), datetime.time(0,0,0)).to_utc()";
assert.strictEqual(JSON.stringify(evaluateExpr(expr)), `"2021-09-16 18:00:00"`);
});
QUnit.test("datetime.datetime.combine", (assert) => {
const expr =
"datetime.datetime.combine(context_today(), datetime.time(23,59,59)).strftime('%Y-%m-%d %H:%M:%S')";
@@ -2,6 +2,7 @@
import { patchDate } from "@web/../tests/helpers/utils";
import { makeWithSearch, setupControlPanelServiceRegistry } from "./helpers";
import { patchWithCleanup } from "@web/../tests/helpers/utils";
const { Component, tags } = owl;
const { xml } = tags;
@@ -777,4 +778,36 @@ QUnit.module("Search", (hooks) => {
]);
assert.deepEqual(model.groupBy, ["date_field:month"]);
});
QUnit.test("dynamic domains evaluation", async function (assert) {
patchDate(2021, 8, 17, 10, 0, 0);
patchWithCleanup(Date.prototype, {
getTimezoneOffset() {
return -120;
},
});
const searchViewArch = `
<search>
<filter name="filter_0" domain="[('datetime', '=', (datetime.datetime.combine(context_today(), datetime.time(0,0,0)).to_utc()))]"/>
<filter name="filter_1" domain="[('date', '=', context_today() + relativedelta(days=-365))]"/>
<filter name="filter_2" domain="[('create_date', '&gt;', (context_today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d'))]"/>
</search>
`;
const evaluatedDomains = [
[["datetime", "=", "2021-09-16 22:00:00"]],
[["date", "=", "2020-09-17 00:00:00"]],
[["create_date", ">", "2021-09-16"]],
];
const model = await makeSearchModel({ serverData, searchViewArch });
for (let i = 0; i < evaluatedDomains.length; i++) {
model.toggleSearchItem(i + 1);
assert.deepEqual(model.domain, evaluatedDomains[i]);
model.toggleSearchItem(i + 1);
}
});
});