Skip to content

A Slightly smarter enum conversion #654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphene_django/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .types import DjangoObjectType
from .fields import DjangoConnectionField

__version__ = "2.2.0"
__version__ = "2.3.0"

__all__ = ["__version__", "DjangoObjectType", "DjangoConnectionField"]
14 changes: 10 additions & 4 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion graphene_django/tests/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
37 changes: 37 additions & 0 deletions graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)

Expand Down
13 changes: 7 additions & 6 deletions graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def test_schema_representation():
}

enum ArticleImportance {
A_1
A_2
VERY_IMPORTANT
NOT_AS_IMPORTANT
}

enum ArticleLang {
Expand Down Expand Up @@ -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 {
Expand Down