From 8744f6a90ea1861e001cc60e3f991677b42b5255 Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Thu, 26 May 2016 15:44:49 +0100 Subject: [PATCH 1/4] Add test for django choices with translation --- graphene/contrib/django/tests/test_converter.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/graphene/contrib/django/tests/test_converter.py b/graphene/contrib/django/tests/test_converter.py index f5d98e63..32855599 100644 --- a/graphene/contrib/django/tests/test_converter.py +++ b/graphene/contrib/django/tests/test_converter.py @@ -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 TranslatedModel(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) From be449ab1c02f9d7256941b40c6733a344a742ada Mon Sep 17 00:00:00 2001 From: Jacob Klapwijk Date: Tue, 31 May 2016 15:56:33 +0200 Subject: [PATCH 2/4] Fix ugettext_lazy objects in Choice tuples not being evaluated. Running .format() on it will return the string we want, and wont cause any problems when its run on a string without arguments --- graphene/contrib/django/tests/models.py | 7 +++++++ graphene/utils/str_converters.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/graphene/contrib/django/tests/models.py b/graphene/contrib/django/tests/models.py index 634aab24..a0559126 100644 --- a/graphene/contrib/django/tests/models.py +++ b/graphene/contrib/django/tests/models.py @@ -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) diff --git a/graphene/utils/str_converters.py b/graphene/utils/str_converters.py index ae8ceffe..d3a58e6c 100644 --- a/graphene/utils/str_converters.py +++ b/graphene/utils/str_converters.py @@ -18,4 +18,4 @@ def to_snake_case(name): def to_const(string): - return re.sub('[\W|^]+', '_', string).upper() + return re.sub('[\W|^]+', '_', string.format()).upper() From 7f6598518e3704f256d966bcd4b136a109066216 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 31 May 2016 19:47:50 -0700 Subject: [PATCH 3/4] Use Django forcetext for choices --- graphene/contrib/django/converter.py | 3 ++- graphene/utils/str_converters.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/graphene/contrib/django/converter.py b/graphene/contrib/django/converter.py index 65ad8237..951af2ad 100644 --- a/graphene/contrib/django/converter.py +++ b/graphene/contrib/django/converter.py @@ -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,7 +15,7 @@ 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): diff --git a/graphene/utils/str_converters.py b/graphene/utils/str_converters.py index d3a58e6c..ae8ceffe 100644 --- a/graphene/utils/str_converters.py +++ b/graphene/utils/str_converters.py @@ -18,4 +18,4 @@ def to_snake_case(name): def to_const(string): - return re.sub('[\W|^]+', '_', string.format()).upper() + return re.sub('[\W|^]+', '_', string).upper() From edfbbf52ef5745c409def954af6b520ac6a73506 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 31 May 2016 20:00:31 -0700 Subject: [PATCH 4/4] Fix field model --- graphene/contrib/django/converter.py | 5 +++-- graphene/contrib/django/tests/test_converter.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/graphene/contrib/django/converter.py b/graphene/contrib/django/converter.py index 951af2ad..24f035eb 100644 --- a/graphene/contrib/django/converter.py +++ b/graphene/contrib/django/converter.py @@ -20,8 +20,9 @@ def convert_choices(choices): 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) diff --git a/graphene/contrib/django/tests/test_converter.py b/graphene/contrib/django/tests/test_converter.py index 32855599..978bf32d 100644 --- a/graphene/contrib/django/tests/test_converter.py +++ b/graphene/contrib/django/tests/test_converter.py @@ -124,7 +124,7 @@ def test_field_with_choices_gettext(): ('en', _('English')) )) - class TranslatedModel(models.Model): + class TranslatedChoicesModel(models.Model): language = field class Meta: