detect if value is a string for enum conversion else use help text to generate name

This commit is contained in:
Jason Kraus 2019-05-30 13:36:48 -07:00
parent b0cba398a1
commit dc5799e109
3 changed files with 31 additions and 9 deletions

View File

@ -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

View File

@ -177,6 +177,25 @@ def test_field_with_choices_gettext():
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",

View File

@ -136,8 +136,8 @@ type ArticleEdge {
}
enum ArticleImportance {
A_1
A_2
VERY_IMPORTANT
NOT_AS_IMPORTANT
}
enum ArticleLang {
@ -172,13 +172,13 @@ type Reporter {
}
enum ReporterAChoice {
A_1
A_2
THIS
THAT
}
enum ReporterReporterType {
A_1
A_2
REGULAR
CNN_REPORTER
}
type RootQuery {