Commit c547c545 authored by Jérome Perrin's avatar Jérome Perrin

web_renderjs_ui: update translation data in post-upgarde

Automate the process of running "Update Translation Data" in
post-upgarde step when the translation data has changed.

This means that translation scripts (such as gadget_translation_data.js
for default ERP5JS gadget) will be updated automatically after each
update and it becomes wrong to have multiple sites using the same
translation gadget for different translations.

If that happens, the upgrader tries to detect a loop and raise an
easy-to-understand error in that case
parent b254edbe
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Script Constraint" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_identity_criterion</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>_range_criterion</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
<item>
<key> <string>categories</string> </key>
<value>
<tuple>
<string>constraint_type/post_upgrade</string>
</tuple>
</value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>translation_signature_constraint</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Script Constraint</string> </value>
</item>
<item>
<key> <string>script_id</string> </key>
<value> <string>WebSite_checkTranslationConsistency</string> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
"""Check that RenderJS translation gadget data is up to date.
This compare the current content of the translation gadget data against what
the content would be if "Update Translation Data" were used. If the scripts
are different, update the translation data to fix.
"""
from collections import defaultdict
if not context.getAvailableLanguageList():
return []
if context.getSkinSelectionName() != 'RJS':
return []
# find the .js containing translation data
gadget_translation_data_js = context.WebSite_getTranslationDataWebScriptValue()
if gadget_translation_data_js is None:
return []
error_list = []
if context.WebSite_getTranslationDataTextContent(
) != gadget_translation_data_js.getTextContent():
error_list.append("Translation data script content is not up to date")
if fixit:
# try to detect the case of two incompatible web sites configured for the same translation gadget.
# Use a mapping of set of web site ids keyed by translation data script reference and check
# if we update the same translation data script more than once in the same REQUEST.
# Using REQUEST is not really good, since upgrader uses grouped activities and we can just check
# web sites processed in the same activity group, but that's easy and hopefully better than nothing.
already_updated_websites = container.REQUEST.get(
script.getId(), defaultdict(set))
container.REQUEST.set(script.getId(), already_updated_websites)
gadget_translation_data_js_reference = gadget_translation_data_js.getReference()
already_updated_websites[gadget_translation_data_js_reference].add(context.getId())
if len(already_updated_websites[gadget_translation_data_js_reference]) > 1:
raise RuntimeError(
"Translation script %s is used by more than one web site with different configurations (%s)"
% (
gadget_translation_data_js_reference,
", ".join(already_updated_websites[gadget_translation_data_js_reference]),
))
context.WebSite_updateTranslationData()
# since we might have modified some cached files, check again the modification date
# consistency.
error_list.extend(
context.WebSite_checkCacheModificationDateConsistency(fixit=True))
return error_list
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>fixit=False</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>WebSite_checkTranslationConsistency</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -119,3 +119,63 @@ class TestRenderJSUpgrade(ERP5TypeTestCase):
[
'Error: Web Site %s references a non existant appcache %s' % (self.web_site.getRelativeUrl(), non_existant_appcache)
], [str(m.getMessage()) for m in self.web_site.checkConsistency()])
def test_upgrade_site_translation(self):
test_upgrade_site_translation_data_js = self.portal.web_page_module.newContent(
portal_type='Web Script',
reference='test_upgrade_site_translation_data.js',
text_content='// will be filled',
)
test_upgrade_site_translation_data_js.publish()
test_upgrade_site_translation_data_js_modification_date = test_upgrade_site_translation_data_js.getModificationDate()
test_upgrade_site_translation_data_html = self.portal.web_page_module.newContent(
portal_type='Web Page',
reference='test_upgrade_site_translation.html',
content_type='text/html',
text_content=textwrap.dedent('''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Translation Gadget</title>
<link rel="http://www.renderjs.org/rel/interface" href="interface_translation.html">
<!-- renderjs -->
<script src="rsvp.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script>
<!-- custom script -->
<script src="test_upgrade_site_translation_data.js" type="text/javascript"></script>
<script src="gadget_translation.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
'''),
)
test_upgrade_site_translation_data_html.publish()
self.web_site.setProperty(
'configuration_translation_gadget_url',
'test_upgrade_site_translation.html',
)
self.web_site.setAvailableLanguageList(['en', 'fa'])
self.tic()
self.assertEqual(
['Translation data script content is not up to date'],
[str(m.getMessage()) for m in self.web_site.checkConsistency()])
self.web_site.fixConsistency()
self.tic()
self.assertEqual(
[],
[str(m.getMessage()) for m in self.web_site.checkConsistency()])
self.assertIn(
"window.translation_data = ",
test_upgrade_site_translation_data_js.getTextContent())
self.assertGreater(
test_upgrade_site_translation_data_js.getModificationDate(),
test_upgrade_site_translation_data_js_modification_date)
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