mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-27 03:53:56 +03:00
Merge pull request #190 from graphql-python/fix_lazy_translated_choice
Fix lazy translated choice
This commit is contained in:
commit
6eb00083d9
|
@ -1,4 +1,5 @@
|
|||
from django.db import models
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from ...core.classtypes.enum import Enum
|
||||
from ...core.types.custom_scalars import DateTime, JSONString
|
||||
|
@ -14,13 +15,14 @@ singledispatch = import_single_dispatch()
|
|||
|
||||
def convert_choices(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):
|
||||
choices = getattr(field, 'choices', None)
|
||||
if choices:
|
||||
meta = field.model._meta
|
||||
model = getattr(field, 'model', None)
|
||||
if choices and model:
|
||||
meta = model._meta
|
||||
name = '{}_{}_{}'.format(meta.app_label, meta.object_name, field.name)
|
||||
return Enum(name.upper(), list(convert_choices(choices)), description=field.help_text)
|
||||
return convert_django_field(field)
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
CHOICES = (
|
||||
(1, 'this'),
|
||||
(2, _('that'))
|
||||
)
|
||||
|
||||
|
||||
class Pet(models.Model):
|
||||
|
@ -22,6 +28,7 @@ class Reporter(models.Model):
|
|||
last_name = models.CharField(max_length=30)
|
||||
email = models.EmailField()
|
||||
pets = models.ManyToManyField('self')
|
||||
a_choice = models.CharField(max_length=30, choices=CHOICES)
|
||||
|
||||
def __str__(self): # __unicode__ on Python 2
|
||||
return "%s %s" % (self.first_name, self.last_name)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import pytest
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from py.test import raises
|
||||
|
||||
import graphene
|
||||
|
@ -117,6 +118,21 @@ def test_field_with_choices_convert_enum():
|
|||
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():
|
||||
assert_conversion(models.FloatField, graphene.Float)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user