Skip to content

Commit

Permalink
Added DateTime custom Scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Apr 3, 2016
1 parent 81560df commit 12c69c0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
8 changes: 6 additions & 2 deletions graphene/contrib/django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ...core.types.definitions import List
from ...core.types.scalars import ID, Boolean, Float, Int, String
from ...core.types.custom_scalars import JSONString
from ...core.types.custom_scalars import JSONString, DateTime
from ...core.classtypes.enum import Enum
from .compat import RelatedObject, UUIDField, ArrayField, HStoreField, JSONField, RangeField
from .utils import get_related_model, import_single_dispatch
Expand All @@ -26,7 +26,6 @@ def convert_django_field(field):
(field, field.__class__))


@convert_django_field.register(models.DateField)
@convert_django_field.register(models.CharField)
@convert_django_field.register(models.TextField)
@convert_django_field.register(models.EmailField)
Expand Down Expand Up @@ -69,6 +68,11 @@ def convert_field_to_float(field):
return Float(description=field.help_text)


@convert_django_field.register(models.DateField)
def convert_date_to_string(field):
return DateTime(description=field.help_text)


@convert_django_field.register(models.ManyToManyField)
@convert_django_field.register(models.ManyToOneRel)
@convert_django_field.register(models.ManyToManyRel)
Expand Down
4 changes: 2 additions & 2 deletions graphene/contrib/django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..fields import (ConnectionOrListField,
DjangoModelField)
from ..compat import MissingType, ArrayField, HStoreField, JSONField, RangeField
from graphene.core.types.custom_scalars import JSONString
from graphene.core.types.custom_scalars import JSONString, DateTime

from .models import Article, Reporter

Expand All @@ -29,7 +29,7 @@ def test_should_unknown_django_field_raise_exception():


def test_should_date_convert_string():
assert_conversion(models.DateField, graphene.String)
assert_conversion(models.DateField, DateTime)


def test_should_char_convert_string():
Expand Down
7 changes: 5 additions & 2 deletions graphene/contrib/django/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import pytest
from py.test import raises
from django.db import models
Expand Down Expand Up @@ -134,7 +135,7 @@ class Meta:

@classmethod
def get_node(cls, id, info):
return ArticleNode(Article(id=1, headline='Article node'))
return ArticleNode(Article(id=1, headline='Article node', pub_date=datetime.date(2002, 3, 11)))

class Query(graphene.ObjectType):
node = relay.NodeField()
Expand Down Expand Up @@ -167,6 +168,7 @@ def resolve_reporter(self, *args, **kwargs):
}
... on ArticleNode {
headline
pubDate
}
}
}
Expand All @@ -187,7 +189,8 @@ def resolve_reporter(self, *args, **kwargs):
},
'myArticle': {
'id': 'QXJ0aWNsZU5vZGU6MQ==',
'headline': 'Article node'
'headline': 'Article node',
'pubDate': '2002-03-11',
}
}
schema = graphene.Schema(query=Query)
Expand Down
19 changes: 19 additions & 0 deletions graphene/core/types/custom_scalars.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import datetime

from graphql.core.language import ast
from ...core.classtypes.scalar import Scalar
Expand All @@ -19,3 +20,21 @@ def parse_literal(node):
@staticmethod
def parse_value(value):
return json.dumps(value)


class DateTime(Scalar):
'''DateTime in ISO 8601 format'''

@staticmethod
def serialize(dt):
return dt.isoformat()

@staticmethod
def parse_literal(node):
if isinstance(node, ast.StringValue):
return datetime.datetime.strptime(
node.value, "%Y-%m-%dT%H:%M:%S.%f")

@staticmethod
def parse_value(value):
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")

0 comments on commit 12c69c0

Please sign in to comment.