Skip to content

Commit 1bec8e4

Browse files
authored
Move to_const function from Graphene into Graphene-Django (#992)
1 parent 3026181 commit 1bec8e4

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

graphene_django/converter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
Time,
2121
)
2222
from graphene.types.json import JSONString
23-
from graphene.utils.str_converters import to_camel_case, to_const
23+
from graphene.utils.str_converters import to_camel_case
2424
from graphql import assert_valid_name
2525

2626
from .settings import graphene_settings
2727
from .compat import ArrayField, HStoreField, JSONField, RangeField
2828
from .fields import DjangoListField, DjangoConnectionField
2929
from .utils import import_single_dispatch
30+
from .utils.str_converters import to_const
3031

3132
singledispatch = import_single_dispatch()
3233

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import re
2+
from unidecode import unidecode
3+
4+
5+
def to_const(string):
6+
return re.sub(r"[\W|^]+", "_", unidecode(string)).upper()

graphene_django/utils/tests/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# coding: utf-8
2+
from ..str_converters import to_const
3+
4+
5+
def test_to_const():
6+
assert to_const('snakes $1. on a "#plane') == "SNAKES_1_ON_A_PLANE"
7+
8+
9+
def test_to_const_unicode():
10+
assert to_const(u"Skoða þetta unicode stöff") == "SKODA_THETTA_UNICODE_STOFF"

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"Django>=1.11",
6767
"singledispatch>=3.4.0.3",
6868
"promise>=2.1",
69+
"unidecode>=1.1.1,<2",
6970
],
7071
setup_requires=["pytest-runner"],
7172
tests_require=tests_require,

0 commit comments

Comments
 (0)