From 8e3b9b430e4d07d166217ce7e6d87d63a5d19b55 Mon Sep 17 00:00:00 2001 From: Jon Dugan Date: Fri, 29 Apr 2016 13:58:49 -0500 Subject: [PATCH 1/5] improve iso8601 handling --- graphene/core/types/custom_scalars.py | 6 ++--- .../core/types/tests/test_custom_scalars.py | 27 +++++++++++++++++++ setup.py | 1 + 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 graphene/core/types/tests/test_custom_scalars.py diff --git a/graphene/core/types/custom_scalars.py b/graphene/core/types/custom_scalars.py index 72f0f8b0..1fa70b22 100644 --- a/graphene/core/types/custom_scalars.py +++ b/graphene/core/types/custom_scalars.py @@ -1,5 +1,6 @@ import datetime import json +import iso8601 from graphql.core.language import ast @@ -33,9 +34,8 @@ class DateTime(Scalar): @staticmethod def parse_literal(node): if isinstance(node, ast.StringValue): - return datetime.datetime.strptime( - node.value, "%Y-%m-%dT%H:%M:%S.%f") + return iso8601.parse_date(node.value) @staticmethod def parse_value(value): - return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f") + return iso8601.parse_date(value) diff --git a/graphene/core/types/tests/test_custom_scalars.py b/graphene/core/types/tests/test_custom_scalars.py new file mode 100644 index 00000000..5aed2d39 --- /dev/null +++ b/graphene/core/types/tests/test_custom_scalars.py @@ -0,0 +1,27 @@ +import iso8601 + +from graphql.core.language.ast import StringValue + +from ..custom_scalars import DateTime + +def test_date_time(): + test_iso_string = "2016-04-29T18:34:12.502Z" + + def check_datetime(test_dt): + assert test_dt.tzinfo == iso8601.UTC + assert test_dt.year == 2016 + assert test_dt.month == 04 + assert test_dt.day == 29 + assert test_dt.hour == 18 + assert test_dt.minute == 34 + assert test_dt.second == 12 + + test_dt = DateTime().parse_value(test_iso_string) + check_datetime(test_dt) + + assert DateTime.serialize(test_dt) == "2016-04-29T18:34:12.502000+00:00" + + node = StringValue(test_iso_string) + test_dt = DateTime.parse_literal(node) + check_datetime(test_dt) + diff --git a/setup.py b/setup.py index 93a08561..4b74a30e 100644 --- a/setup.py +++ b/setup.py @@ -57,6 +57,7 @@ setup( 'six>=1.10.0', 'graphql-core>=0.4.9', 'graphql-relay==0.3.3', + 'iso8601==0.1.11', ], tests_require=[ 'django-filter>=0.10.0', From a158f4e00261003c556da6729c2536d21407014b Mon Sep 17 00:00:00 2001 From: Jon Dugan Date: Fri, 29 Apr 2016 14:10:28 -0500 Subject: [PATCH 2/5] liniting fixes --- graphene/core/types/custom_scalars.py | 1 - graphene/core/types/tests/test_custom_scalars.py | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/graphene/core/types/custom_scalars.py b/graphene/core/types/custom_scalars.py index 1fa70b22..2dad3c64 100644 --- a/graphene/core/types/custom_scalars.py +++ b/graphene/core/types/custom_scalars.py @@ -1,4 +1,3 @@ -import datetime import json import iso8601 diff --git a/graphene/core/types/tests/test_custom_scalars.py b/graphene/core/types/tests/test_custom_scalars.py index 5aed2d39..ee09447d 100644 --- a/graphene/core/types/tests/test_custom_scalars.py +++ b/graphene/core/types/tests/test_custom_scalars.py @@ -4,9 +4,11 @@ from graphql.core.language.ast import StringValue from ..custom_scalars import DateTime + def test_date_time(): test_iso_string = "2016-04-29T18:34:12.502Z" + def check_datetime(test_dt): assert test_dt.tzinfo == iso8601.UTC assert test_dt.year == 2016 @@ -24,4 +26,3 @@ def test_date_time(): node = StringValue(test_iso_string) test_dt = DateTime.parse_literal(node) check_datetime(test_dt) - From f325964a9e35284b2046ff6b92a6bc994b2c4f4a Mon Sep 17 00:00:00 2001 From: Jon Dugan Date: Fri, 29 Apr 2016 14:16:31 -0500 Subject: [PATCH 3/5] one more linting fix --- graphene/core/types/tests/test_custom_scalars.py | 1 - 1 file changed, 1 deletion(-) diff --git a/graphene/core/types/tests/test_custom_scalars.py b/graphene/core/types/tests/test_custom_scalars.py index ee09447d..b16c00f5 100644 --- a/graphene/core/types/tests/test_custom_scalars.py +++ b/graphene/core/types/tests/test_custom_scalars.py @@ -8,7 +8,6 @@ from ..custom_scalars import DateTime def test_date_time(): test_iso_string = "2016-04-29T18:34:12.502Z" - def check_datetime(test_dt): assert test_dt.tzinfo == iso8601.UTC assert test_dt.year == 2016 From 9c2d571aae0e14cd5d89ae48659dbbc415a02fe8 Mon Sep 17 00:00:00 2001 From: Jon Dugan Date: Fri, 29 Apr 2016 14:21:18 -0500 Subject: [PATCH 4/5] py 3.4/3.5 don't like the leading 0 --- graphene/core/types/tests/test_custom_scalars.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene/core/types/tests/test_custom_scalars.py b/graphene/core/types/tests/test_custom_scalars.py index b16c00f5..49c9a9c7 100644 --- a/graphene/core/types/tests/test_custom_scalars.py +++ b/graphene/core/types/tests/test_custom_scalars.py @@ -11,7 +11,7 @@ def test_date_time(): def check_datetime(test_dt): assert test_dt.tzinfo == iso8601.UTC assert test_dt.year == 2016 - assert test_dt.month == 04 + assert test_dt.month == 4 assert test_dt.day == 29 assert test_dt.hour == 18 assert test_dt.minute == 34 From aea560e3635fb5a92f1aa3bde59353f6a5e94e1d Mon Sep 17 00:00:00 2001 From: Jon Dugan Date: Fri, 29 Apr 2016 15:31:57 -0500 Subject: [PATCH 5/5] don't restrict version number for the iso8601 package --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 4b74a30e..d0476511 100644 --- a/setup.py +++ b/setup.py @@ -57,7 +57,7 @@ setup( 'six>=1.10.0', 'graphql-core>=0.4.9', 'graphql-relay==0.3.3', - 'iso8601==0.1.11', + 'iso8601', ], tests_require=[ 'django-filter>=0.10.0',