[FIX] web: write date without separator

Since fbbfa6d it's not longer possible
to write a date without separator (e.g., 0102 for 01/02/2019; 121298 for
12/12/1998;  02042018 for 02/04/2018), or a date without year (e.g.,
01/02 for 01/02/2019) in the datepicker widget.

In this commit we have decided to be as permissive as tempus dominus
that call moment.js with strict false. This allows moment.js to deduct a
date from a string in a more versatile way. This also allows a more
versatile autocomplete in the search views.

opw-2070464
opw-2070931

closes odoo/odoo#37095

Forward-port-of: odoo/odoo#36821
Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
This commit is contained in:
Jorge Pinna Puissant
2019-09-19 07:07:03 +00:00
parent d786d7d250
commit f629cd9672
4 changed files with 44 additions and 2 deletions
@@ -415,7 +415,7 @@ function parseDate(value, field, options) {
if (options && options.isUTC) {
date = moment.utc(value);
} else {
date = moment.utc(value, [datePattern, datePatternWoZero, moment.ISO_8601], true);
date = moment.utc(value, [datePattern, datePatternWoZero, moment.ISO_8601]);
}
if (date.isValid()) {
if (date.year() === 0) {
@@ -459,7 +459,7 @@ function parseDateTime(value, field, options) {
// phatomjs crash if we don't use this format
datetime = moment.utc(value.replace(' ', 'T') + 'Z');
} else {
datetime = moment.utc(value, [pattern1, pattern2, moment.ISO_8601], true);
datetime = moment.utc(value, [pattern1, pattern2, moment.ISO_8601]);
if (options && options.timezone) {
datetime.add(-session.getTZOffset(datetime), 'minutes');
}
@@ -88,6 +88,13 @@ var DateWidget = Widget.extend({
* set datetime value
*/
changeDatetime: function () {
if (this.__libInput > 0) {
if (this.options.warn_future) {
this._warnFuture(this.getValue());
}
this.trigger("datetime_changed");
return;
}
var oldValue = this.getValue();
if (this.isValid()) {
this._setValueFromUi();
@@ -353,5 +353,39 @@ QUnit.test('parse datetime', function (assert) {
core._t.database.parameters = originalParameters;
});
QUnit.test('parse date without separator', function (assert) {
assert.expect(8);
var originalParameters = _.clone(core._t.database.parameters);
_.extend(core._t.database.parameters, {date_format: '%d.%m/%Y'});
var dateFormat = "DD.MM/YYYY";
assert.throws(function () {fieldUtils.parse.date("1197")}, /is not a correct/, "Wrongly formated dates should be invalid");
assert.throws(function () {fieldUtils.parse.date("0131")}, /is not a correct/, "Wrongly formated dates should be invalid");
assert.throws(function () {fieldUtils.parse.date("970131")}, /is not a correct/, "Wrongly formated dates should be invalid");
assert.equal(fieldUtils.parse.date("3101").format(dateFormat), "31.01/" + moment.utc().year());
assert.equal(fieldUtils.parse.date("31.01").format(dateFormat), "31.01/" + moment.utc().year());
assert.equal(fieldUtils.parse.date("310197").format(dateFormat), "31.01/1997");
assert.equal(fieldUtils.parse.date("310117").format(dateFormat), "31.01/2017");
assert.equal(fieldUtils.parse.date("31011985").format(dateFormat), "31.01/1985");
core._t.database.parameters = originalParameters;
});
QUnit.test('parse datetime without separator', function (assert) {
assert.expect(3);
var originalParameters = _.clone(core._t.database.parameters);
_.extend(core._t.database.parameters, {date_format: '%d.%m/%Y', time_format: '%H:%M/%S'});
var dateTimeFormat = "DD.MM/YYYY HH:mm/ss";
assert.equal(fieldUtils.parse.datetime("3101198508").format(dateTimeFormat), "31.01/1985 08:00/00");
assert.equal(fieldUtils.parse.datetime("310119850833").format(dateTimeFormat), "31.01/1985 08:33/00");
assert.equal(fieldUtils.parse.datetime("31/01/1985 08").format(dateTimeFormat), "31.01/1985 08:00/00");
core._t.database.parameters = originalParameters;
});
});
});
@@ -604,6 +604,7 @@ QUnit.module('Search View', {
// DateTime case
$autocomplete = $('.o_searchview_input');
await stringToEvent($autocomplete, '07/15/1983 00:00:00');
await testUtils.fields.triggerKey('down', $autocomplete, 'down');
await testUtils.fields.triggerKey('up', $autocomplete, 'enter');
assert.equal($('.o_searchview_facet .o_facet_values').text().trim(), '07/15/1983 00:00:00',