Skip to content

Commit 666ddb2

Browse files
author
Grant McConnaughey
committed
Merge form converter modules
1 parent f5083cb commit 666ddb2

File tree

4 files changed

+58
-121
lines changed

4 files changed

+58
-121
lines changed

graphene_django/filter/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def get_filtering_args_from_filterset(filterset_class, type):
88
a Graphene Field. These arguments will be available to
99
filter against in the GraphQL
1010
"""
11-
from ..form_converter import convert_form_field
11+
from ..forms.converter import convert_form_field
1212

1313
args = {}
1414
for name, filter_field in six.iteritems(filterset_class.base_filters):

graphene_django/form_converter.py

Lines changed: 0 additions & 75 deletions
This file was deleted.

graphene_django/forms/converter.py

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
from django import forms
22
from django.core.exceptions import ImproperlyConfigured
3-
from graphene_django.utils import import_single_dispatch
3+
44
import graphene
55

6+
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
7+
from .utils import import_single_dispatch
8+
9+
try:
10+
UUIDField = forms.UUIDField
11+
except AttributeError:
12+
class UUIDField(object):
13+
pass
14+
615

716
singledispatch = import_single_dispatch()
817

@@ -23,68 +32,71 @@ def convert_form_to_input_type(form_class):
2332

2433

2534
@singledispatch
26-
def get_graphene_type_from_form_field(field):
35+
def convert_form_field(field):
2736
raise ImproperlyConfigured(
28-
"Don't know how to convert the form field %s (%s) "
29-
"to Graphene type" % (field, field.__class__)
37+
"Don't know how to convert the Django form field %s (%s) "
38+
"to Graphene type" %
39+
(field, field.__class__)
3040
)
3141

3242

33-
def convert_form_field(field, is_input=True):
34-
"""
35-
Converts a Django form field to a graphql field and marks the field as
36-
required if we are creating an input type and the field itself is required
37-
"""
43+
@convert_form_field.register(forms.BaseTemporalField)
44+
@convert_form_field.register(forms.CharField)
45+
@convert_form_field.register(forms.EmailField)
46+
@convert_form_field.register(forms.SlugField)
47+
@convert_form_field.register(forms.URLField)
48+
@convert_form_field.register(forms.ChoiceField)
49+
@convert_form_field.register(forms.RegexField)
50+
@convert_form_field.register(forms.Field)
51+
def convert_form_field_to_string(field):
52+
return graphene.String(description=field.help_text, required=field.required)
3853

39-
graphql_type = get_graphene_type_from_form_field(field)
4054

41-
kwargs = {
42-
'description': field.help_text,
43-
'required': is_input and field.required,
44-
}
55+
@convert_form_field.register(UUIDField)
56+
def convert_form_field_to_uuid(field):
57+
return graphene.UUID(description=field.help_text, required=field.required)
4558

46-
# if it is a tuple or a list it means that we are returning
47-
# the graphql type and the child type
48-
if isinstance(graphql_type, (list, tuple)):
49-
kwargs['of_type'] = graphql_type[1]
50-
graphql_type = graphql_type[0]
5159

52-
return graphql_type(**kwargs)
60+
@convert_form_field.register(forms.IntegerField)
61+
@convert_form_field.register(forms.NumberInput)
62+
def convert_form_field_to_int(field):
63+
return graphene.Int(description=field.help_text, required=field.required)
5364

5465

55-
@get_graphene_type_from_form_field.register(forms.CharField)
56-
@get_graphene_type_from_form_field.register(forms.ChoiceField)
57-
def convert_form_field_to_string(field):
58-
return graphene.String
66+
@convert_form_field.register(forms.BooleanField)
67+
def convert_form_field_to_boolean(field):
68+
return graphene.Boolean(description=field.help_text, required=True)
5969

6070

61-
@get_graphene_type_from_form_field.register(forms.IntegerField)
62-
def convert_form_field_to_int(field):
63-
return graphene.Int
71+
@convert_form_field.register(forms.NullBooleanField)
72+
def convert_form_field_to_nullboolean(field):
73+
return graphene.Boolean(description=field.help_text)
6474

6575

66-
@get_graphene_type_from_form_field.register(forms.BooleanField)
67-
def convert_form_field_to_bool(field):
68-
return graphene.Boolean
76+
@convert_form_field.register(forms.DecimalField)
77+
@convert_form_field.register(forms.FloatField)
78+
def convert_form_field_to_float(field):
79+
return graphene.Float(description=field.help_text, required=field.required)
6980

7081

71-
@get_graphene_type_from_form_field.register(forms.FloatField)
72-
@get_graphene_type_from_form_field.register(forms.DecimalField)
73-
def convert_form_field_to_float(field):
74-
return graphene.Float
82+
@convert_form_field.register(forms.ModelMultipleChoiceField)
83+
@convert_form_field.register(GlobalIDMultipleChoiceField)
84+
def convert_form_field_to_list(field):
85+
return graphene.List(graphene.ID, required=field.required)
7586

7687

77-
@get_graphene_type_from_form_field.register(forms.DateField)
78-
@get_graphene_type_from_form_field.register(forms.DateTimeField)
79-
def convert_form_field_to_datetime(field):
80-
return graphene.types.datetime.DateTime
88+
@convert_form_field.register(forms.ModelChoiceField)
89+
@convert_form_field.register(GlobalIDFormField)
90+
def convert_form_field_to_id(field):
91+
return graphene.ID(required=field.required)
8192

8293

83-
@get_graphene_type_from_form_field.register(forms.TimeField)
84-
def convert_form_field_to_time(field):
85-
return graphene.types.datetime.Time
94+
@convert_form_field.register(forms.DateField)
95+
@convert_form_field.register(forms.DateTimeField)
96+
def convert_form_field_to_datetime(field):
97+
return graphene.types.datetime.DateTime(description=field.help_text, required=field.required)
8698

8799

88-
@get_graphene_type_from_form_field.register(forms.MultipleChoiceField)
89-
def convert_form_field_to_list_of_string(field):
90-
return (graphene.List, graphene.String)
100+
@convert_form_field.register(forms.TimeField)
101+
def convert_form_field_to_time(field):
102+
return graphene.types.datetime.Time(description=field.help_text, required=field.required)

graphene_django/tests/test_form_converter.py renamed to graphene_django/forms/tests/test_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import graphene
55
from graphene import ID, List, NonNull
66

7-
from ..form_converter import convert_form_field
7+
from ..converter import convert_form_field
88
from .models import Reporter
99

1010

0 commit comments

Comments
 (0)