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): ...@@ -201,6 +201,7 @@ class TestContentTranslation(ERP5TypeTestCase):
self.assert_(not person.hasJaKanaTranslatedFirstName()) self.assert_(not person.hasJaKanaTranslatedFirstName())
# if there is no translation, original value is returned. # 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', person.getTranslatedFirstName())
self.assertEqual('Yusei Tahara', person.getTranslatedTitle()) self.assertEqual('Yusei Tahara', person.getTranslatedTitle())
self.assertEqual('Yusei', person.getJaKanaTranslatedFirstName()) self.assertEqual('Yusei', person.getJaKanaTranslatedFirstName())
...@@ -208,9 +209,16 @@ class TestContentTranslation(ERP5TypeTestCase): ...@@ -208,9 +209,16 @@ class TestContentTranslation(ERP5TypeTestCase):
self.assertEqual('', person.getTranslatedFirstName(no_original_value=True)) self.assertEqual('', person.getTranslatedFirstName(no_original_value=True))
self.assertEqual('', person.getTranslatedTitle(no_original_value=True)) self.assertEqual('', person.getTranslatedTitle(no_original_value=True))
self.assertEqual('', person.getJaKanaTranslatedFirstName(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.setJaKanaTranslatedFirstName('タハラ')
person.setJaKanaTranslatedLastName('ユウセイ') 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()) self.assert_(person.hasJaKanaTranslatedFirstName())
......
...@@ -41,13 +41,19 @@ class PropertyTranslatableBuiltInDictMixIn: ...@@ -41,13 +41,19 @@ class PropertyTranslatableBuiltInDictMixIn:
security = ClassSecurityInfo() 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: try:
return getattr(self, INTERNAL_TRANSLATION_DICT_NAME) return getattr(self, INTERNAL_TRANSLATION_DICT_NAME)
except AttributeError: except AttributeError:
dict_ = {} dict_ = {}
setattr(self, INTERNAL_TRANSLATION_DICT_NAME, dict_) if create_if_missing:
self._p_changed = True setattr(self, INTERNAL_TRANSLATION_DICT_NAME, dict_)
self._p_changed = True
return dict_ return dict_
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
...@@ -58,7 +64,8 @@ class PropertyTranslatableBuiltInDictMixIn: ...@@ -58,7 +64,8 @@ class PropertyTranslatableBuiltInDictMixIn:
security.declareProtected(Permissions.ModifyPortalContent, security.declareProtected(Permissions.ModifyPortalContent,
'setPropertyTranslation') 'setPropertyTranslation')
def setPropertyTranslation(self, property_id, language, original_text, translation): 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 self._p_changed = True
security.declareProtected(Permissions.ModifyPortalContent, 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