Merge pull request #630 from xmo-odoo/master-website-save-datetime-xmo
website: missing timezone conversion back to UTC on save
This commit is contained in:
commit
81c174b740
|
@ -15,6 +15,7 @@ import urllib2
|
||||||
import urlparse
|
import urlparse
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
import pytz
|
||||||
import werkzeug.urls
|
import werkzeug.urls
|
||||||
import werkzeug.utils
|
import werkzeug.utils
|
||||||
from dateutil import parser
|
from dateutil import parser
|
||||||
|
@ -153,15 +154,15 @@ class DateTime(orm.AbstractModel):
|
||||||
def attributes(self, cr, uid, field_name, record, options,
|
def attributes(self, cr, uid, field_name, record, options,
|
||||||
source_element, g_att, t_att, qweb_context,
|
source_element, g_att, t_att, qweb_context,
|
||||||
context=None):
|
context=None):
|
||||||
column = record._model._all_columns[field_name].column
|
|
||||||
value = record[field_name]
|
value = record[field_name]
|
||||||
if isinstance(value, basestring):
|
if isinstance(value, basestring):
|
||||||
value = datetime.datetime.strptime(
|
value = datetime.datetime.strptime(
|
||||||
value, DEFAULT_SERVER_DATETIME_FORMAT)
|
value, DEFAULT_SERVER_DATETIME_FORMAT)
|
||||||
if value:
|
if value:
|
||||||
|
# convert from UTC (server timezone) to user timezone
|
||||||
value = fields.datetime.context_timestamp(
|
value = fields.datetime.context_timestamp(
|
||||||
cr, uid, timestamp=value, context=context)
|
cr, uid, timestamp=value, context=context)
|
||||||
value = value.strftime(openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT)
|
value = value.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
|
||||||
|
|
||||||
attrs = super(DateTime, self).attributes(
|
attrs = super(DateTime, self).attributes(
|
||||||
cr, uid, field_name, record, options, source_element, g_att, t_att,
|
cr, uid, field_name, record, options, source_element, g_att, t_att,
|
||||||
|
@ -171,11 +172,31 @@ class DateTime(orm.AbstractModel):
|
||||||
])
|
])
|
||||||
|
|
||||||
def from_html(self, cr, uid, model, column, element, context=None):
|
def from_html(self, cr, uid, model, column, element, context=None):
|
||||||
|
if context is None: context = {}
|
||||||
value = element.text_content().strip()
|
value = element.text_content().strip()
|
||||||
if not value: return False
|
if not value: return False
|
||||||
|
|
||||||
datetime.datetime.strptime(value, DEFAULT_SERVER_DATETIME_FORMAT)
|
# parse from string to datetime
|
||||||
return value
|
dt = datetime.datetime.strptime(value, DEFAULT_SERVER_DATETIME_FORMAT)
|
||||||
|
|
||||||
|
# convert back from user's timezone to UTC
|
||||||
|
tz_name = context.get('tz') \
|
||||||
|
or self.pool['res.users'].read(cr, openerp.SUPERUSER_ID, uid, ['tz'], context=context)['tz']
|
||||||
|
if tz_name:
|
||||||
|
try:
|
||||||
|
user_tz = pytz.timezone(tz_name)
|
||||||
|
utc = pytz.utc
|
||||||
|
|
||||||
|
dt = user_tz.localize(dt).astimezone(utc)
|
||||||
|
except Exception:
|
||||||
|
logger.warn(
|
||||||
|
"Failed to convert the value for a field of the model"
|
||||||
|
" %s back from the user's timezone (%s) to UTC",
|
||||||
|
model, tz_name,
|
||||||
|
exc_info=True)
|
||||||
|
|
||||||
|
# format back to string
|
||||||
|
return dt.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
|
||||||
|
|
||||||
class Text(orm.AbstractModel):
|
class Text(orm.AbstractModel):
|
||||||
_name = 'website.qweb.field.text'
|
_name = 'website.qweb.field.text'
|
||||||
|
|
|
@ -422,7 +422,7 @@ class datetime(_column):
|
||||||
tz_name = registry.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz']
|
tz_name = registry.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz']
|
||||||
if tz_name:
|
if tz_name:
|
||||||
try:
|
try:
|
||||||
utc = pytz.timezone('UTC')
|
utc = pytz.utc
|
||||||
context_tz = pytz.timezone(tz_name)
|
context_tz = pytz.timezone(tz_name)
|
||||||
utc_timestamp = utc.localize(timestamp, is_dst=False) # UTC = no DST
|
utc_timestamp = utc.localize(timestamp, is_dst=False) # UTC = no DST
|
||||||
return utc_timestamp.astimezone(context_tz)
|
return utc_timestamp.astimezone(context_tz)
|
||||||
|
|
Loading…
Reference in New Issue