mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-04-28 13:03:50 +03:00
Merge pull request #8 from revolico/convert_form_choice#7
Convert form choice#7
This commit is contained in:
commit
1151897f4a
|
@ -167,5 +167,6 @@ class DjangoField(Field):
|
||||||
"""Intercept resolver to analyse permissions"""
|
"""Intercept resolver to analyse permissions"""
|
||||||
parent_resolver = super(DjangoField, self).get_resolver(parent_resolver)
|
parent_resolver = super(DjangoField, self).get_resolver(parent_resolver)
|
||||||
if self.permissions:
|
if self.permissions:
|
||||||
return partial(get_unbound_function(self.permissions_resolver), parent_resolver, self.permissions, None, None, True)
|
return partial(get_unbound_function(self.permissions_resolver), parent_resolver, self.permissions, None,
|
||||||
|
None, True)
|
||||||
return parent_resolver
|
return parent_resolver
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
|
||||||
from graphene import ID, Boolean, Float, Int, List, String, UUID, Date, DateTime, Time
|
from graphene import ID, Boolean, Float, Int, List, String, UUID, Date, DateTime, Time, Enum
|
||||||
|
from graphene.utils.str_converters import to_camel_case
|
||||||
|
|
||||||
|
from graphene_django.converter import get_choices
|
||||||
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
|
from .forms import GlobalIDFormField, GlobalIDMultipleChoiceField
|
||||||
from ..utils import import_single_dispatch
|
from ..utils import import_single_dispatch
|
||||||
|
|
||||||
|
@ -82,3 +84,44 @@ def convert_form_field_to_time(field):
|
||||||
@convert_form_field.register(GlobalIDFormField)
|
@convert_form_field.register(GlobalIDFormField)
|
||||||
def convert_form_field_to_id(field):
|
def convert_form_field_to_id(field):
|
||||||
return ID(required=field.required)
|
return ID(required=field.required)
|
||||||
|
|
||||||
|
|
||||||
|
def get_form_name(form):
|
||||||
|
"""Get form name"""
|
||||||
|
class_name = str(form.__class__).split('.')[-1]
|
||||||
|
return class_name[:-2]
|
||||||
|
|
||||||
|
|
||||||
|
def convert_form_field_with_choices(field, name=None, form=None):
|
||||||
|
"""
|
||||||
|
Helper method to convert a field to graphene Field type.
|
||||||
|
:param name: form field's name
|
||||||
|
:param field: form field to convert to
|
||||||
|
:param form: field's form
|
||||||
|
:return: graphene Field
|
||||||
|
"""
|
||||||
|
choices = getattr(field, 'choices', None)
|
||||||
|
|
||||||
|
# If is a choice field, but not depends on models
|
||||||
|
if choices and not isinstance(field, (forms.ModelMultipleChoiceField, forms.ModelChoiceField)):
|
||||||
|
if form:
|
||||||
|
name = to_camel_case("{}_{}".format(get_form_name(form), field.label or name))
|
||||||
|
else:
|
||||||
|
name = field.label or name
|
||||||
|
name = to_camel_case(name.replace(' ', '_'))
|
||||||
|
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"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self):
|
||||||
|
"""Return field description"""
|
||||||
|
|
||||||
|
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.types.utils import yank_fields_from_attrs
|
||||||
from graphene_django.registry import get_global_registry
|
from graphene_django.registry import get_global_registry
|
||||||
|
|
||||||
from .converter import convert_form_field
|
from .converter import convert_form_field_with_choices
|
||||||
from .types import ErrorType
|
from .types import ErrorType
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ def fields_for_form(form, only_fields, exclude_fields):
|
||||||
if is_not_in_only or is_excluded:
|
if is_not_in_only or is_excluded:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
fields[name] = convert_form_field(field)
|
fields[name] = convert_form_field_with_choices(field, name=name, form=form)
|
||||||
return fields
|
return fields
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,9 +14,10 @@ from graphene import (
|
||||||
DateTime,
|
DateTime,
|
||||||
Date,
|
Date,
|
||||||
Time,
|
Time,
|
||||||
|
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):
|
def assert_conversion(django_field, graphene_field, *args):
|
||||||
|
@ -112,3 +113,9 @@ def test_should_manytoone_convert_connectionorlist():
|
||||||
field = forms.ModelChoiceField(queryset=None)
|
field = forms.ModelChoiceField(queryset=None)
|
||||||
graphene_type = convert_form_field(field)
|
graphene_type = convert_form_field(field)
|
||||||
assert isinstance(graphene_type, ID)
|
assert isinstance(graphene_type, ID)
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_typed_choice_convert_enum():
|
||||||
|
field = forms.TypedChoiceField(choices=(('A', 'Choice A'), ('B', 'Choice B')), label='field')
|
||||||
|
graphene_type = convert_form_field_with_choices(field, name='field_name')
|
||||||
|
assert isinstance(graphene_type, Enum)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user