Skip to content

models.DateField => graphene Date Scalar #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphene_django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
DjangoConnectionField,
)

__version__ = '2.0.0'
__version__ = '2.0.1'

__all__ = [
'__version__',
Expand Down
9 changes: 7 additions & 2 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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
Expand Down Expand Up @@ -121,9 +121,14 @@ def convert_field_to_float(field, registry=None):
return Float(description=field.help_text, required=not field.null)


@convert_django_field.register(models.DateTimeField)
def convert_datetime_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 DateTime(description=field.help_text, required=not field.null)
return Date(description=field.help_text, required=not field.null)


@convert_django_field.register(models.TimeField)
Expand Down
10 changes: 6 additions & 4 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ class Query(ObjectType):

r1 = Reporter.objects.create(first_name='r1', last_name='r1', email='[email protected]')
r2 = Reporter.objects.create(first_name='r2', last_name='r2', email='[email protected]')
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
Expand Down Expand Up @@ -245,8 +245,8 @@ class Query(ObjectType):

r1 = Reporter.objects.create(first_name='r1', last_name='r1', email='[email protected]')
r2 = Reporter.objects.create(first_name='r2', last_name='r2', email='[email protected]')
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 {
Expand Down Expand Up @@ -464,13 +464,15 @@ def resolve_all_reporters(self, info, **args):
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.now(),
pub_date_time=datetime.now(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.now(),
pub_date_time=datetime.now(),
reporter=r,
editor=r,
lang='en'
Expand Down
6 changes: 5 additions & 1 deletion graphene_django/rest_framework/serializer_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ def convert_serializer_field_to_float(field):


@get_graphene_type_from_serializer_field.register(serializers.DateTimeField)
def convert_serializer_field_to_datetime_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.DateTime
return graphene.types.datetime.Date


@get_graphene_type_from_serializer_field.register(serializers.TimeField)
Expand Down
4 changes: 2 additions & 2 deletions graphene_django/rest_framework/tests/test_field_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
1 change: 1 addition & 0 deletions graphene_django/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __str__(self): # __unicode__ on Python 2
class Article(models.Model):
headline = models.CharField(max_length=100)
pub_date = models.DateField()
pub_date_time = models.DateTimeField()
reporter = models.ForeignKey(Reporter, related_name='articles')
editor = models.ForeignKey(Reporter, related_name='edited_articles_+')
lang = models.CharField(max_length=2, help_text='Language', choices=[
Expand Down
7 changes: 5 additions & 2 deletions graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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
Expand Down Expand Up @@ -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():
Expand Down
2 changes: 2 additions & 0 deletions graphene_django/tests/test_form_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,15 @@ class Query(graphene.ObjectType):
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='en'
Expand Down Expand Up @@ -453,20 +455,23 @@ class Query(graphene.ObjectType):
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 3',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='en'
Expand Down Expand Up @@ -692,13 +697,15 @@ class Query(graphene.ObjectType):
Article.objects.create(
headline='Article Node 1',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='es'
)
Article.objects.create(
headline='Article Node 2',
pub_date=datetime.date.today(),
pub_date_time=datetime.datetime.now(),
reporter=r,
editor=r,
lang='en'
Expand Down
7 changes: 5 additions & 2 deletions graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -76,7 +76,8 @@ def test_schema_representation():
type Article implements Node {
id: ID!
headline: String!
pubDate: DateTime!
pubDate: Date!
pubDateTime: DateTime!
reporter: Reporter!
editor: Reporter!
lang: ArticleLang!
Expand Down Expand Up @@ -104,6 +105,8 @@ def test_schema_representation():
EN
}

scalar Date

scalar DateTime

interface Node {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

install_requires=[
'six>=1.10.0',
'graphene>=2.0,<3',
'graphene>=2.0.1,<3',
django_version,
'iso8601',
'singledispatch>=3.4.0.3',
Expand Down