Skip to content

Commit 17146f9

Browse files
DoctorJohnjkimbo
andauthored
Make v3 django choice field enum naming default (in v3) (#982)
Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
1 parent 85976ff commit 17146f9

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

docs/settings.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,20 @@ Default: ``False``
142142
# ]
143143
144144
145-
``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING``
145+
``DJANGO_CHOICE_FIELD_ENUM_V2_NAMING``
146146
--------------------------------------
147147

148-
Set to ``True`` to use the new naming format for the auto generated Enum types from Django choice fields. The new format looks like this: ``{app_label}{object_name}{field_name}Choices``
148+
Set to ``True`` to use the old naming format for the auto generated Enum types from Django choice fields. The old format looks like this: ``{object_name}_{field_name}``
149149

150150
Default: ``False``
151151

152152

153153
``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME``
154-
--------------------------------------
154+
----------------------------------------
155155

156156
Define the path of a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type.
157157

158-
If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` setting is ignored.
158+
If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V2_NAMING`` setting is ignored.
159159

160160
Default: ``None``
161161

graphene_django/converter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ def generate_enum_name(django_model_meta, field):
7979
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
8080
)
8181
name = custom_func(field)
82-
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
82+
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V2_NAMING is True:
83+
name = to_camel_case("{}_{}".format(django_model_meta.object_name, field.name))
84+
else:
8385
name = "{app_label}{object_name}{field_name}Choices".format(
8486
app_label=to_camel_case(django_model_meta.app_label.title()),
8587
object_name=django_model_meta.object_name,
8688
field_name=to_camel_case(field.name.title()),
8789
)
88-
else:
89-
name = to_camel_case("{}_{}".format(django_model_meta.object_name, field.name))
9090
return name
9191

9292

graphene_django/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
# Max items returned in ConnectionFields / FilterConnectionFields
3636
"RELAY_CONNECTION_MAX_LIMIT": 100,
3737
"CAMELCASE_ERRORS": True,
38-
# Set to True to enable v3 naming convention for choice field Enum's
39-
"DJANGO_CHOICE_FIELD_ENUM_V3_NAMING": False,
38+
# Set to True to enable v2 naming convention for choice field Enum's
39+
"DJANGO_CHOICE_FIELD_ENUM_V2_NAMING": False,
4040
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
4141
}
4242

graphene_django/tests/test_converter.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Meta:
159159

160160
graphene_type = convert_django_field_with_choices(field)
161161
assert isinstance(graphene_type, graphene.Enum)
162-
assert graphene_type._meta.name == "TranslatedModelLanguage"
162+
assert graphene_type._meta.name == "TestTranslatedModelLanguageChoices"
163163
assert graphene_type._meta.enum.__members__["ES"].value == "es"
164164
assert graphene_type._meta.enum.__members__["ES"].description == "Spanish"
165165
assert graphene_type._meta.enum.__members__["EN"].value == "en"
@@ -344,9 +344,8 @@ def test_should_postgres_range_convert_list():
344344
assert field.type.of_type.of_type == graphene.Int
345345

346346

347-
def test_generate_enum_name(graphene_settings):
347+
def test_generate_enum_name():
348348
MockDjangoModelMeta = namedtuple("DjangoMeta", ["app_label", "object_name"])
349-
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = True
350349

351350
# Simple case
352351
field = graphene.Field(graphene.String, name="type")
@@ -362,3 +361,20 @@ def test_generate_enum_name(graphene_settings):
362361
generate_enum_name(model_meta, field)
363362
== "SomeLongAppNameSomeObjectFizzBuzzChoices"
364363
)
364+
365+
366+
def test_generate_v2_enum_name(graphene_settings):
367+
MockDjangoModelMeta = namedtuple("DjangoMeta", ["app_label", "object_name"])
368+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V2_NAMING = True
369+
370+
# Simple case
371+
field = graphene.Field(graphene.String, name="type")
372+
model_meta = MockDjangoModelMeta(app_label="users", object_name="User")
373+
assert generate_enum_name(model_meta, field) == "UserType"
374+
375+
# More complicated multiple work case
376+
field = graphene.Field(graphene.String, name="fizz_buzz")
377+
model_meta = MockDjangoModelMeta(
378+
app_label="some_long_app_name", object_name="SomeObject"
379+
)
380+
assert generate_enum_name(model_meta, field) == "SomeObjectFizzBuzz"

graphene_django/tests/test_types.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ def test_schema_representation():
128128
editor: Reporter!
129129
130130
\"""Language\"""
131-
lang: ArticleLang!
132-
importance: ArticleImportance
131+
lang: TestsArticleLangChoices!
132+
importance: TestsArticleImportanceChoices
133133
}
134134
135135
\"""An object with an ID\"""
@@ -153,7 +153,7 @@ def test_schema_representation():
153153
scalar DateTime
154154
155155
\"""An enumeration.\"""
156-
enum ArticleLang {
156+
enum TestsArticleLangChoices {
157157
\"""Spanish\"""
158158
ES
159159
@@ -162,7 +162,7 @@ def test_schema_representation():
162162
}
163163
164164
\"""An enumeration.\"""
165-
enum ArticleImportance {
165+
enum TestsArticleImportanceChoices {
166166
\"""Very important\"""
167167
A_1
168168
@@ -177,13 +177,13 @@ def test_schema_representation():
177177
lastName: String!
178178
email: String!
179179
pets: [Reporter!]!
180-
aChoice: ReporterAChoice
181-
reporterType: ReporterReporterType
180+
aChoice: TestsReporterAChoiceChoices
181+
reporterType: TestsReporterReporterTypeChoices
182182
articles(before: String = null, after: String = null, first: Int = null, last: Int = null): ArticleConnection!
183183
}
184184
185185
\"""An enumeration.\"""
186-
enum ReporterAChoice {
186+
enum TestsReporterAChoiceChoices {
187187
\"""this\"""
188188
A_1
189189
@@ -192,7 +192,7 @@ def test_schema_representation():
192192
}
193193
194194
\"""An enumeration.\"""
195-
enum ReporterReporterType {
195+
enum TestsReporterReporterTypeChoices {
196196
\"""Regular\"""
197197
A_1
198198
@@ -512,12 +512,12 @@ class Query(ObjectType):
512512
513513
type Pet {
514514
id: ID!
515-
kind: PetModelKind!
515+
kind: TestsPetModelKindChoices!
516516
cuteness: Int!
517517
}
518518
519519
\"""An enumeration.\"""
520-
enum PetModelKind {
520+
enum TestsPetModelKindChoices {
521521
\"""Cat\"""
522522
CAT
523523
@@ -555,8 +555,6 @@ class Query(ObjectType):
555555
def test_django_objecttype_convert_choices_enum_naming_collisions(
556556
self, PetModel, graphene_settings
557557
):
558-
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = True
559-
560558
class PetModelKind(DjangoObjectType):
561559
class Meta:
562560
model = PetModel

0 commit comments

Comments
 (0)