mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 17:52:19 +03:00
detect if value is a string for enum conversion else use help text to generate name
This commit is contained in:
parent
b0cba398a1
commit
dc5799e109
|
@ -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
|
||||
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user