Import function through import_string function

This commit is contained in:
Jonathan Kim 2020-02-23 09:04:57 +00:00
parent d0e3a04ac7
commit 9cff36573c
3 changed files with 19 additions and 12 deletions

View File

@ -153,7 +153,7 @@ Default: ``False``
``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME`` ``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME``
-------------------------------------- --------------------------------------
Set to a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type. 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.
If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` setting is ignored. If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` setting is ignored.
@ -161,11 +161,12 @@ Default: ``None``
.. code:: python .. code:: python
# myapp.utils
def enum_naming(field): def enum_naming(field):
if isinstance(field.model, User): if isinstance(field.model, User):
return f"CustomUserEnum{field.name.title()}" return f"CustomUserEnum{field.name.title()}"
return f"CustomEnum{field.name.title()}" return f"CustomEnum{field.name.title()}"
GRAPHENE = { GRAPHENE = {
'DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME': enum_naming 'DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME': "myapp.utils.enum_naming"
} }

View File

@ -1,6 +1,7 @@
from collections import OrderedDict from collections import OrderedDict
from django.db import models from django.db import models
from django.utils.encoding import force_str from django.utils.encoding import force_str
from django.utils.module_loading import import_string
from graphene import ( from graphene import (
ID, ID,
@ -70,10 +71,12 @@ def convert_choices_to_named_enum_with_descriptions(name, choices):
def generate_enum_name(django_model_meta, field): def generate_enum_name(django_model_meta, field):
if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME and callable( if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME:
# Try and import custom function
custom_func = import_string(
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
): )
name = graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME(field) name = custom_func(field)
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True: elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
name = "{app_label}{object_name}{field_name}Choices".format( name = "{app_label}{object_name}{field_name}Choices".format(
app_label=to_camel_case(django_model_meta.app_label.title()), app_label=to_camel_case(django_model_meta.app_label.title()),

View File

@ -388,6 +388,10 @@ def test_django_objecttype_exclude_fields_exist_on_model():
assert len(record) == 0 assert len(record) == 0
def custom_enum_name(field):
return "CustomEnum{}".format(field.name.title())
class TestDjangoObjectType: class TestDjangoObjectType:
@pytest.fixture @pytest.fixture
def PetModel(self): def PetModel(self):
@ -532,10 +536,9 @@ class TestDjangoObjectType:
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False
def test_django_objecttype_choices_custom_enum_name(self, PetModel): def test_django_objecttype_choices_custom_enum_name(self, PetModel):
def custom_name(field): graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = (
return "CustomEnum{}".format(field.name.title()) "graphene_django.tests.test_types.custom_enum_name"
)
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = custom_name
class PetModelKind(DjangoObjectType): class PetModelKind(DjangoObjectType):
class Meta: class Meta:
@ -553,14 +556,14 @@ class TestDjangoObjectType:
query: Query query: Query
} }
enum DjangoModelTestsPetModelKindChoices { enum CustomEnumKind {
CAT CAT
DOG DOG
} }
type PetModelKind { type PetModelKind {
id: ID! id: ID!
kind: DjangoModelTestsPetModelKindChoices! kind: CustomEnumKind!
} }
type Query { type Query {