From 6cc5a9963b3cce5d6ddf2cd038def5fe21f17af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Ku=CC=88hn?= Date: Mon, 4 Dec 2017 11:01:36 +0100 Subject: [PATCH] [FIX] web,*: time format from user's language Before this fix, the chatter always showed dates of messages in military time format. This was not the expected behavior, as the user could have had customize the date and time format in the settings of the current language. Steps to reproduce the issue: 1. Set Odoo in English 2. Settings > Translations > Languages > English 2.1. Set Date Format to %m/%d/%Y 2.2. Set Time Format to %I:%M:%S %p 3. CRM > (Select any opportunity with at least one message) > Mouse hover on elapsed time (e.g. "an hour ago") Expected: - It shows the customized time format (e.g. 11/07/2017 5:33:36 PM) Results: - It shows the military time format (e.g. Tue Nov 07 2017 17:33:36 GMT+0100) opw-781647 --- .../reconciliation/reconciliation_renderer.js | 2 +- addons/mail/static/src/js/activity.js | 12 +++++++--- addons/mail/static/src/js/thread.js | 2 ++ addons/mail/static/src/xml/activity.xml | 4 ++-- addons/mail/static/src/xml/thread.xml | 4 ++-- addons/survey/static/src/js/survey.js | 3 +-- addons/web/static/src/js/core/time.js | 24 +++++++++++++++++++ .../web/static/src/js/fields/field_utils.js | 15 ++++-------- .../src/js/views/calendar/calendar_model.js | 4 ++-- .../web/static/src/js/widgets/date_picker.js | 2 +- addons/website/static/src/js/menu/content.js | 2 +- .../static/src/js/website_form.js | 8 +++---- 12 files changed, 54 insertions(+), 28 deletions(-) diff --git a/addons/account/static/src/js/reconciliation/reconciliation_renderer.js b/addons/account/static/src/js/reconciliation/reconciliation_renderer.js index 9e72fdeb44f..1a36cdbe6f2 100644 --- a/addons/account/static/src/js/reconciliation/reconciliation_renderer.js +++ b/addons/account/static/src/js/reconciliation/reconciliation_renderer.js @@ -113,7 +113,7 @@ var StatementRenderer = Widget.extend(FieldManagerMixin, { if (state.valuenow === state.valuemax && !this.$('.done_message').length) { var dt = Date.now()-this.time; var $done = $(qweb.render("reconciliation.done", { - 'duration': moment(dt).utc().format(time.strftime_to_moment_format(_t.database.parameters.time_format)), + 'duration': moment(dt).utc().format(time.getLangTimeFormat()), 'number': state.valuenow, 'timePerTransaction': Math.round(dt/1000/state.valuemax), 'context': state.context, diff --git a/addons/mail/static/src/js/activity.js b/addons/mail/static/src/js/activity.js index a2ae24dff91..4d3d7adee50 100644 --- a/addons/mail/static/src/js/activity.js +++ b/addons/mail/static/src/js/activity.js @@ -20,8 +20,7 @@ var setDelayLabel = function(activities){ var today = moment().startOf('day'); _.each(activities, function(activity){ var to_display = ''; - var deadline = moment(activity.date_deadline + ' 00:00:00'); - var diff = deadline.diff(today, 'days', true); // true means no rounding + var diff = activity.date_deadline.diff(today, 'days', true); // true means no rounding if(diff === 0){ to_display = _t('Today'); }else{ @@ -72,10 +71,17 @@ var AbstractActivityField = AbstractField.extend({ }); } return $.when(fetch_def).then(function (results) { + // convert create_date and date_deadline to moments + _.each(results, function (activity) { + activity.create_date = moment(time.auto_str_to_date(activity.create_date)); + activity.date_deadline = moment(time.auto_str_to_date(activity.date_deadline)); + }); + // filter out activities that are no longer linked to this record self.activities = _.filter(self.activities.concat(results || []), function (activity) { return _.contains(self.value.res_ids, activity.id); }); + // sort activities by due date self.activities = _.sortBy(self.activities, 'date_deadline'); }); @@ -122,7 +128,6 @@ var Activity = AbstractActivityField.extend({ var fetch_def = this.dp.add(this._readActivities()); return fetch_def.then(function () { _.each(self.activities, function (activity) { - activity.time_ago = moment(time.auto_str_to_date(activity.create_date)).fromNow(); if (activity.note) { activity.note = utils.parse_and_transform(activity.note, utils.add_link); } @@ -135,6 +140,7 @@ var Activity = AbstractActivityField.extend({ nbPlannedActivities: nbActivities.planned, nbTodayActivities: nbActivities.today, nbOverdueActivities: nbActivities.overdue, + date_format: time.getLangDatetimeFormat(), })); } else { self.$el.empty(); diff --git a/addons/mail/static/src/js/thread.js b/addons/mail/static/src/js/thread.js index ec5ffdda1a9..68a01df2345 100644 --- a/addons/mail/static/src/js/thread.js +++ b/addons/mail/static/src/js/thread.js @@ -2,6 +2,7 @@ odoo.define('mail.ChatThread', function (require) { "use strict"; var core = require('web.core'); +var time = require('web.time'); var DocumentViewer = require('mail.DocumentViewer'); var Widget = require('web.Widget'); @@ -111,6 +112,7 @@ var Thread = Widget.extend({ messages: msgs, options: options, ORDER: ORDER, + date_format: time.getLangDatetimeFormat(), })); this.attachments = _.uniq(_.flatten(_.map(messages, 'attachment_ids'))); diff --git a/addons/mail/static/src/xml/activity.xml b/addons/mail/static/src/xml/activity.xml index 2b0316ad59f..83dc4295e73 100644 --- a/addons/mail/static/src/xml/activity.xml +++ b/addons/mail/static/src/xml/activity.xml @@ -40,13 +40,13 @@
Created on
- +
Assigned to
- , due on + , due on
diff --git a/addons/mail/static/src/xml/thread.xml b/addons/mail/static/src/xml/thread.xml index 89f0921a6cc..6f5f5e71cfe 100644 --- a/addons/mail/static/src/xml/thread.xml +++ b/addons/mail/static/src/xml/thread.xml @@ -145,7 +145,7 @@ t-att-src="message.avatar_src" class="o_thread_message_avatar img-circle"/> - + - - + - on diff --git a/addons/survey/static/src/js/survey.js b/addons/survey/static/src/js/survey.js index e6db7435972..230003de673 100644 --- a/addons/survey/static/src/js/survey.js +++ b/addons/survey/static/src/js/survey.js @@ -181,9 +181,8 @@ if(!the_form.length) { // frontend does not load moment locale at all. // so wait until DOM ready with locale then init datetimepicker ready_with_locale.then(function(){ - var l10n = _t.database.parameters; $('.form-control.date').datetimepicker({ - format : time.strftime_to_moment_format(l10n.date_format), + format : time.getLangDateFormat(), minDate: moment({ y: 1900 }), maxDate: moment().add(200, "y"), calendarWeeks: true, diff --git a/addons/web/static/src/js/core/time.js b/addons/web/static/src/js/core/time.js index af13aa5a3b1..47a4c81465d 100644 --- a/addons/web/static/src/js/core/time.js +++ b/addons/web/static/src/js/core/time.js @@ -279,6 +279,27 @@ var normalize_format_table = { }; var inverse_normalize_format_table = _.invert(normalize_format_table); +/** + * Get date format of the user's language + */ +function getLangDateFormat() { + return this.strftime_to_moment_format(_t.database.parameters.date_format); +} + +/** + * Get time format of the user's language + */ +function getLangTimeFormat() { + return this.strftime_to_moment_format(_t.database.parameters.time_format); +} + +/** + * Get date time format of the user's language + */ +function getLangDatetimeFormat() { + return this.strftime_to_moment_format(_t.database.parameters.date_format + " " + _t.database.parameters.time_format); +} + return { @@ -293,6 +314,9 @@ return { auto_date_to_str: auto_date_to_str, strftime_to_moment_format: strftime_to_moment_format, moment_to_strftime_format: moment_to_strftime_format, + getLangDateFormat: getLangDateFormat, + getLangTimeFormat: getLangTimeFormat, + getLangDatetimeFormat: getLangDatetimeFormat, }; }); diff --git a/addons/web/static/src/js/fields/field_utils.js b/addons/web/static/src/js/fields/field_utils.js index 7ba4122cccb..bca74acc088 100644 --- a/addons/web/static/src/js/fields/field_utils.js +++ b/addons/web/static/src/js/fields/field_utils.js @@ -97,8 +97,7 @@ function formatDate(value, field, options) { value = value.clone().add(session.getTZOffset(value), 'minutes'); } } - var l10n = core._t.database.parameters; - var date_format = time.strftime_to_moment_format(l10n.date_format); + var date_format = time.getLangDateFormat(); return value.format(date_format); } @@ -122,11 +121,7 @@ function formatDateTime(value, field, options) { if (!options || !('timezone' in options) || options.timezone) { value = value.clone().add(session.getTZOffset(value), 'minutes'); } - var l10n = core._t.database.parameters; - var date_format = time.strftime_to_moment_format(l10n.date_format); - var time_format = time.strftime_to_moment_format(l10n.time_format); - var datetime_format = date_format + ' ' + time_format; - return value.format(datetime_format); + return value.format(time.getLangDatetimeFormat()); } /** @@ -349,7 +344,7 @@ function parseDate(value, field, options) { if (!value) { return false; } - var datePattern = time.strftime_to_moment_format(core._t.database.parameters.date_format); + var datePattern = time.getLangDateFormat(); var datePatternWoZero = datePattern.replace('MM','M').replace('DD','D'); var date; if (options && options.isUTC) { @@ -388,8 +383,8 @@ function parseDateTime(value, field, options) { if (!value) { return false; } - var datePattern = time.strftime_to_moment_format(core._t.database.parameters.date_format), - timePattern = time.strftime_to_moment_format(core._t.database.parameters.time_format); + var datePattern = time.getLangDateFormat(), + timePattern = time.getLangTimeFormat(); var datePatternWoZero = datePattern.replace('MM','M').replace('DD','D'), timePatternWoZero = timePattern.replace('HH','H').replace('mm','m').replace('ss','s'); var pattern1 = datePattern + ' ' + timePattern; diff --git a/addons/web/static/src/js/views/calendar/calendar_model.js b/addons/web/static/src/js/views/calendar/calendar_model.js index a0f6515b98c..fac6845a31f 100644 --- a/addons/web/static/src/js/views/calendar/calendar_model.js +++ b/addons/web/static/src/js/views/calendar/calendar_model.js @@ -382,8 +382,8 @@ return AbstractModel.extend({ allDayText: _t("All day"), views: { week: { - columnFormat: 'ddd ' + time.strftime_to_moment_format(_t.database.parameters.date_format), - titleFormat: time.strftime_to_moment_format(_t.database.parameters.time_format), + columnFormat: 'ddd ' + time.getLangDateFormat(), + titleFormat: time.getLangTimeFormat(), } }, monthNames: moment.months(), diff --git a/addons/web/static/src/js/widgets/date_picker.js b/addons/web/static/src/js/widgets/date_picker.js index 3573e9232e5..8d0b04332ad 100644 --- a/addons/web/static/src/js/widgets/date_picker.js +++ b/addons/web/static/src/js/widgets/date_picker.js @@ -26,7 +26,7 @@ var DateWidget = Widget.extend({ this.name = parent.name; this.options = _.defaults(options || {}, { - format : time.strftime_to_moment_format((this.type_of_date === 'datetime')? (l10n.date_format + ' ' + l10n.time_format) : l10n.date_format), + format : this.type_of_date === 'datetime' ? time.getLangDatetimeFormat() : time.getLangDateFormat(), minDate: moment({ y: 1900 }), maxDate: moment().add(200, "y"), calendarWeeks: true, diff --git a/addons/website/static/src/js/menu/content.js b/addons/website/static/src/js/menu/content.js index 9807b534b50..020ed362d2b 100644 --- a/addons/website/static/src/js/menu/content.js +++ b/addons/website/static/src/js/menu/content.js @@ -140,7 +140,7 @@ var PagePropertiesDialog = widget.Dialog.extend({ down: 'fa fa-chevron-down', }, locale : moment.locale(), - format : time.strftime_to_moment_format(l10n.date_format +' '+ l10n.time_format), + format : time.getLangDatetimeFormat(), widgetPositioning : { horizontal: 'auto', vertical: 'top', diff --git a/addons/website_form/static/src/js/website_form.js b/addons/website_form/static/src/js/website_form.js index e98fae5e0ce..aa540650473 100644 --- a/addons/website_form/static/src/js/website_form.js +++ b/addons/website_form/static/src/js/website_form.js @@ -40,12 +40,12 @@ odoo.define('website_form.animation', function (require) { down: 'fa fa-chevron-down', }, locale : moment.locale(), - format : time.strftime_to_moment_format(l10n.date_format +' '+ l10n.time_format), + format : time.getLangDatetimeFormat(), }; this.$target.find('.o_website_form_datetime').datetimepicker(datepickers_options); // Adapt options to date-only pickers - datepickers_options.format = time.strftime_to_moment_format(l10n.date_format); + datepickers_options.format = time.getLangDateFormat(); this.$target.find('.o_website_form_date').datetimepicker(datepickers_options); return this._super.apply(this, arguments); @@ -209,8 +209,8 @@ odoo.define('website_form.animation', function (require) { // This is a stripped down version of format.js parse_value function parse_date: function (value, type_of_date, value_if_empty) { - var date_pattern = time.strftime_to_moment_format(_t.database.parameters.date_format), - time_pattern = time.strftime_to_moment_format(_t.database.parameters.time_format); + var date_pattern = time.getLangDateFormat(), + time_pattern = time.getLangTimeFormat(); var date_pattern_wo_zero = date_pattern.replace('MM','M').replace('DD','D'), time_pattern_wo_zero = time_pattern.replace('HH','H').replace('mm','m').replace('ss','s'); switch (type_of_date) {