graphene-django/graphene_django/forms/converter.py

88 lines
2.8 KiB
Python
Raw Normal View History

2017-07-18 18:15:25 +03:00
from django import forms
from django.core.exceptions import ImproperlyConfigured
2017-10-02 17:42:57 +03:00
2017-07-18 18:15:25 +03:00
import graphene
2017-10-02 17:42:57 +03:00
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
2017-10-02 21:03:20 +03:00
from ..utils import import_single_dispatch
2017-10-02 17:42:57 +03:00
try:
UUIDField = forms.UUIDField
except AttributeError:
class UUIDField(object):
pass
2017-07-18 18:15:25 +03:00
singledispatch = import_single_dispatch()
@singledispatch
2017-10-02 17:42:57 +03:00
def convert_form_field(field):
2017-07-18 18:15:25 +03:00
raise ImproperlyConfigured(
2017-10-02 17:42:57 +03:00
"Don't know how to convert the Django form field %s (%s) "
"to Graphene type" %
(field, field.__class__)
2017-07-18 18:15:25 +03:00
)
2017-10-02 21:03:20 +03:00
@convert_form_field.register(forms.fields.BaseTemporalField)
2017-10-02 17:42:57 +03:00
@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 graphene.String(description=field.help_text, required=field.required)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(UUIDField)
def convert_form_field_to_uuid(field):
return graphene.UUID(description=field.help_text, required=field.required)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(forms.IntegerField)
@convert_form_field.register(forms.NumberInput)
def convert_form_field_to_int(field):
return graphene.Int(description=field.help_text, required=field.required)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(forms.BooleanField)
def convert_form_field_to_boolean(field):
return graphene.Boolean(description=field.help_text, required=True)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(forms.NullBooleanField)
def convert_form_field_to_nullboolean(field):
return graphene.Boolean(description=field.help_text)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(forms.DecimalField)
@convert_form_field.register(forms.FloatField)
def convert_form_field_to_float(field):
return graphene.Float(description=field.help_text, required=field.required)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(forms.ModelMultipleChoiceField)
@convert_form_field.register(GlobalIDMultipleChoiceField)
def convert_form_field_to_list(field):
return graphene.List(graphene.ID, required=field.required)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(forms.ModelChoiceField)
@convert_form_field.register(GlobalIDFormField)
def convert_form_field_to_id(field):
return graphene.ID(required=field.required)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(forms.DateField)
@convert_form_field.register(forms.DateTimeField)
def convert_form_field_to_datetime(field):
return graphene.types.datetime.DateTime(description=field.help_text, required=field.required)
2017-07-18 18:15:25 +03:00
2017-10-02 17:42:57 +03:00
@convert_form_field.register(forms.TimeField)
def convert_form_field_to_time(field):
return graphene.types.datetime.Time(description=field.help_text, required=field.required)