From dfc92c66a67fee0bdcbe29d98beca453a8f0349a Mon Sep 17 00:00:00 2001 From: Nicolas Lempereur Date: Tue, 12 Nov 2019 17:18:46 +0000 Subject: [PATCH] [FIX] web: week view ok if week start not sunday/monday Currently, if the first week day was Saturday the calendar week view would possibly get the wrong week. This is because current heuristic would get date of current week from 6 (Saturday) to 12 (Sunday). Thus if we were: - on Friday 14th, we would have a week day ranges: 15-21 (wrong) - on Saturday 15th, we would have a week day ranges: 15-21 (ok) - on Sunday 16th, we would have a week day ranges: 22-28 (wrong) So it would only get the right range when getting range from saturday. If a week: - start on monday, the week range would only be wrong on sunday. - start on sunday, the week range would always be alright. Added test without the fix fail: - CalendarView: Saturday week start week mode The domain to search events in should be correct (domain range 14-20 instead of correct 07-13 whilst the day was 12th) - CalendarView: Monday week start week mode The domain to search events in should be correct (domain range 16-22 instead of correct 09-15 whilst the day was 15th) opw-2091448 closes #40244 closes odoo/odoo#40327 X-original-commit: 6c3d74846581d6c1bfc0c6e3a7104440515ddbc5 Signed-off-by: Nicolas Lempereur (nle) --- .../src/js/views/calendar/calendar_model.js | 11 ++- .../web/static/tests/views/calendar_tests.js | 92 ++++++++++++++++++- 2 files changed, 100 insertions(+), 3 deletions(-) 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 dc27ff1bfdb..a12f8f0469d 100644 --- a/addons/web/static/src/js/views/calendar/calendar_model.js +++ b/addons/web/static/src/js/views/calendar/calendar_model.js @@ -297,8 +297,15 @@ return AbstractModel.extend({ this.data.end_date = this.data.start_date.clone().add(5, 'week').day(this.week_stop).endOf('day'); break; case 'week': - this.data.start_date = this.data.start_date.clone().day(this.week_start).startOf('day'); - this.data.end_date = this.data.end_date.clone().day(this.week_stop).endOf('day'); + var weekStart = this.data.start_date.clone().startOf('week'); + var weekStartDay = this.week_start; + if (this.data.start_date.day() < this.week_start) { + // The week's first day is after our current day + // Then we should go back to the previous week + weekStartDay -= 7; + } + this.data.start_date = this.data.start_date.clone().day(weekStartDay).startOf('day'); + this.data.end_date = this.data.end_date.clone().day(weekStartDay + 6).endOf('day'); break; default: this.data.start_date = this.data.start_date.clone().startOf('day'); diff --git a/addons/web/static/tests/views/calendar_tests.js b/addons/web/static/tests/views/calendar_tests.js index b4fae73ff70..6b7915c0e69 100644 --- a/addons/web/static/tests/views/calendar_tests.js +++ b/addons/web/static/tests/views/calendar_tests.js @@ -2838,7 +2838,7 @@ QUnit.module('Views', { assert.expect(8); // 2019-09-12 08:00:00 - var initDate = new Date(2019, 8, 12, 8, 0, 0); + var initDate = new Date(2019, 8, 15, 8, 0, 0); initDate = new Date(initDate.getTime() - initDate.getTimezoneOffset()*60*1000); var calendar = await createCalendarView({ @@ -2892,6 +2892,96 @@ QUnit.module('Views', { calendar.destroy(); }); + + QUnit.test('Monday week start week mode', async function (assert) { + assert.expect(3); + + // 2019-09-12 08:00:00 + var initDate = new Date(2019, 8, 15, 8, 0, 0); + initDate = new Date(initDate.getTime() - initDate.getTimezoneOffset()*60*1000); + + var calendar = await createCalendarView({ + View: CalendarView, + model: 'event', + data: this.data, + arch: + ''+ + '', + archs: archs, + + viewOptions: { + initialDate: initDate, + }, + translateParameters: { + week_start: 1, + }, + mockRPC: function (route, args) { + if (args.method === 'search_read' && args.model === 'event') { + assert.deepEqual(args.kwargs.domain, [ + ["start","<=","2019-09-15 23:59:59"], + ["stop",">=","2019-09-09 00:00:00"] + ], + 'The domain to search events in should be correct'); + } + return this._super.apply(this, arguments); + } + }); + + assert.strictEqual(calendar.$('.fc-day-header').first().text(), "Mon 9", + "The first day of the week should be Monday the 9th"); + assert.strictEqual(calendar.$('.fc-day-header').last().text(), "Sun 15", + "The last day of the week should be Sunday the 15th"); + + calendar.destroy(); + }); + + QUnit.test('Saturday week start week mode', async function (assert) { + assert.expect(3); + + // 2019-09-12 08:00:00 + var initDate = new Date(2019, 8, 12, 8, 0, 0); + initDate = new Date(initDate.getTime() - initDate.getTimezoneOffset()*60*1000); + + var calendar = await createCalendarView({ + View: CalendarView, + model: 'event', + data: this.data, + arch: + ''+ + '', + archs: archs, + + viewOptions: { + initialDate: initDate, + }, + translateParameters: { + week_start: 6, + }, + mockRPC: function (route, args) { + if (args.method === 'search_read' && args.model === 'event') { + assert.deepEqual(args.kwargs.domain, [ + ["start","<=","2019-09-13 23:59:59"], + ["stop",">=","2019-09-07 00:00:00"] + ], + 'The domain to search events in should be correct'); + } + return this._super.apply(this, arguments); + } + }); + + assert.strictEqual(calendar.$('.fc-day-header').first().text(), "Sat 7", + "The first day of the week should be Saturday the 7th"); + assert.strictEqual(calendar.$('.fc-day-header').last().text(), "Fri 13", + "The last day of the week should be Friday the 13th"); + + calendar.destroy(); + }); }); });