From 478ff1afba4500c24aaa78ed3e9420eee1b27e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agathe=20Moll=C3=A9?= Date: Mon, 20 Jul 2015 12:52:40 -0400 Subject: [PATCH] [IMP] Handle timezones --- .../models/super_calendar_configurator.py | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/super_calendar/models/super_calendar_configurator.py b/super_calendar/models/super_calendar_configurator.py index 026584a26..421cde696 100644 --- a/super_calendar/models/super_calendar_configurator.py +++ b/super_calendar/models/super_calendar_configurator.py @@ -28,6 +28,7 @@ import logging from datetime import datetime +from pytz import timezone, utc from mako.template import Template from openerp import _, api, exceptions, fields, models, tools from openerp.tools.safe_eval import safe_eval @@ -103,29 +104,31 @@ class SuperCalendarConfigurator(models.Model): f_date_start = line.date_start_field_id.name f_date_stop = line.date_stop_field_id.name f_duration = line.duration_field_id.name - if (f_user and - cur_rec[f_user] and + + # Check if f_user refer to a res.users + if (f_user and cur_rec[f_user] and cur_rec[f_user]._model._name != 'res.users'): raise exceptions.ValidationError( _("The 'User' field of record %s (%s) " "does not refer to res.users") % (cur_rec[f_descr], line.name.model)) - if (((f_descr and cur_rec[f_descr]) or - line.description_code) and + if ((cur_rec[f_descr] or line.description_code) and cur_rec[f_date_start]): duration = False + + if line.date_start_field_id.ttype == 'date': + date_format = tools.DEFAULT_SERVER_DATE_FORMAT + else: + date_format = tools.DEFAULT_SERVER_DATETIME_FORMAT + date_start = datetime.strptime( + cur_rec[f_date_start], date_format + ) + if (not line.duration_field_id and line.date_stop_field_id and cur_rec[f_date_start] and cur_rec[f_date_stop]): - if line.date_start_field_id.ttype == 'date': - date_format = tools.DEFAULT_SERVER_DATE_FORMAT - else: - date_format = tools.DEFAULT_SERVER_DATETIME_FORMAT - date_start = datetime.strptime( - cur_rec[f_date_start], date_format - ) if line.date_stop_field_id.ttype == 'date': date_format = tools.DEFAULT_SERVER_DATE_FORMAT else: @@ -135,8 +138,10 @@ class SuperCalendarConfigurator(models.Model): ) date_diff = (date_stop - date_start) duration = date_diff.total_seconds() / 3600 + elif line.duration_field_id: duration = cur_rec[f_duration] + if line.description_type != 'code': name = cur_rec[f_descr] else: @@ -144,9 +149,22 @@ class SuperCalendarConfigurator(models.Model): mytemplate = Template(line.description_code) name = mytemplate.render(**parse_dict) + # Convert date_start to UTC timezone if it is a date field + # in order to be stored in UTC in the database + if line.date_start_field_id.ttype == 'date': + tz = timezone(self._context.get('tz') + or self.env.user.tz + or 'UTC') + local_date_start = tz.localize(date_start) + utc_date_start = local_date_start.astimezone(utc) + date_start = datetime.strftime( + utc_date_start, + tools.DEFAULT_SERVER_DATETIME_FORMAT + ) + super_calendar_values = { 'name': name, - 'date_start': cur_rec[f_date_start], + 'date_start': date_start, 'duration': duration, 'user_id': (f_user and cur_rec[f_user].id), 'configurator_id': self.id,