diff --git a/graphene_django/__init__.py b/graphene_django/__init__.py index 4538cb343..51acfd222 100644 --- a/graphene_django/__init__.py +++ b/graphene_django/__init__.py @@ -1,6 +1,6 @@ from .types import DjangoObjectType from .fields import DjangoConnectionField -__version__ = "2.2.0" +__version__ = "2.3.0" __all__ = ["__version__", "DjangoObjectType", "DjangoConnectionField"] diff --git a/graphene_django/converter.py b/graphene_django/converter.py index c40313df0..09c5583ba 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -29,7 +29,10 @@ def convert_choice_name(name): - name = to_const(force_text(name)) + name = force_text(name).encode('utf8').decode('ascii', 'ignore') + name = to_const(name) + if name.startswith('_'): + name = "A%s" % name try: assert_valid_name(name) except AssertionError: @@ -38,16 +41,19 @@ def convert_choice_name(name): def get_choices(choices): - converted_names = [] + converted_names = set() for value, help_text in choices: if isinstance(help_text, (tuple, list)): for choice in get_choices(help_text): yield choice else: - name = convert_choice_name(value) + if isinstance(value, str): + name = convert_choice_name(value) + else: + name = convert_choice_name(help_text) while name in converted_names: name += "_" + str(len(converted_names)) - converted_names.append(name) + converted_names.add(name) description = help_text yield name, value, description diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index 4fe546deb..39e626bd8 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -1,9 +1,10 @@ +# -*- coding: utf-8 -*- from __future__ import absolute_import from django.db import models from django.utils.translation import ugettext_lazy as _ -CHOICES = ((1, "this"), (2, _("that"))) +CHOICES = ((1, u"1: thisę¼¢"), (2, _(u"2: thatę¼¢")), (3, "__amount__")) class Pet(models.Model): diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index bb176b343..466e60738 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -177,6 +177,25 @@ class Meta: convert_django_field_with_choices(field) +def test_field_with_integer_choices(): + field = models.IntegerField( + help_text="Language", choices=((1, "Spanish"), (2, "English")) + ) + + class TranslatedChoicesModel(models.Model): + language = field + + class Meta: + app_label = "test" + + graphene_type = convert_django_field_with_choices(field) + #assert False, str(graphene_type._meta.enum.__members__) + assert graphene_type._meta.enum.__members__["SPANISH"].value == 1 + assert graphene_type._meta.enum.__members__["SPANISH"].description == "Spanish" + assert graphene_type._meta.enum.__members__["ENGLISH"].value == 2 + assert graphene_type._meta.enum.__members__["ENGLISH"].description == "English" + + def test_field_with_choices_collision(): field = models.CharField( help_text="Timezone", @@ -196,6 +215,24 @@ class Meta: convert_django_field_with_choices(field) +def test_field_with_choices_underscore(): + field = models.CharField( + choices=( + ("__amount__", "Amount"), + ("__percentage__", "Percentage"), + ), + ) + + class UnderscoreChoicesModel(models.Model): + ourfield = field + + class Meta: + app_label = "test" + + graphene_type = convert_django_field_with_choices(field) + assert len(graphene_type._meta.enum.__members__) == 2 + + def test_should_float_convert_float(): assert_conversion(models.FloatField, graphene.Float) diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 8a8643b9b..25dbfbc28 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -136,8 +136,8 @@ def test_schema_representation(): } enum ArticleImportance { - A_1 - A_2 + VERY_IMPORTANT + NOT_AS_IMPORTANT } enum ArticleLang { @@ -172,13 +172,14 @@ def test_schema_representation(): } enum ReporterAChoice { - A_1 - A_2 + A_1_THIS + A_2_THAT + A__AMOUNT__ } enum ReporterReporterType { - A_1 - A_2 + REGULAR + CNN_REPORTER } type RootQuery {