From dc5799e109d2e2cd7eed59282fe35a477942a56b Mon Sep 17 00:00:00 2001 From: Jason Kraus Date: Thu, 30 May 2019 13:36:48 -0700 Subject: [PATCH 1/7] detect if value is a string for enum conversion else use help text to generate name --- graphene_django/converter.py | 9 ++++++--- graphene_django/tests/test_converter.py | 19 +++++++++++++++++++ graphene_django/tests/test_types.py | 12 ++++++------ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index c40313df0..8d39c6bb7 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -38,16 +38,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/test_converter.py b/graphene_django/tests/test_converter.py index bb176b343..c6ee12812 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", diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 8a8643b9b..16e187af0 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,13 @@ def test_schema_representation(): } enum ReporterAChoice { - A_1 - A_2 + THIS + THAT } enum ReporterReporterType { - A_1 - A_2 + REGULAR + CNN_REPORTER } type RootQuery { From c4febcde04c8778f1622a2df713607313c4d7411 Mon Sep 17 00:00:00 2001 From: Jason Kraus Date: Thu, 30 May 2019 14:13:19 -0700 Subject: [PATCH 2/7] cover enum case where choice begins with a number --- graphene_django/tests/models.py | 2 +- graphene_django/tests/test_types.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index 4fe546deb..2d9b79433 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -3,7 +3,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -CHOICES = ((1, "this"), (2, _("that"))) +CHOICES = ((1, "1: this"), (2, _("2: that"))) class Pet(models.Model): diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 16e187af0..888faf49e 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -172,8 +172,8 @@ def test_schema_representation(): } enum ReporterAChoice { - THIS - THAT + A_1_THIS + A_2_THAT } enum ReporterReporterType { From 3d99b9c05f0b58b6391fa8ca09e854b14c65fd8b Mon Sep 17 00:00:00 2001 From: Jason Kraus Date: Fri, 31 May 2019 10:57:18 -0700 Subject: [PATCH 3/7] chomp out non-ascii characters from enum name generation --- graphene_django/converter.py | 3 ++- graphene_django/tests/models.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index 8d39c6bb7..c7c5cd6fc 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -29,7 +29,8 @@ def convert_choice_name(name): - name = to_const(force_text(name)) + name = force_text(name).encode('utf8').decode('ascii', 'ignore') + name = to_const(name) try: assert_valid_name(name) except AssertionError: diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index 2d9b79433..1d456b24b 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -3,7 +3,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -CHOICES = ((1, "1: this"), (2, _("2: that"))) +CHOICES = ((1, "1: this漢"), (2, _("2: that漢"))) class Pet(models.Model): From 5bbbe4c03f04c641274dc2aeffd027dd1f4f3e7b Mon Sep 17 00:00:00 2001 From: Jason Kraus Date: Fri, 31 May 2019 11:03:26 -0700 Subject: [PATCH 4/7] fix python 2 test regression --- graphene_django/tests/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index 1d456b24b..957d3564f 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -3,7 +3,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -CHOICES = ((1, "1: this漢"), (2, _("2: that漢"))) +CHOICES = ((1, u"1: this漢"), (2, _(u"2: that漢"))) class Pet(models.Model): From 65531595ef641be7bad180d07303a56376534477 Mon Sep 17 00:00:00 2001 From: Jason Kraus Date: Fri, 31 May 2019 11:39:11 -0700 Subject: [PATCH 5/7] fix python 2 test regression: set source file encoding to utf8 --- graphene_django/tests/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index 957d3564f..f54575f28 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from __future__ import absolute_import from django.db import models From ae0331873e54a33a84b1aa9da944a0a7ca42d0a5 Mon Sep 17 00:00:00 2001 From: mvanlonden Date: Fri, 31 May 2019 14:38:34 -0700 Subject: [PATCH 6/7] increment version to match release tag --- graphene_django/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"] From 32d280818751b3e44dd226c3ae4ee45b9e5f49da Mon Sep 17 00:00:00 2001 From: Jason Kraus Date: Wed, 5 Jun 2019 09:52:50 -0700 Subject: [PATCH 7/7] test and handle choices that start with an underscore, issue #141 --- graphene_django/converter.py | 2 ++ graphene_django/tests/models.py | 2 +- graphene_django/tests/test_converter.py | 18 ++++++++++++++++++ graphene_django/tests/test_types.py | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index c7c5cd6fc..09c5583ba 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -31,6 +31,8 @@ def convert_choice_name(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: diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index f54575f28..39e626bd8 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -4,7 +4,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -CHOICES = ((1, u"1: this漢"), (2, _(u"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 c6ee12812..466e60738 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -215,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 888faf49e..25dbfbc28 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -174,6 +174,7 @@ def test_schema_representation(): enum ReporterAChoice { A_1_THIS A_2_THAT + A__AMOUNT__ } enum ReporterReporterType {