Force ChoiceBlock.get_searchable_content to text - fixes #2928

This commit is contained in:
Matt Westcott 2016-08-24 15:59:23 +01:00
parent 7bc819640d
commit 9111483dec
4 changed files with 35 additions and 2 deletions

View file

@ -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)

View file

@ -16,6 +16,7 @@ Bug fixes
* Wagtail's middleware classes are now compatible with Django 1.10's `new-style middleware <https://docs.djangoproject.com/en/1.10/releases/1.10/#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

View file

@ -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:

View file

@ -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):