From cfa7db9abb47d75bbf9490320eeef0d159ab434a Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Thu, 25 Jun 2020 15:11:18 +0100 Subject: [PATCH] Move to_const function from Graphene into Graphene-Django (#992) --- graphene_django/converter.py | 3 ++- graphene_django/utils/str_converters.py | 6 ++++++ graphene_django/utils/tests/__init__.py | 0 graphene_django/utils/tests/test_str_converters.py | 9 +++++++++ setup.py | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 graphene_django/utils/str_converters.py create mode 100644 graphene_django/utils/tests/__init__.py create mode 100644 graphene_django/utils/tests/test_str_converters.py diff --git a/graphene_django/converter.py b/graphene_django/converter.py index 53b25374f..1cdc8cc03 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -22,13 +22,14 @@ Time, ) from graphene.types.json import JSONString -from graphene.utils.str_converters import to_camel_case, to_const +from graphene.utils.str_converters import to_camel_case from graphql import GraphQLError, assert_valid_name from graphql.pyutils import register_description from .compat import ArrayField, HStoreField, JSONField, RangeField from .fields import DjangoConnectionField, DjangoListField from .settings import graphene_settings +from .utils.str_converters import to_const def convert_choice_name(name): diff --git a/graphene_django/utils/str_converters.py b/graphene_django/utils/str_converters.py new file mode 100644 index 000000000..f41e87a10 --- /dev/null +++ b/graphene_django/utils/str_converters.py @@ -0,0 +1,6 @@ +import re +from unidecode import unidecode + + +def to_const(string): + return re.sub(r"[\W|^]+", "_", unidecode(string)).upper() diff --git a/graphene_django/utils/tests/__init__.py b/graphene_django/utils/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/graphene_django/utils/tests/test_str_converters.py b/graphene_django/utils/tests/test_str_converters.py new file mode 100644 index 000000000..6460c4e23 --- /dev/null +++ b/graphene_django/utils/tests/test_str_converters.py @@ -0,0 +1,9 @@ +from ..str_converters import to_const + + +def test_to_const(): + assert to_const('snakes $1. on a "#plane') == "SNAKES_1_ON_A_PLANE" + + +def test_to_const_unicode(): + assert to_const(u"Skoða þetta unicode stöff") == "SKODA_THETTA_UNICODE_STOFF" diff --git a/setup.py b/setup.py index 980871a6b..5e90b7e70 100644 --- a/setup.py +++ b/setup.py @@ -58,6 +58,7 @@ "graphql-core>=3.1.0,<4", "Django>=2.2", "promise>=2.1", + "unidecode>=1.1.1,<2", ], setup_requires=["pytest-runner"], tests_require=tests_require,