mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-04-25 19:43:51 +03:00
Set conversion to enum
This commit is contained in:
parent
e99d22510f
commit
8e2f626f35
|
@ -86,22 +86,30 @@ def convert_form_field_to_id(field):
|
|||
return ID(required=field.required)
|
||||
|
||||
|
||||
@convert_form_field.register(forms.TypedChoiceField)
|
||||
def convert_form_to_enum(field, name):
|
||||
def convert_form_field_with_choices(name, field):
|
||||
"""
|
||||
Helper method to convert a field to graphene Field type.
|
||||
:param name: form field's name
|
||||
:param field: form field to convert to
|
||||
:return: graphene Field
|
||||
"""
|
||||
choices = getattr(field, 'choices', None)
|
||||
name = to_camel_case(name)
|
||||
choices = list(get_choices(choices))
|
||||
named_choices = [(c[0], c[1]) for c in choices]
|
||||
named_choices_descriptions = {c[0]: c[2] for c in choices}
|
||||
|
||||
class EnumWithDescriptionsType(object):
|
||||
"""Enum type definition"""
|
||||
if choices:
|
||||
name = to_camel_case(field.label or name)
|
||||
choices = list(get_choices(choices))
|
||||
named_choices = [(c[0], c[1]) for c in choices]
|
||||
named_choices_descriptions = {c[0]: c[2] for c in choices}
|
||||
|
||||
@property
|
||||
def description(self):
|
||||
"""Return field description"""
|
||||
class EnumWithDescriptionsType(object):
|
||||
"""Enum type definition"""
|
||||
|
||||
return named_choices_descriptions[self.name]
|
||||
@property
|
||||
def description(self):
|
||||
"""Return field description"""
|
||||
|
||||
enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
|
||||
return enum(description=field.help_text, required=field.required)
|
||||
return named_choices_descriptions[self.name]
|
||||
|
||||
enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
|
||||
return enum(description=field.help_text, required=field.required) # pylint: disable=E1102
|
||||
return convert_form_field(field)
|
||||
|
|
|
@ -13,7 +13,7 @@ from graphene.types.mutation import MutationOptions
|
|||
from graphene.types.utils import yank_fields_from_attrs
|
||||
from graphene_django.registry import get_global_registry
|
||||
|
||||
from .converter import convert_form_field
|
||||
from .converter import convert_form_field, convert_form_field_with_choices
|
||||
from .types import ErrorType
|
||||
|
||||
|
||||
|
@ -30,11 +30,7 @@ def fields_for_form(form, only_fields, exclude_fields):
|
|||
if is_not_in_only or is_excluded:
|
||||
continue
|
||||
|
||||
choices = getattr(field, 'choices', None)
|
||||
if choices:
|
||||
fields[name] = convert_form_field(field, field.label or name)
|
||||
else:
|
||||
fields[name] = convert_form_field(field)
|
||||
fields[name] = convert_form_field_with_choices(name, field)
|
||||
return fields
|
||||
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ from graphene import (
|
|||
Enum,
|
||||
)
|
||||
|
||||
from ..converter import convert_form_field
|
||||
from ..converter import convert_form_field, convert_form_field_with_choices
|
||||
|
||||
|
||||
def assert_conversion(django_field, graphene_field, *args):
|
||||
|
@ -116,6 +116,6 @@ def test_should_manytoone_convert_connectionorlist():
|
|||
|
||||
|
||||
def test_should_typed_choice_convert_enum():
|
||||
field = forms.TypedChoiceField(choices=(('A', 'Choice A'), ('B', 'Choice B')))
|
||||
graphene_type = convert_form_field(field, 'field')
|
||||
field = forms.TypedChoiceField(choices=(('A', 'Choice A'), ('B', 'Choice B')), label='field')
|
||||
graphene_type = convert_form_field_with_choices('field_name', field)
|
||||
assert isinstance(graphene_type, Enum)
|
||||
|
|
Loading…
Reference in New Issue
Block a user