diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 563d973e2..1f8431c53 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -15,6 +15,7 @@ Changelog * Fix: Wagtail's middleware classes are now compatible with Django 1.10's new-style middleware (Karl Hobley) * Fix: The `Pages.can_create_at` method is now checked in the create page view (Mikalai Radchuk) * Fix: Fixed regression on Django 1.10.1 causing Page subclasses to fail to use PageManager (Matt Westcott) + * Fix: ChoiceBlocks with lazy translations as option labels no longer break Elasticsearch indexing (Matt Westcott) 1.6 (15.08.2016) diff --git a/docs/releases/1.6.1.rst b/docs/releases/1.6.1.rst index f81b1b048..4649be2fe 100644 --- a/docs/releases/1.6.1.rst +++ b/docs/releases/1.6.1.rst @@ -16,6 +16,7 @@ Bug fixes * Wagtail's middleware classes are now compatible with Django 1.10's `new-style middleware `_ (Karl Hobley) * The :meth:`~wagtail.wagtailcore.models.Page.can_create_at` method is now checked in the create page view (Mikalai Radchuk) * Fixed regression on Django 1.10.1 causing Page subclasses to fail to use PageManager (Matt Westcott) +* ChoiceBlocks with lazy translations as option labels no longer break Elasticsearch indexing (Matt Westcott) Upgrade considerations diff --git a/wagtail/wagtailcore/blocks/field_block.py b/wagtail/wagtailcore/blocks/field_block.py index cce57eb4e..71af4b90b 100644 --- a/wagtail/wagtailcore/blocks/field_block.py +++ b/wagtail/wagtailcore/blocks/field_block.py @@ -370,10 +370,10 @@ class ChoiceBlock(FieldBlock): # This is an optgroup, so look inside the group for options for k2, v2 in v: if value == k2 or text_value == force_text(k2): - return [k, v2] + return [force_text(k), force_text(v2)] else: if value == k or text_value == force_text(k): - return [v] + return [force_text(v)] return [] # Value was not found in the list of choices class Meta: diff --git a/wagtail/wagtailcore/tests/test_blocks.py b/wagtail/wagtailcore/tests/test_blocks.py index 76f34f631..e9029bdd2 100644 --- a/wagtail/wagtailcore/tests/test_blocks.py +++ b/wagtail/wagtailcore/tests/test_blocks.py @@ -3,10 +3,12 @@ from __future__ import absolute_import, unicode_literals import base64 import collections +import json import unittest import warnings from decimal import Decimal +# non-standard import name for ugettext_lazy, to prevent strings from being picked up for translation from django import forms from django.core.exceptions import ValidationError from django.forms.utils import ErrorList @@ -14,6 +16,7 @@ from django.template.loader import render_to_string from django.test import SimpleTestCase, TestCase from django.utils.html import format_html from django.utils.safestring import SafeData, mark_safe +from django.utils.translation import ugettext_lazy as __ from wagtail.tests.testapp.blocks import LinkBlock as CustomLinkBlock from wagtail.tests.testapp.blocks import SectionBlock @@ -608,6 +611,34 @@ class TestChoiceBlock(unittest.TestCase): ]) self.assertEqual(block.get_searchable_content('three'), []) + def test_searchable_content_with_lazy_translation(self): + block = blocks.ChoiceBlock(choices=[ + ('choice-1', __("Choice 1")), + ('choice-2', __("Choice 2")), + ]) + result = block.get_searchable_content("choice-1") + # result must survive JSON (de)serialisation, which is not the case for + # lazy translation objects + result = json.loads(json.dumps(result)) + self.assertEqual(result, ["Choice 1"]) + + def test_optgroup_searchable_content_with_lazy_translation(self): + block = blocks.ChoiceBlock(choices=[ + (__('Section 1'), [ + ('1-1', __("Block 1")), + ('1-2', __("Block 2")), + ]), + (__('Section 2'), [ + ('2-1', __("Block 1")), + ('2-2', __("Block 2")), + ]), + ]) + result = block.get_searchable_content("2-2") + # result must survive JSON (de)serialisation, which is not the case for + # lazy translation objects + result = json.loads(json.dumps(result)) + self.assertEqual(result, ["Section 2", "Block 2"]) + class TestRawHTMLBlock(unittest.TestCase): def test_get_default_with_fallback_value(self):