[IMP] Handle timezones
parent
0286a865f3
commit
478ff1afba
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from pytz import timezone, utc
|
||||||
from mako.template import Template
|
from mako.template import Template
|
||||||
from openerp import _, api, exceptions, fields, models, tools
|
from openerp import _, api, exceptions, fields, models, tools
|
||||||
from openerp.tools.safe_eval import safe_eval
|
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_start = line.date_start_field_id.name
|
||||||
f_date_stop = line.date_stop_field_id.name
|
f_date_stop = line.date_stop_field_id.name
|
||||||
f_duration = line.duration_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'):
|
cur_rec[f_user]._model._name != 'res.users'):
|
||||||
raise exceptions.ValidationError(
|
raise exceptions.ValidationError(
|
||||||
_("The 'User' field of record %s (%s) "
|
_("The 'User' field of record %s (%s) "
|
||||||
"does not refer to res.users")
|
"does not refer to res.users")
|
||||||
% (cur_rec[f_descr], line.name.model))
|
% (cur_rec[f_descr], line.name.model))
|
||||||
|
|
||||||
if (((f_descr and cur_rec[f_descr]) or
|
if ((cur_rec[f_descr] or line.description_code) and
|
||||||
line.description_code) and
|
|
||||||
cur_rec[f_date_start]):
|
cur_rec[f_date_start]):
|
||||||
duration = False
|
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
|
if (not line.duration_field_id and
|
||||||
line.date_stop_field_id and
|
line.date_stop_field_id and
|
||||||
cur_rec[f_date_start] and
|
cur_rec[f_date_start] and
|
||||||
cur_rec[f_date_stop]):
|
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':
|
if line.date_stop_field_id.ttype == 'date':
|
||||||
date_format = tools.DEFAULT_SERVER_DATE_FORMAT
|
date_format = tools.DEFAULT_SERVER_DATE_FORMAT
|
||||||
else:
|
else:
|
||||||
|
@ -135,8 +138,10 @@ class SuperCalendarConfigurator(models.Model):
|
||||||
)
|
)
|
||||||
date_diff = (date_stop - date_start)
|
date_diff = (date_stop - date_start)
|
||||||
duration = date_diff.total_seconds() / 3600
|
duration = date_diff.total_seconds() / 3600
|
||||||
|
|
||||||
elif line.duration_field_id:
|
elif line.duration_field_id:
|
||||||
duration = cur_rec[f_duration]
|
duration = cur_rec[f_duration]
|
||||||
|
|
||||||
if line.description_type != 'code':
|
if line.description_type != 'code':
|
||||||
name = cur_rec[f_descr]
|
name = cur_rec[f_descr]
|
||||||
else:
|
else:
|
||||||
|
@ -144,9 +149,22 @@ class SuperCalendarConfigurator(models.Model):
|
||||||
mytemplate = Template(line.description_code)
|
mytemplate = Template(line.description_code)
|
||||||
name = mytemplate.render(**parse_dict)
|
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 = {
|
super_calendar_values = {
|
||||||
'name': name,
|
'name': name,
|
||||||
'date_start': cur_rec[f_date_start],
|
'date_start': date_start,
|
||||||
'duration': duration,
|
'duration': duration,
|
||||||
'user_id': (f_user and cur_rec[f_user].id),
|
'user_id': (f_user and cur_rec[f_user].id),
|
||||||
'configurator_id': self.id,
|
'configurator_id': self.id,
|
||||||
|
|
Loading…
Reference in New Issue