[FIX] pingen bug lp:1086393

This commit is contained in:
gbaconnier-c2c
2012-12-28 15:06:44 +01:00
committed by unknown
2 changed files with 29 additions and 4 deletions

View File

@@ -23,7 +23,9 @@ import requests
import logging import logging
import urlparse import urlparse
import json import json
import pytz
from datetime import datetime
from requests.packages.urllib3.filepost import encode_multipart_formdata from requests.packages.urllib3.filepost import encode_multipart_formdata
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@@ -37,6 +39,23 @@ POST_SENDING_STATUS = {
400: 'Sending cancelled', 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): class PingenException(RuntimeError):
"""There was an ambiguous exception that occurred while handling your """There was an ambiguous exception that occurred while handling your

View File

@@ -26,8 +26,9 @@ from cStringIO import StringIO
from contextlib import closing from contextlib import closing
from openerp.osv import osv, orm, fields from openerp.osv import osv, orm, fields
from openerp.tools.translate import _ from openerp.tools.translate import _
from openerp import pooler from openerp import pooler, tools
from .pingen import APIError, ConnectionError, POST_SENDING_STATUS from .pingen import APIError, ConnectionError, POST_SENDING_STATUS, \
pingen_datetime_to_utc
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@@ -135,10 +136,12 @@ class pingen_document(orm.Model):
state = 'pingen_error' state = 'pingen_error'
error = _('The document does not meet the Pingen requirements.') error = _('The document does not meet the Pingen requirements.')
push_date = pingen_datetime_to_utc(infos['date'])
document.write( document.write(
{'last_error_message': error, {'last_error_message': error,
'state': state, 'state': state,
'push_date': infos['date'], 'push_date': push_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
'pingen_id': doc_id, 'pingen_id': doc_id,
'post_id': post_id}, 'post_id': post_id},
context=context) context=context)
@@ -336,13 +339,16 @@ class pingen_document(orm.Model):
cr, uid, [('name', '=', post_infos['currency'])], context=context) cr, uid, [('name', '=', post_infos['currency'])], context=context)
country_ids = self.pool.get('res.country').search( country_ids = self.pool.get('res.country').search(
cr, uid, [('code', '=', post_infos['country'])], context=context) cr, uid, [('code', '=', post_infos['country'])], context=context)
send_date = pingen_datetime_to_utc(infos['date'])
vals = { vals = {
'post_status': POST_SENDING_STATUS[post_infos['status']], 'post_status': POST_SENDING_STATUS[post_infos['status']],
'cost': post_infos['cost'], 'cost': post_infos['cost'],
'currency_id': currency_ids[0] if currency_ids else False, 'currency_id': currency_ids[0] if currency_ids else False,
'parsed_address': post_infos['address'], 'parsed_address': post_infos['address'],
'country_id': country_ids[0] if country_ids else False, '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'], 'pages': post_infos['pages'],
'last_error_message': False, 'last_error_message': False,
} }