Skip to content

Commit

Permalink
Test that we allow lazily translated strings for display values
Browse files Browse the repository at this point in the history
  • Loading branch information
danpalmer authored and lamby committed Jun 21, 2019
1 parent ba77822 commit b73e2e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
13 changes: 10 additions & 3 deletions django_enumfield/item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import six
import functools

from .utils import is_lazy_translation
from .app_settings import app_settings


Expand Down Expand Up @@ -34,9 +35,15 @@ def __init__(self, value, slug, display=None):
if not isinstance(slug, str):
raise TypeError("item slug should be a str, not %r" % type(slug))

if display is not None and not isinstance(display, six.string_types):
raise TypeError("item display name should be a basestring, not %r" \
% type(display))
if (
display is not None and
not isinstance(display, six.string_types) and
not is_lazy_translation(display)
):
raise TypeError(
"item display name should be a string or lazily evaluated "
"string, not %r" % type(display),
)

self.value = value
self.slug = slug
Expand Down
14 changes: 14 additions & 0 deletions django_enumfield/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import six

from django.http import Http404
from django.utils.functional import Promise

def get_enum_or_404(enum, slug):
try:
Expand All @@ -25,3 +28,14 @@ def __getitem__(self, key):

class TemplateErrorException(RuntimeError):
silent_variable_failure = False

def is_lazy_translation(obj):
# There's no public API to figure out the type of a "Promise"/"__proxy__"
# object, so we look at whether the object has a string type in its set
# of resultclasses. We do this so that we don't have to force the lazy
# object as that may be expensive and we're likely working at import time.
if not isinstance(obj, Promise):
return False

resultclasses = obj.__reduce__()[1][3:]
return any(issubclass(x, six.string_types) for x in resultclasses)
5 changes: 5 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.template import RequestContext
from django.template.loader import render_to_string
from django.db.models.fields import NOT_PROVIDED
from django.utils.translation import gettext_lazy as _

from django_enumfield import Enum, Item, get_enum_or_404
from django_enumfield.utils import TemplateErrorException
Expand Down Expand Up @@ -68,6 +69,10 @@ def test_comparison(self):
self.assertGreater(item2, item1)
self.assertGreaterEqual(item2, item2_copy)
self.assertLessEqual(item2, item2_copy)

def test_lazy_translation_in_display(self):
item = Item(10, 'slug', _("Display"))
self.assertEqual(item.display, "Display")


class EnumConstructionTests(unittest.TestCase):
Expand Down

0 comments on commit b73e2e8

Please sign in to comment.