Merge pull request #190 from graphql-python/fix_lazy_translated_choice

Fix lazy translated choice
This commit is contained in:
Syrus Akbary 2016-05-31 20:15:15 -07:00
commit 6eb00083d9
3 changed files with 28 additions and 3 deletions

View File

@ -1,4 +1,5 @@
from django.db import models from django.db import models
from django.utils.encoding import force_text
from ...core.classtypes.enum import Enum from ...core.classtypes.enum import Enum
from ...core.types.custom_scalars import DateTime, JSONString from ...core.types.custom_scalars import DateTime, JSONString
@ -14,13 +15,14 @@ singledispatch = import_single_dispatch()
def convert_choices(choices): def convert_choices(choices):
for value, name in choices: for value, name in choices:
yield to_const(name), value yield to_const(force_text(name)), value
def convert_django_field_with_choices(field): def convert_django_field_with_choices(field):
choices = getattr(field, 'choices', None) choices = getattr(field, 'choices', None)
if choices: model = getattr(field, 'model', None)
meta = field.model._meta if choices and model:
meta = model._meta
name = '{}_{}_{}'.format(meta.app_label, meta.object_name, field.name) name = '{}_{}_{}'.format(meta.app_label, meta.object_name, field.name)
return Enum(name.upper(), list(convert_choices(choices)), description=field.help_text) return Enum(name.upper(), list(convert_choices(choices)), description=field.help_text)
return convert_django_field(field) return convert_django_field(field)

View File

@ -1,6 +1,12 @@
from __future__ import absolute_import from __future__ import absolute_import
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _
CHOICES = (
(1, 'this'),
(2, _('that'))
)
class Pet(models.Model): class Pet(models.Model):
@ -22,6 +28,7 @@ class Reporter(models.Model):
last_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30)
email = models.EmailField() email = models.EmailField()
pets = models.ManyToManyField('self') pets = models.ManyToManyField('self')
a_choice = models.CharField(max_length=30, choices=CHOICES)
def __str__(self): # __unicode__ on Python 2 def __str__(self): # __unicode__ on Python 2
return "%s %s" % (self.first_name, self.last_name) return "%s %s" % (self.first_name, self.last_name)

View File

@ -1,5 +1,6 @@
import pytest import pytest
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _
from py.test import raises from py.test import raises
import graphene import graphene
@ -117,6 +118,21 @@ def test_field_with_choices_convert_enum():
assert graphene_type.__enum__.__members__['ENGLISH'].value == 'en' assert graphene_type.__enum__.__members__['ENGLISH'].value == 'en'
def test_field_with_choices_gettext():
field = models.CharField(help_text='Language', choices=(
('es', _('Spanish')),
('en', _('English'))
))
class TranslatedChoicesModel(models.Model):
language = field
class Meta:
app_label = 'test'
convert_django_field_with_choices(field)
def test_should_float_convert_float(): def test_should_float_convert_float():
assert_conversion(models.FloatField, graphene.Float) assert_conversion(models.FloatField, graphene.Float)