We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 13ca2fe + e1a693e commit ae23a11Copy full SHA for ae23a11
graphene/contrib/django/converter.py
@@ -2,18 +2,24 @@
2
3
from ...core.types.scalars import ID, Boolean, Float, Int, String
4
from ...core.classtypes.enum import Enum
5
+from ...utils import to_const
6
from .compat import RelatedObject, UUIDField
7
from .utils import get_related_model, import_single_dispatch
8
9
singledispatch = import_single_dispatch()
10
11
12
+def convert_choices(choices):
13
+ for value, name in choices:
14
+ yield to_const(name), value
15
+
16
17
def convert_django_field_with_choices(field):
18
choices = getattr(field, 'choices', None)
19
if choices:
20
meta = field.model._meta
21
name = '{}_{}_{}'.format(meta.app_label, meta.object_name, field.name)
- return Enum(name.upper(), choices, description=field.help_text)
22
+ return Enum(name.upper(), list(convert_choices(choices)), description=field.help_text)
23
return convert_django_field(field)
24
25
graphene/contrib/django/tests/models.py
@@ -30,6 +30,8 @@ class Article(models.Model):
30
('es', 'Spanish'),
31
('en', 'English')
32
], default='es')
33
+ importance = models.IntegerField('Importance', null=True, blank=True,
34
+ choices=[(1, u'Very important'), (2, u'Not as important')])
35
36
def __str__(self): # __unicode__ on Python 2
37
return self.headline
graphene/contrib/django/tests/test_converter.py
@@ -103,8 +103,8 @@ class Meta:
103
assert issubclass(graphene_type, graphene.Enum)
104
assert graphene_type._meta.type_name == 'TEST_TRANSLATEDMODEL_LANGUAGE'
105
assert graphene_type._meta.description == 'Language'
106
- assert graphene_type.__enum__.__members__['es'].value == 'Spanish'
107
- assert graphene_type.__enum__.__members__['en'].value == 'English'
+ assert graphene_type.__enum__.__members__['SPANISH'].value == 'es'
+ assert graphene_type.__enum__.__members__['ENGLISH'].value == 'en'
108
109
110
def test_should_float_convert_float():
graphene/core/classtypes/enum.py
@@ -2,6 +2,7 @@
from graphql.core.type import GraphQLEnumType, GraphQLEnumValue
from .base import ClassTypeMeta, ClassType
+from ..types.base import MountedType
from ...utils.enum import Enum as PyEnum
@@ -17,7 +18,12 @@ def construct(cls, bases, attrs):
attrs[k] = v.value
return super(EnumMeta, cls).construct(bases, attrs)
- def __call__(cls, name, names=None, description=None):
+ def __call__(cls, *args, **kwargs):
+ if cls is Enum:
+ return cls.create_enum(*args, **kwargs)
+ return super(EnumMeta, cls).__call__(*args, **kwargs)
26
+ def create_enum(cls, name, names=None, description=None):
27
attrs = {
28
'__enum__': PyEnum(name, names)
29
}
@@ -26,7 +32,7 @@ def __call__(cls, name, names=None, description=None):
return type(name, (Enum,), attrs)
-class Enum(six.with_metaclass(EnumMeta, ClassType)):
+class Enum(six.with_metaclass(EnumMeta, ClassType, MountedType)):
class Meta:
38
abstract = True
graphene/core/classtypes/tests/test_enum.py
@@ -3,6 +3,7 @@
from graphene.core.schema import Schema
from ..enum import Enum
+from ..objecttype import ObjectType
def test_enum():
@@ -35,3 +36,14 @@ def test_enum_values():
assert RGB.RED == 0
assert RGB.GREEN == 1
assert RGB.BLUE == 2
39
40
41
+def test_enum_instance():
42
+ RGB = Enum('RGB', dict(RED=0, GREEN=1, BLUE=2))
43
+ RGB_field = RGB(description='RGB enum description')
44
45
+ class ObjectWithColor(ObjectType):
46
+ color = RGB_field
47
48
+ object_field = ObjectWithColor._meta.fields_map['color']
49
+ assert object_field.description == 'RGB enum description'
graphene/utils/__init__.py
@@ -1,4 +1,4 @@
1
-from .str_converters import to_camel_case, to_snake_case
+from .str_converters import to_camel_case, to_snake_case, to_const
from .proxy_snake_dict import ProxySnakeDict
from .caching import cached_property, memoize
from .maybe_func import maybe_func
@@ -7,6 +7,6 @@
from .lazylist import LazyList
-__all__ = ['to_camel_case', 'to_snake_case', 'ProxySnakeDict',
+__all__ = ['to_camel_case', 'to_snake_case', 'to_const', 'ProxySnakeDict',
'cached_property', 'memoize', 'maybe_func', 'enum_to_graphql_enum',
'resolve_only_args', 'LazyList']
graphene/utils/str_converters.py
@@ -15,3 +15,7 @@ def to_camel_case(snake_str):
def to_snake_case(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
+def to_const(string):
+ return re.sub('[\W|^(?=\d)]+', '_', string).upper()
graphene/utils/tests/test_str_converter.py
@@ -1,4 +1,5 @@
-from ..str_converters import to_camel_case, to_snake_case
+# coding: utf-8
+from ..str_converters import to_camel_case, to_snake_case, to_const
def test_snake_case():
@@ -15,3 +16,7 @@ def test_camel_case():
assert to_camel_case('snakes_on_a_plane') == 'snakesOnAPlane'
assert to_camel_case('snakes_on_a__plane') == 'snakesOnA_Plane'
assert to_camel_case('i_phone_hysteria') == 'iPhoneHysteria'
+def test_to_const():
+ assert to_const('snakes $1. on a "#plane') == 'SNAKES_ON_A_PLANE'
0 commit comments