2016-09-18 02:29:00 +03:00
|
|
|
from django import forms
|
|
|
|
from django.forms.fields import BaseTemporalField
|
|
|
|
|
2017-07-25 09:42:40 +03:00
|
|
|
from graphene import ID, Boolean, Float, Int, List, String, UUID
|
2018-01-08 21:48:47 +03:00
|
|
|
from graphene.types.datetime import Date, DateTime, Time
|
2016-09-18 03:09:56 +03:00
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
|
|
|
|
from .utils import import_single_dispatch
|
|
|
|
|
|
|
|
singledispatch = import_single_dispatch()
|
|
|
|
|
|
|
|
|
|
|
|
@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):
|
2016-09-23 05:57:28 +03:00
|
|
|
return String(description=field.help_text, required=field.required)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2017-12-10 08:53:13 +03:00
|
|
|
@convert_form_field.register(forms.UUIDField)
|
2017-07-25 09:42:40 +03:00
|
|
|
def convert_form_field_to_uuid(field):
|
|
|
|
return UUID(description=field.help_text, required=field.required)
|
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
@convert_form_field.register(forms.IntegerField)
|
|
|
|
@convert_form_field.register(forms.NumberInput)
|
|
|
|
def convert_form_field_to_int(field):
|
2016-09-23 05:57:28 +03:00
|
|
|
return Int(description=field.help_text, required=field.required)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
@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):
|
2016-09-23 05:57:28 +03:00
|
|
|
return Float(description=field.help_text, required=field.required)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
@convert_form_field.register(forms.ModelMultipleChoiceField)
|
|
|
|
@convert_form_field.register(GlobalIDMultipleChoiceField)
|
|
|
|
def convert_form_field_to_list(field):
|
2016-09-23 05:57:28 +03:00
|
|
|
return List(ID, required=field.required)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2018-01-08 21:48:47 +03:00
|
|
|
@convert_form_field.register(forms.DateField)
|
|
|
|
def convert_form_field_to_date(field):
|
|
|
|
return Date(description=field.help_text, required=field.required)
|
|
|
|
|
|
|
|
|
|
|
|
@convert_form_field.register(forms.DateTimeField)
|
|
|
|
def convert_form_field_to_datetime(field):
|
|
|
|
return DateTime(description=field.help_text, required=field.required)
|
|
|
|
|
|
|
|
|
|
|
|
@convert_form_field.register(forms.TimeField)
|
|
|
|
def convert_form_field_to_time(field):
|
|
|
|
return Time(description=field.help_text, required=field.required)
|
|
|
|
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
@convert_form_field.register(forms.ModelChoiceField)
|
|
|
|
@convert_form_field.register(GlobalIDFormField)
|
|
|
|
def convert_form_field_to_id(field):
|
2016-09-23 05:57:28 +03:00
|
|
|
return ID(required=field.required)
|