mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-02-12 09:30:35 +03:00
Merge form converter modules
This commit is contained in:
parent
f5083cb190
commit
666ddb2ff3
|
@ -8,7 +8,7 @@ def get_filtering_args_from_filterset(filterset_class, type):
|
||||||
a Graphene Field. These arguments will be available to
|
a Graphene Field. These arguments will be available to
|
||||||
filter against in the GraphQL
|
filter against in the GraphQL
|
||||||
"""
|
"""
|
||||||
from ..form_converter import convert_form_field
|
from ..forms.converter import convert_form_field
|
||||||
|
|
||||||
args = {}
|
args = {}
|
||||||
for name, filter_field in six.iteritems(filterset_class.base_filters):
|
for name, filter_field in six.iteritems(filterset_class.base_filters):
|
||||||
|
|
|
@ -1,75 +0,0 @@
|
||||||
from django import forms
|
|
||||||
from django.forms.fields import BaseTemporalField
|
|
||||||
|
|
||||||
from graphene import ID, Boolean, Float, Int, List, String, UUID
|
|
||||||
|
|
||||||
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
|
|
||||||
from .utils import import_single_dispatch
|
|
||||||
|
|
||||||
singledispatch = import_single_dispatch()
|
|
||||||
|
|
||||||
try:
|
|
||||||
UUIDField = forms.UUIDField
|
|
||||||
except AttributeError:
|
|
||||||
class UUIDField(object):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@singledispatch
|
|
||||||
def convert_form_field(field):
|
|
||||||
raise Exception(
|
|
||||||
"Don't know how to convert the Django form field %s (%s) "
|
|
||||||
"to Graphene type" %
|
|
||||||
(field, field.__class__)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(BaseTemporalField)
|
|
||||||
@convert_form_field.register(forms.CharField)
|
|
||||||
@convert_form_field.register(forms.EmailField)
|
|
||||||
@convert_form_field.register(forms.SlugField)
|
|
||||||
@convert_form_field.register(forms.URLField)
|
|
||||||
@convert_form_field.register(forms.ChoiceField)
|
|
||||||
@convert_form_field.register(forms.RegexField)
|
|
||||||
@convert_form_field.register(forms.Field)
|
|
||||||
def convert_form_field_to_string(field):
|
|
||||||
return String(description=field.help_text, required=field.required)
|
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(UUIDField)
|
|
||||||
def convert_form_field_to_uuid(field):
|
|
||||||
return UUID(description=field.help_text, required=field.required)
|
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(forms.IntegerField)
|
|
||||||
@convert_form_field.register(forms.NumberInput)
|
|
||||||
def convert_form_field_to_int(field):
|
|
||||||
return Int(description=field.help_text, required=field.required)
|
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(forms.BooleanField)
|
|
||||||
def convert_form_field_to_boolean(field):
|
|
||||||
return Boolean(description=field.help_text, required=True)
|
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(forms.NullBooleanField)
|
|
||||||
def convert_form_field_to_nullboolean(field):
|
|
||||||
return Boolean(description=field.help_text)
|
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(forms.DecimalField)
|
|
||||||
@convert_form_field.register(forms.FloatField)
|
|
||||||
def convert_form_field_to_float(field):
|
|
||||||
return Float(description=field.help_text, required=field.required)
|
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(forms.ModelMultipleChoiceField)
|
|
||||||
@convert_form_field.register(GlobalIDMultipleChoiceField)
|
|
||||||
def convert_form_field_to_list(field):
|
|
||||||
return List(ID, required=field.required)
|
|
||||||
|
|
||||||
|
|
||||||
@convert_form_field.register(forms.ModelChoiceField)
|
|
||||||
@convert_form_field.register(GlobalIDFormField)
|
|
||||||
def convert_form_field_to_id(field):
|
|
||||||
return ID(required=field.required)
|
|
|
@ -1,8 +1,17 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from graphene_django.utils import import_single_dispatch
|
|
||||||
import graphene
|
import graphene
|
||||||
|
|
||||||
|
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
|
||||||
|
from .utils import import_single_dispatch
|
||||||
|
|
||||||
|
try:
|
||||||
|
UUIDField = forms.UUIDField
|
||||||
|
except AttributeError:
|
||||||
|
class UUIDField(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
singledispatch = import_single_dispatch()
|
singledispatch = import_single_dispatch()
|
||||||
|
|
||||||
|
@ -23,68 +32,71 @@ def convert_form_to_input_type(form_class):
|
||||||
|
|
||||||
|
|
||||||
@singledispatch
|
@singledispatch
|
||||||
def get_graphene_type_from_form_field(field):
|
def convert_form_field(field):
|
||||||
raise ImproperlyConfigured(
|
raise ImproperlyConfigured(
|
||||||
"Don't know how to convert the form field %s (%s) "
|
"Don't know how to convert the Django form field %s (%s) "
|
||||||
"to Graphene type" % (field, field.__class__)
|
"to Graphene type" %
|
||||||
|
(field, field.__class__)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def convert_form_field(field, is_input=True):
|
@convert_form_field.register(forms.BaseTemporalField)
|
||||||
"""
|
@convert_form_field.register(forms.CharField)
|
||||||
Converts a Django form field to a graphql field and marks the field as
|
@convert_form_field.register(forms.EmailField)
|
||||||
required if we are creating an input type and the field itself is required
|
@convert_form_field.register(forms.SlugField)
|
||||||
"""
|
@convert_form_field.register(forms.URLField)
|
||||||
|
@convert_form_field.register(forms.ChoiceField)
|
||||||
graphql_type = get_graphene_type_from_form_field(field)
|
@convert_form_field.register(forms.RegexField)
|
||||||
|
@convert_form_field.register(forms.Field)
|
||||||
kwargs = {
|
|
||||||
'description': field.help_text,
|
|
||||||
'required': is_input and field.required,
|
|
||||||
}
|
|
||||||
|
|
||||||
# if it is a tuple or a list it means that we are returning
|
|
||||||
# the graphql type and the child type
|
|
||||||
if isinstance(graphql_type, (list, tuple)):
|
|
||||||
kwargs['of_type'] = graphql_type[1]
|
|
||||||
graphql_type = graphql_type[0]
|
|
||||||
|
|
||||||
return graphql_type(**kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
@get_graphene_type_from_form_field.register(forms.CharField)
|
|
||||||
@get_graphene_type_from_form_field.register(forms.ChoiceField)
|
|
||||||
def convert_form_field_to_string(field):
|
def convert_form_field_to_string(field):
|
||||||
return graphene.String
|
return graphene.String(description=field.help_text, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
@get_graphene_type_from_form_field.register(forms.IntegerField)
|
@convert_form_field.register(UUIDField)
|
||||||
|
def convert_form_field_to_uuid(field):
|
||||||
|
return graphene.UUID(description=field.help_text, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
|
@convert_form_field.register(forms.IntegerField)
|
||||||
|
@convert_form_field.register(forms.NumberInput)
|
||||||
def convert_form_field_to_int(field):
|
def convert_form_field_to_int(field):
|
||||||
return graphene.Int
|
return graphene.Int(description=field.help_text, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
@get_graphene_type_from_form_field.register(forms.BooleanField)
|
@convert_form_field.register(forms.BooleanField)
|
||||||
def convert_form_field_to_bool(field):
|
def convert_form_field_to_boolean(field):
|
||||||
return graphene.Boolean
|
return graphene.Boolean(description=field.help_text, required=True)
|
||||||
|
|
||||||
|
|
||||||
@get_graphene_type_from_form_field.register(forms.FloatField)
|
@convert_form_field.register(forms.NullBooleanField)
|
||||||
@get_graphene_type_from_form_field.register(forms.DecimalField)
|
def convert_form_field_to_nullboolean(field):
|
||||||
|
return graphene.Boolean(description=field.help_text)
|
||||||
|
|
||||||
|
|
||||||
|
@convert_form_field.register(forms.DecimalField)
|
||||||
|
@convert_form_field.register(forms.FloatField)
|
||||||
def convert_form_field_to_float(field):
|
def convert_form_field_to_float(field):
|
||||||
return graphene.Float
|
return graphene.Float(description=field.help_text, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
@get_graphene_type_from_form_field.register(forms.DateField)
|
@convert_form_field.register(forms.ModelMultipleChoiceField)
|
||||||
@get_graphene_type_from_form_field.register(forms.DateTimeField)
|
@convert_form_field.register(GlobalIDMultipleChoiceField)
|
||||||
|
def convert_form_field_to_list(field):
|
||||||
|
return graphene.List(graphene.ID, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
|
@convert_form_field.register(forms.ModelChoiceField)
|
||||||
|
@convert_form_field.register(GlobalIDFormField)
|
||||||
|
def convert_form_field_to_id(field):
|
||||||
|
return graphene.ID(required=field.required)
|
||||||
|
|
||||||
|
|
||||||
|
@convert_form_field.register(forms.DateField)
|
||||||
|
@convert_form_field.register(forms.DateTimeField)
|
||||||
def convert_form_field_to_datetime(field):
|
def convert_form_field_to_datetime(field):
|
||||||
return graphene.types.datetime.DateTime
|
return graphene.types.datetime.DateTime(description=field.help_text, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
@get_graphene_type_from_form_field.register(forms.TimeField)
|
@convert_form_field.register(forms.TimeField)
|
||||||
def convert_form_field_to_time(field):
|
def convert_form_field_to_time(field):
|
||||||
return graphene.types.datetime.Time
|
return graphene.types.datetime.Time(description=field.help_text, required=field.required)
|
||||||
|
|
||||||
|
|
||||||
@get_graphene_type_from_form_field.register(forms.MultipleChoiceField)
|
|
||||||
def convert_form_field_to_list_of_string(field):
|
|
||||||
return (graphene.List, graphene.String)
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ from py.test import raises
|
||||||
import graphene
|
import graphene
|
||||||
from graphene import ID, List, NonNull
|
from graphene import ID, List, NonNull
|
||||||
|
|
||||||
from ..form_converter import convert_form_field
|
from ..converter import convert_form_field
|
||||||
from .models import Reporter
|
from .models import Reporter
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user