More informative error for invalid values passed to `|richtext` (#4971)

Fixes #4958
pull/4987/head
Mukesh 2018-12-30 12:02:28 +05:30 zatwierdzone przez Matt Westcott
rodzic 5f0fa624f9
commit fa5deb6b44
5 zmienionych plików z 16 dodań i 1 usunięć

Wyświetl plik

@ -4,6 +4,7 @@ Changelog
2.5 (xx.xx.xxxx) - IN DEVELOPMENT
~~~~~~~~~~~~~~~~
* Added more informative error message when `|richtext` filter is applied to a non-string value (mukesh5)
* Fix: Set `SERVER_PORT` to 443 in `Page.dummy_request()` for HTTPS sites (Sergey Fedoseev)
* Fix: Validation error messages in `InlinePanel` no longer count towards `max_num` when disabling the 'add' button (Todd Dembrey, Thibaud Colas)

Wyświetl plik

@ -337,6 +337,7 @@ Contributors
* Nick Travis
* Maylon Pedroso
* Thijs Walcarius
* mukesh5
Translators
===========

Wyświetl plik

@ -14,6 +14,8 @@ What's new
Other features
~~~~~~~~~~~~~~
* Added more informative error message when ``|richtext`` filter is applied to a non-string value (mukesh5)
Bug fixes
~~~~~~~~~

Wyświetl plik

@ -87,7 +87,10 @@ def richtext(value):
elif value is None:
html = ''
else:
html = expand_db_html(value)
if isinstance(value, str):
html = expand_db_html(value)
else:
raise TypeError("'richtext' template filter received an invalid value; expected string, got {}.".format(type(value)))
return mark_safe('<div class="rich-text">' + html + '</div>')

Wyświetl plik

@ -261,3 +261,11 @@ class TestRichtextTag(TestCase):
def test_call_with_none(self):
result = richtext(None)
self.assertEqual(result, '<div class="rich-text"></div>')
def test_call_with_invalid_value(self):
with self.assertRaisesRegex(TypeError, "'richtext' template filter received an invalid value"):
richtext(42)
def test_call_with_bytes(self):
with self.assertRaisesRegex(TypeError, "'richtext' template filter received an invalid value"):
richtext(b"Hello world!")