Skip to content

Move to_const function from Graphene into Graphene-Django (#992) #996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions graphene_django/utils/str_converters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import re
from unidecode import unidecode


def to_const(string):
return re.sub(r"[\W|^]+", "_", unidecode(string)).upper()
Empty file.
9 changes: 9 additions & 0 deletions graphene_django/utils/tests/test_str_converters.py
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down