Merge lp:~camptocamp/report-print-send/pingen-utc-fix-1086393 into lp:~report-print-send-core-editors/report-print-send/6.1

Proposed by Guewen Baconnier @ Camptocamp
Status: Merged
Merged at revision: 6
Proposed branch: lp:~camptocamp/report-print-send/pingen-utc-fix-1086393
Merge into: lp:~report-print-send-core-editors/report-print-send/6.1
Diff against target: 85 lines (+29/-4)
2 files modified
pingen/pingen.py (+19/-0)
pingen/pingen_document.py (+10/-4)
To merge this branch: bzr merge lp:~camptocamp/report-print-send/pingen-utc-fix-1086393
Reviewer Review Type Date Requested Status
Nicolas Bessi - Camptocamp (community) Approve
Alexandre Fayolle - camptocamp code review, no test Approve
Review via email: mp+138934@code.launchpad.net

Description of the change

Fix for the bug lp:1086393

Convert the pingen's datetime to UTC datetimes.

To post a comment you must log in.
Revision history for this message
Alexandre Fayolle - camptocamp (alexandre-fayolle-c2c) wrote :

LGTM

review: Approve (code review, no test)
Revision history for this message
Nicolas Bessi - Camptocamp (nbessi-c2c-deactivatedaccount) wrote :

LGTM too, merged

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pingen/pingen.py'
2--- pingen/pingen.py 2012-12-07 08:35:13 +0000
3+++ pingen/pingen.py 2012-12-10 11:16:22 +0000
4@@ -23,7 +23,9 @@
5 import logging
6 import urlparse
7 import json
8+import pytz
9
10+from datetime import datetime
11 from requests.packages.urllib3.filepost import encode_multipart_formdata
12
13 _logger = logging.getLogger(__name__)
14@@ -37,6 +39,23 @@
15 400: 'Sending cancelled',
16 }
17
18+DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S' # this is the format used by pingen API
19+
20+TZ = pytz.timezone('Europe/Zurich') # this is the timezone of the pingen API
21+
22+
23+def pingen_datetime_to_utc(dt):
24+ """ Convert a date/time used by pingen.com to UTC timezone
25+
26+ :param dt: pingen date/time as string (as received from the API)
27+ to convert to UTC
28+ :return: datetime in the UTC timezone
29+ """
30+ utc = pytz.utc
31+ dt = datetime.strptime(dt, DATETIME_FORMAT)
32+ localized_dt = TZ.localize(dt, is_dst=True)
33+ return localized_dt.astimezone(utc)
34+
35
36 class PingenException(RuntimeError):
37 """There was an ambiguous exception that occurred while handling your
38
39=== modified file 'pingen/pingen_document.py'
40--- pingen/pingen_document.py 2012-12-07 08:35:13 +0000
41+++ pingen/pingen_document.py 2012-12-10 11:16:22 +0000
42@@ -26,8 +26,9 @@
43 from contextlib import closing
44 from openerp.osv import osv, orm, fields
45 from openerp.tools.translate import _
46-from openerp import pooler
47-from .pingen import APIError, ConnectionError, POST_SENDING_STATUS
48+from openerp import pooler, tools
49+from .pingen import APIError, ConnectionError, POST_SENDING_STATUS, \
50+ pingen_datetime_to_utc
51
52 _logger = logging.getLogger(__name__)
53
54@@ -135,10 +136,12 @@
55 state = 'pingen_error'
56 error = _('The document does not meet the Pingen requirements.')
57
58+ push_date = pingen_datetime_to_utc(infos['date'])
59+
60 document.write(
61 {'last_error_message': error,
62 'state': state,
63- 'push_date': infos['date'],
64+ 'push_date': push_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
65 'pingen_id': doc_id,
66 'post_id': post_id},
67 context=context)
68@@ -336,13 +339,16 @@
69 cr, uid, [('name', '=', post_infos['currency'])], context=context)
70 country_ids = self.pool.get('res.country').search(
71 cr, uid, [('code', '=', post_infos['country'])], context=context)
72+
73+ send_date = pingen_datetime_to_utc(infos['date'])
74+
75 vals = {
76 'post_status': POST_SENDING_STATUS[post_infos['status']],
77 'cost': post_infos['cost'],
78 'currency_id': currency_ids[0] if currency_ids else False,
79 'parsed_address': post_infos['address'],
80 'country_id': country_ids[0] if country_ids else False,
81- 'send_date': post_infos['date'],
82+ 'send_date': send_date.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
83 'pages': post_infos['pages'],
84 'last_error_message': False,
85 }

Subscribers

People subscribed via source and target branches