From f4c1f5b912f6db2acda33aca295bcaf457c09474 Mon Sep 17 00:00:00 2001 From: "Guewen Baconnier @ Camptocamp" Date: Mon, 10 Dec 2012 12:14:01 +0100 Subject: [PATCH] [FIX] pingen: convert datetimes to UTC before storing them --- pingen/pingen.py | 19 +++++++++++++++++++ pingen/pingen_document.py | 14 ++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pingen/pingen.py b/pingen/pingen.py index b460618..ecb8c2e 100644 --- a/pingen/pingen.py +++ b/pingen/pingen.py @@ -23,7 +23,9 @@ import requests import logging import urlparse import json +import pytz +from datetime import datetime from requests.packages.urllib3.filepost import encode_multipart_formdata _logger = logging.getLogger(__name__) @@ -37,6 +39,23 @@ POST_SENDING_STATUS = { 400: 'Sending cancelled', } +DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S' # this is the format used by pingen API + +TZ = pytz.timezone('Europe/Zurich') # this is the timezone of the pingen API + + +def pingen_datetime_to_utc(dt): + """ Convert a date/time used by pingen.com to UTC timezone + + :param dt: pingen date/time as string (as received from the API) + to convert to UTC + :return: datetime in the UTC timezone + """ + utc = pytz.utc + dt = datetime.strptime(dt, DATETIME_FORMAT) + localized_dt = TZ.localize(dt, is_dst=True) + return localized_dt.astimezone(utc) + class PingenException(RuntimeError): """There was an ambiguous exception that occurred while handling your diff --git a/pingen/pingen_document.py b/pingen/pingen_document.py index 522b2c2..b613327 100644 --- a/pingen/pingen_document.py +++ b/pingen/pingen_document.py @@ -26,8 +26,9 @@ from cStringIO import StringIO from contextlib import closing from openerp.osv import osv, orm, fields from openerp.tools.translate import _ -from openerp import pooler -from .pingen import APIError, ConnectionError, POST_SENDING_STATUS +from openerp import pooler, tools +from .pingen import APIError, ConnectionError, POST_SENDING_STATUS, \ + pingen_datetime_to_utc _logger = logging.getLogger(__name__) @@ -135,10 +136,12 @@ class pingen_document(orm.Model): state = 'pingen_error' error = _('The document does not meet the Pingen requirements.') + push_date = pingen_datetime_to_utc(infos['date']) + document.write( {'last_error_message': error, 'state': state, - 'push_date': infos['date'], + 'push_date': push_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT), 'pingen_id': doc_id, 'post_id': post_id}, context=context) @@ -336,13 +339,16 @@ class pingen_document(orm.Model): cr, uid, [('name', '=', post_infos['currency'])], context=context) country_ids = self.pool.get('res.country').search( cr, uid, [('code', '=', post_infos['country'])], context=context) + + send_date = pingen_datetime_to_utc(infos['date']) + vals = { 'post_status': POST_SENDING_STATUS[post_infos['status']], 'cost': post_infos['cost'], 'currency_id': currency_ids[0] if currency_ids else False, 'parsed_address': post_infos['address'], 'country_id': country_ids[0] if country_ids else False, - 'send_date': post_infos['date'], + 'send_date': send_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT), 'pages': post_infos['pages'], 'last_error_message': False, }