[FIX] hr_holidays: default date should be local to the user's timezone

Before this the following happened:

- Be in PST (UTC -8)
- Create a 1 day leave starting Jan 1 (clicking in the calendar view)

The default request_date_from is Jan 1, default request_date_to is Jan
2.

It happens because the dates are extracted from the UTC time, which in
this case are different (15:00 AM Jan 1 - 3:00 AM Jan 2).

To solve it set dates local to the user.

opw-2169863

X-original-commit: 740f39d31f2480ee7ae9cd3472b733087f1cf280
This commit is contained in:
Joren Van Onder
2020-02-20 01:14:02 +00:00
parent 96eb99f0f2
commit 2e490f1c95
2 changed files with 23 additions and 2 deletions
+2 -2
View File
@@ -92,12 +92,12 @@ class HolidaysRequest(models.Model):
user_tz = self.env.user.tz or 'UTC'
localized_dt = timezone('UTC').localize(values['date_from']).astimezone(timezone(user_tz))
global_from = localized_dt.time().hour == 7 and localized_dt.time().minute == 0
new_values['request_date_from'] = values['date_from'].date()
new_values['request_date_from'] = localized_dt.date()
if values.get('date_to'):
user_tz = self.env.user.tz or 'UTC'
localized_dt = timezone('UTC').localize(values['date_to']).astimezone(timezone(user_tz))
global_to = localized_dt.time().hour == 19 and localized_dt.time().minute == 0
new_values['request_date_to'] = values['date_to'].date()
new_values['request_date_to'] = localized_dt.date()
if global_from and global_to:
new_values['request_unit_custom'] = True
return new_values
@@ -365,3 +365,24 @@ class TestLeaveRequests(TestHrHolidaysCommon):
self.assertEqual(leave1.number_of_hours_display, 24)
leave1.action_validate()
self.assertEqual(leave1.number_of_hours_display, 24)
@mute_logger('odoo.models.unlink', 'odoo.addons.mail.models.mail_mail')
def test_leave_defaults_with_timezones(self):
""" Make sure that leaves start with correct defaults for non-UTC timezones """
self.user_employee.tz = 'US/Pacific'
# Mimic what is done by the calendar widget when clicking on a day. It
# will take the local datetime from 7:00 to 19:00 and then convert it
# to UTC before sending it. Values here are for PST (UTC -8) and
# represent a leave on 2019/1/1 from 7:00 to 19:00 local time.
webclient_values = {
'date_from': datetime(2019, 1, 1, 15, 0, 0),
'date_to': datetime(2019, 1, 2, 3, 0), # note that this is the next day in UTC
}
values = self.env['hr.leave'].sudo(self.user_employee_id)._default_get_request_parameters(webclient_values)
# Dates should be local to the user, they do not and cannot be
# converted using the user's timezone.
first_of_january = date(2019, 1, 1)
self.assertEqual(values['request_date_from'], first_of_january)
self.assertEqual(values['request_date_to'], first_of_january)