2020-04-06 15:21:07 +03:00
|
|
|
from functools import singledispatch
|
|
|
|
|
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
|
|
|
|
2022-01-18 17:03:08 +03:00
|
|
|
from graphene import (
|
|
|
|
ID,
|
2023-08-06 01:47:00 +03:00
|
|
|
UUID,
|
2022-01-18 17:03:08 +03:00
|
|
|
Boolean,
|
2023-08-06 01:47:00 +03:00
|
|
|
Date,
|
|
|
|
DateTime,
|
2022-01-18 17:03:08 +03:00
|
|
|
Decimal,
|
|
|
|
Float,
|
|
|
|
Int,
|
|
|
|
List,
|
|
|
|
String,
|
|
|
|
Time,
|
|
|
|
)
|
2017-07-18 18:15:25 +03:00
|
|
|
|
2017-10-02 17:42:57 +03:00
|
|
|
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
2020-06-06 19:08:10 +03:00
|
|
|
def get_form_field_description(field):
|
2020-06-10 19:21:37 +03:00
|
|
|
return str(field.help_text) if field.help_text else None
|
2020-06-06 19:08:10 +03:00
|
|
|
|
|
|
|
|
2017-07-18 18:15:25 +03:00
|
|
|
@singledispatch
|
2017-10-02 17:42:57 +03:00
|
|
|
def convert_form_field(field):
|
2017-07-18 18:15:25 +03:00
|
|
|
raise ImproperlyConfigured(
|
2023-08-06 01:47:00 +03:00
|
|
|
"Don't know how to convert the Django form field {} ({}) "
|
|
|
|
"to Graphene type".format(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):
|
2020-06-06 19:08:10 +03:00
|
|
|
return String(
|
|
|
|
description=get_form_field_description(field), required=field.required
|
|
|
|
)
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
2017-12-10 08:53:13 +03:00
|
|
|
@convert_form_field.register(forms.UUIDField)
|
2017-10-02 17:42:57 +03:00
|
|
|
def convert_form_field_to_uuid(field):
|
2020-06-06 19:08:10 +03:00
|
|
|
return UUID(description=get_form_field_description(field), 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):
|
2020-06-06 19:08:10 +03:00
|
|
|
return Int(description=get_form_field_description(field), 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):
|
2020-06-06 19:08:10 +03:00
|
|
|
return Boolean(
|
|
|
|
description=get_form_field_description(field), required=field.required
|
|
|
|
)
|
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):
|
2020-06-06 19:08:10 +03:00
|
|
|
return Boolean(description=get_form_field_description(field))
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
2017-10-02 17:42:57 +03:00
|
|
|
@convert_form_field.register(forms.FloatField)
|
|
|
|
def convert_form_field_to_float(field):
|
2020-06-06 19:08:10 +03:00
|
|
|
return Float(description=get_form_field_description(field), required=field.required)
|
2017-07-18 18:15:25 +03:00
|
|
|
|
|
|
|
|
2022-01-18 17:03:08 +03:00
|
|
|
@convert_form_field.register(forms.DecimalField)
|
|
|
|
def convert_form_field_to_decimal(field):
|
|
|
|
return Decimal(
|
|
|
|
description=get_form_field_description(field), required=field.required
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-25 16:22:09 +03:00
|
|
|
@convert_form_field.register(forms.MultipleChoiceField)
|
|
|
|
def convert_form_field_to_string_list(field):
|
2020-06-06 19:08:10 +03:00
|
|
|
return List(
|
|
|
|
String, description=get_form_field_description(field), required=field.required
|
|
|
|
)
|
2020-04-25 16:22:09 +03:00
|
|
|
|
|
|
|
|
2017-10-02 17:42:57 +03:00
|
|
|
@convert_form_field.register(forms.ModelMultipleChoiceField)
|
|
|
|
@convert_form_field.register(GlobalIDMultipleChoiceField)
|
2020-04-25 16:22:09 +03:00
|
|
|
def convert_form_field_to_id_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):
|
2020-06-06 19:08:10 +03:00
|
|
|
return Date(description=get_form_field_description(field), required=field.required)
|
2018-01-08 21:48:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
@convert_form_field.register(forms.DateTimeField)
|
|
|
|
def convert_form_field_to_datetime(field):
|
2020-06-06 19:08:10 +03:00
|
|
|
return DateTime(
|
|
|
|
description=get_form_field_description(field), required=field.required
|
|
|
|
)
|
2018-01-08 21:48:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
@convert_form_field.register(forms.TimeField)
|
|
|
|
def convert_form_field_to_time(field):
|
2020-06-06 19:08:10 +03:00
|
|
|
return Time(description=get_form_field_description(field), 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):
|
2016-09-23 05:57:28 +03:00
|
|
|
return ID(required=field.required)
|