mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-01-31 03:34:13 +03:00
(v3) Cast potentially translated strings used as descriptions (#976)
This commit is contained in:
parent
fb90cb78b3
commit
b320b94a73
|
@ -108,7 +108,9 @@ def convert_django_field_with_choices(
|
|||
if choices and convert_choices_to_enum:
|
||||
enum = convert_choice_field_to_enum(field)
|
||||
required = not (field.blank or field.null)
|
||||
converted = enum(description=field.help_text, required=required)
|
||||
converted = enum(
|
||||
description=get_django_field_description(field), required=required
|
||||
)
|
||||
else:
|
||||
converted = convert_django_field(field, registry)
|
||||
if registry is not None:
|
||||
|
@ -116,6 +118,10 @@ def convert_django_field_with_choices(
|
|||
return converted
|
||||
|
||||
|
||||
def get_django_field_description(field):
|
||||
return None if field.help_text is None else str(field.help_text)
|
||||
|
||||
|
||||
@singledispatch
|
||||
def convert_django_field(field, registry=None):
|
||||
raise Exception(
|
||||
|
@ -132,17 +138,21 @@ def convert_django_field(field, registry=None):
|
|||
@convert_django_field.register(models.FileField)
|
||||
@convert_django_field.register(models.FilePathField)
|
||||
def convert_field_to_string(field, registry=None):
|
||||
return String(description=field.help_text, required=not field.null)
|
||||
return String(
|
||||
description=get_django_field_description(field), required=not field.null
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(models.AutoField)
|
||||
def convert_field_to_id(field, registry=None):
|
||||
return ID(description=field.help_text, required=not field.null)
|
||||
return ID(description=get_django_field_description(field), required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(models.UUIDField)
|
||||
def convert_field_to_uuid(field, registry=None):
|
||||
return UUID(description=field.help_text, required=not field.null)
|
||||
return UUID(
|
||||
description=get_django_field_description(field), required=not field.null
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(models.PositiveIntegerField)
|
||||
|
@ -151,35 +161,45 @@ def convert_field_to_uuid(field, registry=None):
|
|||
@convert_django_field.register(models.BigIntegerField)
|
||||
@convert_django_field.register(models.IntegerField)
|
||||
def convert_field_to_int(field, registry=None):
|
||||
return Int(description=field.help_text, required=not field.null)
|
||||
return Int(description=get_django_field_description(field), required=not field.null)
|
||||
|
||||
|
||||
@convert_django_field.register(models.NullBooleanField)
|
||||
@convert_django_field.register(models.BooleanField)
|
||||
def convert_field_to_boolean(field, registry=None):
|
||||
return Boolean(description=field.help_text, required=not field.null)
|
||||
return Boolean(
|
||||
description=get_django_field_description(field), required=not field.null
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(models.DecimalField)
|
||||
@convert_django_field.register(models.FloatField)
|
||||
@convert_django_field.register(models.DurationField)
|
||||
def convert_field_to_float(field, registry=None):
|
||||
return Float(description=field.help_text, required=not field.null)
|
||||
return Float(
|
||||
description=get_django_field_description(field), required=not field.null
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(models.DateTimeField)
|
||||
def convert_datetime_to_string(field, registry=None):
|
||||
return DateTime(description=field.help_text, required=not field.null)
|
||||
return DateTime(
|
||||
description=get_django_field_description(field), required=not field.null
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(models.DateField)
|
||||
def convert_date_to_string(field, registry=None):
|
||||
return Date(description=field.help_text, required=not field.null)
|
||||
return Date(
|
||||
description=get_django_field_description(field), required=not field.null
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(models.TimeField)
|
||||
def convert_time_to_string(field, registry=None):
|
||||
return Time(description=field.help_text, required=not field.null)
|
||||
return Time(
|
||||
description=get_django_field_description(field), required=not field.null
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(models.OneToOneRel)
|
||||
|
@ -250,7 +270,11 @@ def convert_field_to_djangomodel(field, registry=None):
|
|||
if not _type:
|
||||
return
|
||||
|
||||
return Field(_type, description=field.help_text, required=not field.null)
|
||||
return Field(
|
||||
_type,
|
||||
description=get_django_field_description(field),
|
||||
required=not field.null,
|
||||
)
|
||||
|
||||
return Dynamic(dynamic_type)
|
||||
|
||||
|
@ -260,13 +284,19 @@ def convert_postgres_array_to_list(field, registry=None):
|
|||
base_type = convert_django_field(field.base_field)
|
||||
if not isinstance(base_type, (List, NonNull)):
|
||||
base_type = type(base_type)
|
||||
return List(base_type, description=field.help_text, required=not field.null)
|
||||
return List(
|
||||
base_type,
|
||||
description=get_django_field_description(field),
|
||||
required=not field.null,
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(HStoreField)
|
||||
@convert_django_field.register(JSONField)
|
||||
def convert_postgres_field_to_string(field, registry=None):
|
||||
return JSONString(description=field.help_text, required=not field.null)
|
||||
return JSONString(
|
||||
description=get_django_field_description(field), required=not field.null
|
||||
)
|
||||
|
||||
|
||||
@convert_django_field.register(RangeField)
|
||||
|
@ -274,7 +304,11 @@ def convert_postgres_range_to_string(field, registry=None):
|
|||
inner_type = convert_django_field(field.base_field)
|
||||
if not isinstance(inner_type, (List, NonNull)):
|
||||
inner_type = type(inner_type)
|
||||
return List(inner_type, description=field.help_text, required=not field.null)
|
||||
return List(
|
||||
inner_type,
|
||||
description=get_django_field_description(field),
|
||||
required=not field.null,
|
||||
)
|
||||
|
||||
|
||||
# Register Django lazy()-wrapped values as GraphQL description/help_text.
|
||||
|
|
|
@ -30,7 +30,9 @@ def get_filtering_args_from_filterset(filterset_class, type):
|
|||
form_field = filter_field.field
|
||||
|
||||
field_type = convert_form_field(form_field).Argument()
|
||||
field_type.description = filter_field.label
|
||||
field_type.description = (
|
||||
None if filter_field.label is None else str(filter_field.label)
|
||||
)
|
||||
args[name] = field_type
|
||||
|
||||
return args
|
||||
|
|
|
@ -8,6 +8,10 @@ from graphene import ID, Boolean, Float, Int, List, String, UUID, Date, DateTime
|
|||
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
|
||||
|
||||
|
||||
def get_form_field_description(field):
|
||||
return None if field.help_text is None else str(field.help_text)
|
||||
|
||||
|
||||
@singledispatch
|
||||
def convert_form_field(field):
|
||||
raise ImproperlyConfigured(
|
||||
|
@ -25,39 +29,45 @@ def convert_form_field(field):
|
|||
@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)
|
||||
return String(
|
||||
description=get_form_field_description(field), required=field.required
|
||||
)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.UUIDField)
|
||||
def convert_form_field_to_uuid(field):
|
||||
return UUID(description=field.help_text, required=field.required)
|
||||
return UUID(description=get_form_field_description(field), 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)
|
||||
return Int(description=get_form_field_description(field), required=field.required)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.BooleanField)
|
||||
def convert_form_field_to_boolean(field):
|
||||
return Boolean(description=field.help_text, required=field.required)
|
||||
return Boolean(
|
||||
description=get_form_field_description(field), required=field.required
|
||||
)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.NullBooleanField)
|
||||
def convert_form_field_to_nullboolean(field):
|
||||
return Boolean(description=field.help_text)
|
||||
return Boolean(description=get_form_field_description(field))
|
||||
|
||||
|
||||
@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)
|
||||
return Float(description=get_form_field_description(field), required=field.required)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.MultipleChoiceField)
|
||||
def convert_form_field_to_string_list(field):
|
||||
return List(String, description=field.help_text, required=field.required)
|
||||
return List(
|
||||
String, description=get_form_field_description(field), required=field.required
|
||||
)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.ModelMultipleChoiceField)
|
||||
|
@ -68,17 +78,19 @@ def convert_form_field_to_id_list(field):
|
|||
|
||||
@convert_form_field.register(forms.DateField)
|
||||
def convert_form_field_to_date(field):
|
||||
return Date(description=field.help_text, required=field.required)
|
||||
return Date(description=get_form_field_description(field), 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)
|
||||
return DateTime(
|
||||
description=get_form_field_description(field), 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)
|
||||
return Time(description=get_form_field_description(field), required=field.required)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.ModelChoiceField)
|
||||
|
|
Loading…
Reference in New Issue
Block a user