misc: fix unaccent template filter with non string values (#78103) #418
|
@ -213,12 +213,20 @@ def test_urljoin_templatefilter():
|
|||
assert tmpl.render({'foo': 'http://example.com/', 'bar': '/some/url'}) == 'http://example.com/some/url'
|
||||
|
||||
|
||||
def test_unaccent_templatetag():
|
||||
def test_unaccent_templatetag(pub):
|
||||
tmpl = Template('{{ foo|unaccent }}')
|
||||
assert tmpl.render() == ''
|
||||
assert tmpl.render({'foo': None}) == ''
|
||||
assert tmpl.render({'foo': 'FOO bar'}) == 'FOO bar'
|
||||
assert tmpl.render({'foo': 'félé'}) == 'fele'
|
||||
assert tmpl.render({'foo': 42}) == ''
|
||||
assert pub.loggederror_class.count() == 1
|
||||
logged_error = pub.loggederror_class.select(order_by='id')[0]
|
||||
assert logged_error.summary == "Failed to apply unaccent filter on value (42)"
|
||||
assert tmpl.render({'foo': ['a', 'z']}) == ''
|
||||
assert pub.loggederror_class.count() == 2
|
||||
logged_error = pub.loggederror_class.select(order_by='id')[1]
|
||||
assert logged_error.summary == "Failed to apply unaccent filter on value (['a', 'z'])"
|
||||
|
||||
# lazy mode
|
||||
FormDef.wipe()
|
||||
|
|
|
@ -51,7 +51,7 @@ from django.utils.encoding import force_bytes, force_str
|
|||
from django.utils.safestring import mark_safe
|
||||
from django.utils.timezone import is_naive, make_aware
|
||||
|
||||
from wcs.qommon import calendar, evalutils, upload_storage
|
||||
from wcs.qommon import _, calendar, evalutils, upload_storage
|
||||
from wcs.qommon.admin.texts import TextsDirectory
|
||||
from wcs.qommon.humantime import seconds2humanduration
|
||||
from wcs.qommon.misc import strip_some_tags, validate_phone_fr
|
||||
|
@ -161,6 +161,9 @@ def unaccent(value):
|
|||
value = unlazy(value)
|
||||
if not value:
|
||||
return ''
|
||||
if not isinstance(value, str):
|
||||
get_publisher().record_error(_('Failed to apply unaccent filter on value (%s)') % value)
|
||||
return ''
|
||||
return force_str(unicodedata.normalize('NFKD', value).encode('ascii', 'ignore'))
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue