Commit 941eba51 authored by Jérome Perrin's avatar Jérome Perrin

Accessor: adjust TranslatedPropertyGetter to use unicode2str

partially revert a17bb910 (py2/py3: Make Products code compatible
with both python2 and python3., 2022-04-13) so that
TranslatedPropertyGetter keeps returning utf-8 encoded str (bytes) on
python2 and returns text only on python3.

This regression caused displaying categories to raise unicode error or
to silently not display the labels for.
parent 8ac0c975
# coding:utf-8
############################################################################## ##############################################################################
# #
# Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved. # Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved.
...@@ -26,6 +27,7 @@ ...@@ -26,6 +27,7 @@
# #
############################################################################## ##############################################################################
import mock
from collections import deque from collections import deque
import unittest import unittest
...@@ -756,6 +758,60 @@ class TestCMFCategory(ERP5TypeTestCase): ...@@ -756,6 +758,60 @@ class TestCMFCategory(ERP5TypeTestCase):
whitespace_number = self.portal.portal_preferences.getPreferredWhitespaceNumberForChildItemIndentation() whitespace_number = self.portal.portal_preferences.getPreferredWhitespaceNumberForChildItemIndentation()
self.assertEqual(NBSP_UTF8 * whitespace_number + 'The Sub Title', sub_cat.getIndentedTitle()) self.assertEqual(NBSP_UTF8 * whitespace_number + 'The Sub Title', sub_cat.getIndentedTitle())
def test_getCategoryChildTranslatedXItemList(self):
base_cat = self.getCategoryTool().newContent(portal_type='Base Category')
cat = base_cat.newContent(
portal_type='Category', id='the_id', title='The Title')
cat.newContent(
portal_type='Category', id='the_sub_id', title='The Sub Title')
default_gettext = self.portal.Localizer.erp5_content.gettext
def gettext(message, **kw):
if message == 'The Sub Title':
return u'The Süb Tïtle'
assert message != u'The Süb Tïtle'
return default_gettext(message, **kw)
with mock.patch.object(
self.portal.Localizer.erp5_content.__class__,
'gettext',
side_effect=gettext), \
mock.patch.object(
self.portal.portal_preferences.__class__,
'getPreferredWhitespaceNumberForChildItemIndentation',
return_value=2):
self.assertEqual(
base_cat.getCategoryChildIndentedTitleItemList(),
[['', ''],
['The Title', 'the_id'],
['\xc2\xa0\xc2\xa0The Sub Title', 'the_id/the_sub_id']],
)
self.assertEqual(
base_cat.getCategoryChildTranslatedIndentedTitleItemList(),
[['', ''],
['The Title', 'the_id'],
['\xc2\xa0\xc2\xa0The S\xc3\xbcb T\xc3\xaftle', 'the_id/the_sub_id']],
)
self.assertEqual(
base_cat.getCategoryChildTranslatedCompactTitleItemList(),
[['', ''],
['The Title', 'the_id'],
['The S\xc3\xbcb T\xc3\xaftle', 'the_id/the_sub_id']],
)
self.assertEqual(
base_cat.getCategoryChildTranslatedLogicalPathItemList(),
[['', ''],
['The Title', 'the_id'],
['The Title/The S\xc3\xbcb T\xc3\xaftle', 'the_id/the_sub_id']],
)
self.assertEqual(
base_cat.getCategoryChildTranslatedCompactLogicalPathItemList(),
[['', ''],
['The Title', 'the_id'],
['The Title/The S\xc3\xbcb T\xc3\xaftle', 'the_id/the_sub_id']],
)
def test_CategoryChildTitleItemListFilterNodeFilterLeave(self): def test_CategoryChildTitleItemListFilterNodeFilterLeave(self):
base_cat = self.getCategoryTool().newContent(portal_type='Base Category') base_cat = self.getCategoryTool().newContent(portal_type='Base Category')
base_cat.newContent( base_cat.newContent(
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
# #
############################################################################## ##############################################################################
from six import ensure_text
from zLOG import LOG from zLOG import LOG
from Products.ERP5Type.PsycoWrapper import psyco from Products.ERP5Type.PsycoWrapper import psyco
from Acquisition import aq_base from Acquisition import aq_base
...@@ -33,6 +34,7 @@ from Acquisition import aq_base ...@@ -33,6 +34,7 @@ from Acquisition import aq_base
from Products.ERP5Type.Accessor.Base import func_code, ATTRIBUTE_PREFIX, evaluateTales, Getter as BaseGetter, Method from Products.ERP5Type.Accessor.Base import func_code, ATTRIBUTE_PREFIX, evaluateTales, Getter as BaseGetter, Method
from Products.ERP5Type.Accessor import Accessor, AcquiredProperty from Products.ERP5Type.Accessor import Accessor, AcquiredProperty
from Products.ERP5Type.Accessor.TypeDefinition import type_definition from Products.ERP5Type.Accessor.TypeDefinition import type_definition
from Products.ERP5Type.Utils import unicode2str
TRANSLATION_DOMAIN_CONTENT_TRANSLATION = 'content_translation' TRANSLATION_DOMAIN_CONTENT_TRANSLATION = 'content_translation'
...@@ -85,8 +87,8 @@ class TranslatedPropertyGetter(BaseGetter): ...@@ -85,8 +87,8 @@ class TranslatedPropertyGetter(BaseGetter):
localizer = instance.getPortalObject().Localizer localizer = instance.getPortalObject().Localizer
message_catalog = getattr(localizer, domain, None) message_catalog = getattr(localizer, domain, None)
if message_catalog is not None: if message_catalog is not None:
return message_catalog.gettext(value, lang=self._language) return unicode2str(
else: message_catalog.gettext(ensure_text(value), lang=self._language))
return value return value
psyco.bind(__call__) psyco.bind(__call__)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment