Commit f53e0c2b authored by Sebastien Robin's avatar Sebastien Robin

content_translation: stop setting empty translation dict on object for nothing

Getter of content translations were setting an empty translation dict. So
there was ZODB pollution by doing useless changes.
parent 08048831
......@@ -201,6 +201,7 @@ class TestContentTranslation(ERP5TypeTestCase):
self.assert_(not person.hasJaKanaTranslatedFirstName())
# if there is no translation, original value is returned.
self.assertEqual(None, getattr(person, '__translation_dict', None))
self.assertEqual('Yusei', person.getTranslatedFirstName())
self.assertEqual('Yusei Tahara', person.getTranslatedTitle())
self.assertEqual('Yusei', person.getJaKanaTranslatedFirstName())
......@@ -208,9 +209,16 @@ class TestContentTranslation(ERP5TypeTestCase):
self.assertEqual('', person.getTranslatedFirstName(no_original_value=True))
self.assertEqual('', person.getTranslatedTitle(no_original_value=True))
self.assertEqual('', person.getJaKanaTranslatedFirstName(no_original_value=True))
# Make sure that until any value is set, we do not create useless __translation_dict
self.assertEqual(None, getattr(person, '__translation_dict', None))
person.setJaKanaTranslatedFirstName('タハラ')
person.setJaKanaTranslatedLastName('ユウセイ')
# Since we add some translated value, we now must have some data in __translation_dict
translation_dict = getattr(person, '__translation_dict', None)
self.assertNotEqual(None, translation_dict)
self.assertEqual(set([('last_name', 'ja-kana'), ('first_name', 'ja-kana')]),
set(translation_dict.keys()))
self.assert_(person.hasJaKanaTranslatedFirstName())
......
......@@ -41,11 +41,17 @@ class PropertyTranslatableBuiltInDictMixIn:
security = ClassSecurityInfo()
def _getTranslationDict(self):
def _getTranslationDict(self, create_if_missing=False):
"""
create_if_missing: force creation of translation dict. It is false by
default to havoid zodb pollution when only try to get
translations
"""
try:
return getattr(self, INTERNAL_TRANSLATION_DICT_NAME)
except AttributeError:
dict_ = {}
if create_if_missing:
setattr(self, INTERNAL_TRANSLATION_DICT_NAME, dict_)
self._p_changed = True
return dict_
......@@ -58,7 +64,8 @@ class PropertyTranslatableBuiltInDictMixIn:
security.declareProtected(Permissions.ModifyPortalContent,
'setPropertyTranslation')
def setPropertyTranslation(self, property_id, language, original_text, translation):
self._getTranslationDict()[(property_id, language)] = (original_text, translation)
self._getTranslationDict(create_if_missing=True)[(property_id, language)] = \
(original_text, translation)
self._p_changed = True
security.declareProtected(Permissions.ModifyPortalContent,
......
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