mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-14 21:56:54 +03:00
Merge pull request #701 from graphql-python/feature/date-improvements
Datetime improvements
This commit is contained in:
commit
d8aacc3b9e
|
@ -11,6 +11,7 @@ from .types import (
|
||||||
Schema,
|
Schema,
|
||||||
Scalar,
|
Scalar,
|
||||||
String, ID, Int, Float, Boolean,
|
String, ID, Int, Float, Boolean,
|
||||||
|
Date, DateTime, Time,
|
||||||
JSONString,
|
JSONString,
|
||||||
UUID,
|
UUID,
|
||||||
List, NonNull,
|
List, NonNull,
|
||||||
|
@ -54,6 +55,9 @@ __all__ = [
|
||||||
'Float',
|
'Float',
|
||||||
'Enum',
|
'Enum',
|
||||||
'Boolean',
|
'Boolean',
|
||||||
|
'Date',
|
||||||
|
'DateTime',
|
||||||
|
'Time',
|
||||||
'JSONString',
|
'JSONString',
|
||||||
'UUID',
|
'UUID',
|
||||||
'List',
|
'List',
|
||||||
|
|
|
@ -5,6 +5,7 @@ from .objecttype import ObjectType
|
||||||
from .interface import Interface
|
from .interface import Interface
|
||||||
from .mutation import Mutation
|
from .mutation import Mutation
|
||||||
from .scalars import Scalar, String, ID, Int, Float, Boolean
|
from .scalars import Scalar, String, ID, Int, Float, Boolean
|
||||||
|
from .datetime import Date, DateTime, Time
|
||||||
from .json import JSONString
|
from .json import JSONString
|
||||||
from .uuid import UUID
|
from .uuid import UUID
|
||||||
from .schema import Schema
|
from .schema import Schema
|
||||||
|
@ -36,6 +37,9 @@ __all__ = [
|
||||||
'ID',
|
'ID',
|
||||||
'Int',
|
'Int',
|
||||||
'Float',
|
'Float',
|
||||||
|
'Date',
|
||||||
|
'DateTime',
|
||||||
|
'Time',
|
||||||
'JSONString',
|
'JSONString',
|
||||||
'UUID',
|
'UUID',
|
||||||
'Boolean',
|
'Boolean',
|
||||||
|
|
|
@ -3,17 +3,10 @@ from __future__ import absolute_import
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from graphql.language import ast
|
from graphql.language import ast
|
||||||
|
from aniso8601 import parse_datetime, parse_date, parse_time
|
||||||
|
|
||||||
from .scalars import Scalar
|
from .scalars import Scalar
|
||||||
|
|
||||||
try:
|
|
||||||
import iso8601
|
|
||||||
except ImportError:
|
|
||||||
raise ImportError(
|
|
||||||
"iso8601 package is required for DateTime Scalar.\n"
|
|
||||||
"You can install it using: pip install iso8601."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Date(Scalar):
|
class Date(Scalar):
|
||||||
'''
|
'''
|
||||||
|
@ -39,8 +32,8 @@ class Date(Scalar):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_value(value):
|
def parse_value(value):
|
||||||
try:
|
try:
|
||||||
return iso8601.parse_date(value).date()
|
return parse_date(value)
|
||||||
except iso8601.ParseError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,8 +59,8 @@ class DateTime(Scalar):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_value(value):
|
def parse_value(value):
|
||||||
try:
|
try:
|
||||||
return iso8601.parse_date(value)
|
return parse_datetime(value)
|
||||||
except iso8601.ParseError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,7 +70,6 @@ class Time(Scalar):
|
||||||
specified by
|
specified by
|
||||||
[iso8601](https://en.wikipedia.org/wiki/ISO_8601).
|
[iso8601](https://en.wikipedia.org/wiki/ISO_8601).
|
||||||
'''
|
'''
|
||||||
epoch_date = '1970-01-01'
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def serialize(time):
|
def serialize(time):
|
||||||
|
@ -94,7 +86,6 @@ class Time(Scalar):
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse_value(cls, value):
|
def parse_value(cls, value):
|
||||||
try:
|
try:
|
||||||
dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value))
|
return parse_time(value)
|
||||||
return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo)
|
except ValueError:
|
||||||
except iso8601.ParseError:
|
|
||||||
return None
|
return None
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -90,6 +90,7 @@ setup(
|
||||||
'graphql-core>=2.0,<3',
|
'graphql-core>=2.0,<3',
|
||||||
'graphql-relay>=0.4.5,<1',
|
'graphql-relay>=0.4.5,<1',
|
||||||
'promise>=2.1,<3',
|
'promise>=2.1,<3',
|
||||||
|
'aniso8601>=3,<4',
|
||||||
],
|
],
|
||||||
tests_require=tests_require,
|
tests_require=tests_require,
|
||||||
extras_require={
|
extras_require={
|
||||||
|
|
Loading…
Reference in New Issue
Block a user