From 9b6b6ce80ede4ec329c1c71c97454e9310b35c49 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 7 Apr 2020 20:20:33 +0200 Subject: [PATCH] toulouse_axel: handle timezone on transaction_date (#41325) --- passerelle/contrib/toulouse_axel/models.py | 5 ++++- passerelle/contrib/toulouse_axel/utils.py | 13 ++++++++++--- tests/test_toulouse_axel_utils.py | 15 ++++++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/passerelle/contrib/toulouse_axel/models.py b/passerelle/contrib/toulouse_axel/models.py index 6b5b23e6..53f34c07 100644 --- a/passerelle/contrib/toulouse_axel/models.py +++ b/passerelle/contrib/toulouse_axel/models.py @@ -996,7 +996,10 @@ class ToulouseAxel(BaseResource): transaction_amount = invoice['amount'] transaction_id = data['transaction_id'] - transaction_date = utils.encode_datetime(data['transaction_date']) + transaction_date = utils.parse_datetime(data['transaction_date']) + if transaction_date is None: + raise APIError('invalid transaction_date') + transaction_date = utils.encode_datetime(transaction_date) post_data = { 'IDFACTURE': int(invoice_id), 'IDREGIEENCAISSEMENT': '', diff --git a/passerelle/contrib/toulouse_axel/utils.py b/passerelle/contrib/toulouse_axel/utils.py index b108e615..7bfb8fee 100644 --- a/passerelle/contrib/toulouse_axel/utils.py +++ b/passerelle/contrib/toulouse_axel/utils.py @@ -22,6 +22,8 @@ import datetime import unicodedata import xml.etree.ElementTree as ET +import pytz + from django.utils.six import string_types @@ -135,11 +137,16 @@ def encode_bool(obj): return obj -def encode_datetime(obj): +def parse_datetime(value): try: - return datetime.datetime.strptime(obj, json_datetime_format).strftime(xml_datetime_format) + dt = datetime.datetime.strptime(value, json_datetime_format) except ValueError: - return obj + return None + return pytz.utc.localize(dt) + + +def encode_datetime(dt): + return dt.astimezone(pytz.timezone('Europe/Paris')).strftime(xml_datetime_format) def upperize(data): diff --git a/tests/test_toulouse_axel_utils.py b/tests/test_toulouse_axel_utils.py index 80ca24b0..e14ebb7d 100644 --- a/tests/test_toulouse_axel_utils.py +++ b/tests/test_toulouse_axel_utils.py @@ -18,6 +18,7 @@ import datetime import pytest from passerelle.contrib.toulouse_axel.utils import ( + parse_datetime, encode_datetime, get_booking, get_reference_year_from_date, @@ -26,13 +27,17 @@ from passerelle.contrib.toulouse_axel.utils import ( ) -def test_encode_datetime(): +def test_parse_datetime(): # wrong format - assert encode_datetime('foo') == 'foo' - assert encode_datetime('2019-12-12') == '2019-12-12' - assert encode_datetime('2019-12-12T12:01:72') == '2019-12-12T12:01:72' + assert parse_datetime('foo') is None + assert parse_datetime('2019-12-12') is None + assert parse_datetime('2019-12-12T12:01:72') is None # ok - assert encode_datetime('2019-12-12T12:01:42') == '12/12/2019 12:01:42' + assert parse_datetime('2019-12-12T12:01:42').isoformat() == '2019-12-12T12:01:42+00:00' + + +def test_encode_datetime(): + assert encode_datetime(parse_datetime('2019-12-12T23:40:42')) == '13/12/2019 00:40:42' @pytest.mark.parametrize('value, expected', [ -- 2.24.0