From 91d3299280a57a6780c446c21a506f6259552c0a Mon Sep 17 00:00:00 2001 From: Dylan Verheul Date: Wed, 30 Nov 2016 12:32:47 +0100 Subject: [PATCH] Fix date and datetime export to JSON in Python versions with a json package Python without a json package will use omnijson and fail on date and datetime objects. Added unit tests. --- .gitignore | 3 +++ tablib/formats/_json.py | 6 ++++-- test_tablib.py | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index acf2d9b..379075b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ junit-py27.xml # tox noise .tox + +# pyenv noise +.python-version diff --git a/tablib/formats/_json.py b/tablib/formats/_json.py index 777040a..fb66745 100644 --- a/tablib/formats/_json.py +++ b/tablib/formats/_json.py @@ -5,8 +5,10 @@ import tablib -import sys -from tablib.packages import omnijson as json +try: + import json +except ImportError: + from tablib.packages import omnijson as json title = 'json' diff --git a/test_tablib.py b/test_tablib.py index be41ee7..6aa4be4 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -6,6 +6,9 @@ import json import unittest import sys import os + +import datetime + import tablib from tablib.compat import markup, unicode, is_py3 from tablib.core import Row @@ -381,6 +384,26 @@ class TablibTestCase(unittest.TestCase): data.html data.latex + def test_datetime_append(self): + """Passes in a single datetime and a single date and exports.""" + + new_row = ( + datetime.datetime.now(), + datetime.datetime.today(), + ) + + data.append(new_row) + + data.json + data.yaml + data.csv + data.tsv + data.xls + data.xlsx + data.ods + data.html + data.latex + def test_book_export_no_exceptions(self): """Test that various exports don't error out."""