models.DateField corresponds to graphene Date Scalar

This commit is contained in:
= 2017-12-05 15:04:29 -05:00
parent 73706fa6bb
commit 62c0694901
9 changed files with 40 additions and 13 deletions

View File

@ -3,7 +3,7 @@ from django.utils.encoding import force_text
from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List,
NonNull, String, UUID)
from graphene.types.datetime import DateTime, Time
from graphene.types.datetime import DateTime, Date, Time
from graphene.types.json import JSONString
from graphene.utils.str_converters import to_camel_case, to_const
from graphql import assert_valid_name
@ -121,11 +121,16 @@ def convert_field_to_float(field, registry=None):
return Float(description=field.help_text, required=not field.null)
@convert_django_field.register(models.DateField)
@convert_django_field.register(models.DateTimeField)
def convert_date_to_string(field, registry=None):
return DateTime(description=field.help_text, required=not field.null)
@convert_django_field.register(models.DateField)
def convert_date_to_string(field, registry=None):
return Date(description=field.help_text, required=not field.null)
@convert_django_field.register(models.TimeField)
def convert_time_to_string(field, registry=None):
return Time(description=field.help_text, required=not field.null)

View File

@ -157,8 +157,8 @@ def test_filter_shortcut_filterset_context():
r1 = Reporter.objects.create(first_name='r1', last_name='r1', email='r1@test.com')
r2 = Reporter.objects.create(first_name='r2', last_name='r2', email='r2@test.com')
Article.objects.create(headline='a1', pub_date=datetime.now(), reporter=r1, editor=r1)
Article.objects.create(headline='a2', pub_date=datetime.now(), reporter=r2, editor=r2)
Article.objects.create(headline='a1', pub_date=datetime.now(), pub_date_time=datetime.now(), reporter=r1, editor=r1)
Article.objects.create(headline='a2', pub_date=datetime.now(), pub_date_time=datetime.now(), reporter=r2, editor=r2)
class context(object):
reporter = r2
@ -245,8 +245,8 @@ def test_filter_filterset_related_results():
r1 = Reporter.objects.create(first_name='r1', last_name='r1', email='r1@test.com')
r2 = Reporter.objects.create(first_name='r2', last_name='r2', email='r2@test.com')
Article.objects.create(headline='a1', pub_date=datetime.now(), reporter=r1)
Article.objects.create(headline='a2', pub_date=datetime.now(), reporter=r2)
Article.objects.create(headline='a1', pub_date=datetime.now(), pub_date_time=datetime.now(), reporter=r1)
Article.objects.create(headline='a2', pub_date=datetime.now(), pub_date_time=datetime.now(), reporter=r2)
query = '''
query {
@ -464,6 +464,7 @@ def test_should_query_filter_node_limit():
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.now(),
pub_date_time=datetime.now(),
reporter=r,
editor=r,
lang='es'
@ -471,6 +472,7 @@ def test_should_query_filter_node_limit():
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.now(),
pub_date_time=datetime.now(),
reporter=r,
editor=r,
lang='en'

View File

@ -92,11 +92,15 @@ def convert_serializer_field_to_float(field):
@get_graphene_type_from_serializer_field.register(serializers.DateTimeField)
@get_graphene_type_from_serializer_field.register(serializers.DateField)
def convert_serializer_field_to_date_time(field):
return graphene.types.datetime.DateTime
@get_graphene_type_from_serializer_field.register(serializers.DateField)
def convert_serializer_field_to_date_time(field):
return graphene.types.datetime.Date
@get_graphene_type_from_serializer_field.register(serializers.TimeField)
def convert_serializer_field_to_time(field):
return graphene.types.datetime.Time

View File

@ -87,8 +87,8 @@ def test_should_date_time_convert_datetime():
assert_conversion(serializers.DateTimeField, graphene.types.datetime.DateTime)
def test_should_date_convert_datetime():
assert_conversion(serializers.DateField, graphene.types.datetime.DateTime)
def test_should_date_convert_date():
assert_conversion(serializers.DateField, graphene.types.datetime.Date)
def test_should_time_convert_time():

View File

@ -37,6 +37,7 @@ class Reporter(models.Model):
class Article(models.Model):
headline = models.CharField(max_length=100)
pub_date = models.DateField()
pub_date_time = models.DateTimeField()
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE, related_name='articles')
editor = models.ForeignKey(Reporter, on_delete=models.CASCADE, related_name='edited_articles_+')
lang = models.CharField(max_length=2, help_text='Language', choices=[

View File

@ -5,7 +5,7 @@ from py.test import raises
import graphene
from graphene.relay import ConnectionField, Node
from graphene.types.datetime import DateTime, Time
from graphene.types.datetime import DateTime, Date, Time
from graphene.types.json import JSONString
from ..compat import JSONField, ArrayField, HStoreField, RangeField, MissingType
@ -38,9 +38,12 @@ def test_should_unknown_django_field_raise_exception():
convert_django_field(None)
assert 'Don\'t know how to convert the Django field' in str(excinfo.value)
def test_should_date_time_convert_string():
assert_conversion(models.DateTimeField, DateTime)
def test_should_date_convert_string():
assert_conversion(models.DateField, DateTime)
assert_conversion(models.DateField, Date)
def test_should_time_convert_string():

View File

@ -30,6 +30,8 @@ def test_should_date_convert_string():
def test_should_time_convert_string():
assert_conversion(forms.TimeField, graphene.String)
def test_should_date_convert_string():
assert_conversion(forms.DateField, graphene.String)
def test_should_date_time_convert_string():
assert_conversion(forms.DateTimeField, graphene.String)

View File

@ -371,6 +371,7 @@ def test_should_query_node_filtering():
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='es'
@ -378,6 +379,7 @@ def test_should_query_node_filtering():
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='en'
@ -453,6 +455,7 @@ def test_should_query_node_multiple_filtering():
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='es'
@ -460,6 +463,7 @@ def test_should_query_node_multiple_filtering():
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='es'
@ -467,6 +471,7 @@ def test_should_query_node_multiple_filtering():
Article.objects.create(
headline='Article Node 3',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='en'
@ -692,6 +697,7 @@ def test_should_query_dataloader_fields():
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='es'
@ -699,6 +705,7 @@ def test_should_query_dataloader_fields():
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='en'

View File

@ -64,7 +64,7 @@ def test_django_objecttype_map_correct_fields():
def test_django_objecttype_with_node_have_correct_fields():
fields = Article._meta.fields
assert list(fields.keys()) == ['id', 'headline', 'pub_date', 'reporter', 'editor', 'lang', 'importance']
assert list(fields.keys()) == ['id', 'headline', 'pub_date', 'pub_date_time', 'reporter', 'editor', 'lang', 'importance']
def test_schema_representation():
@ -76,7 +76,8 @@ schema {
type Article implements Node {
id: ID!
headline: String!
pubDate: DateTime!
pubDate: Date!
pubDateTime: DateTime!
reporter: Reporter!
editor: Reporter!
lang: ArticleLang!
@ -104,6 +105,8 @@ enum ArticleLang {
EN
}
scalar Date
scalar DateTime
interface Node {