Commit 34ee4bdc authored by Tatuya Kamada's avatar Tatuya Kamada

erp5_hal_json_style: Fix double translation in ERP5Document_getHateoas

See merge request nexedi/erp5!1498
parents fd5a9d60 42f67e1c
Pipeline #18045 failed with stage
in 0 seconds
...@@ -695,26 +695,25 @@ def renderField(traversed_document, field, form, value=MARKER, meta_type=None, ...@@ -695,26 +695,25 @@ def renderField(traversed_document, field, form, value=MARKER, meta_type=None,
the original REQUEST with sent POST values from the parent form. We can save those the original REQUEST with sent POST values from the parent form. We can save those
values into our query method and reconstruct them meanwhile calling asynchronous jio.allDocs. values into our query method and reconstruct them meanwhile calling asynchronous jio.allDocs.
""" """
_translate = Base_translateString
# column definition in ListBox own value 'columns' is superseded by dynamic # column definition in ListBox own value 'columns' is superseded by dynamic
# column definition from Selection for specific Report ListBoxes; the same for editable_columns # column definition from Selection for specific Report ListBoxes; the same for editable_columns
column_list = [(name, _translate(title)) for name, title in (selection_params.get('selection_columns', []) column_list = [(name, title) for name, title in (selection_params.get('selection_columns', [])
or field.get_value("columns"))] or field.get_value("columns"))]
editable_column_list = [(name, _translate(title)) for name, title in (selection_params.get('editable_columns', []) editable_column_list = [(name, title) for name, title in (selection_params.get('editable_columns', [])
or field.get_value("editable_columns"))] or field.get_value("editable_columns"))]
all_column_list = [(name, _translate(title)) for name, title in field.get_value("all_columns")] all_column_list = [(name, title) for name, title in field.get_value("all_columns")]
catalog_column_list = [(name, title) catalog_column_list = [(name, title)
for name, title in OrderedDict(column_list + all_column_list).items() for name, title in OrderedDict(column_list + all_column_list).items()
if sql_catalog.isValidColumn(name)] if sql_catalog.isValidColumn(name)]
# try to get specified searchable columns and fail back to all searchable columns # try to get specified searchable columns and fail back to all searchable columns
search_column_list = [(name, _translate(title)) search_column_list = [(name, title)
for name, title in (field.get_value("search_columns") or catalog_column_list) for name, title in (field.get_value("search_columns") or catalog_column_list)
if sql_catalog.isValidColumn(name)] if sql_catalog.isValidColumn(name)]
# try to get specified sortable columns and fail back to searchable fields # try to get specified sortable columns and fail back to searchable fields
sort_column_list = [(name, _translate(title)) sort_column_list = [(name, title)
for name, title in (selection_params.get('selection_sort_order', []) for name, title in (selection_params.get('selection_sort_order', [])
or field.get_value("sort_columns") or search_column_list) or field.get_value("sort_columns") or search_column_list)
if sql_catalog.isValidColumn(name)] if sql_catalog.isValidColumn(name)]
...@@ -824,13 +823,16 @@ def renderField(traversed_document, field, form, value=MARKER, meta_type=None, ...@@ -824,13 +823,16 @@ def renderField(traversed_document, field, form, value=MARKER, meta_type=None,
# line["_relative_url"] = document.getRelativeUrl() # line["_relative_url"] = document.getRelativeUrl()
# line_list.append(line) # line_list.append(line)
def translateColumnListTitle(column_list):
return [(name, Base_translateString(title)) for (name, title) in column_list]
result.update({ result.update({
"column_list": column_list, "column_list": translateColumnListTitle(column_list),
"all_column_list": all_column_list, "all_column_list": translateColumnListTitle(all_column_list),
"search_column_list": search_column_list, "search_column_list": translateColumnListTitle(search_column_list),
"sort" :field.get_value('sort'), "sort" :field.get_value('sort'),
"sort_column_list": sort_column_list, "sort_column_list": translateColumnListTitle(sort_column_list),
"editable_column_list": editable_column_list, "editable_column_list": translateColumnListTitle(editable_column_list),
"show_anchor": field.get_value("anchor"), "show_anchor": field.get_value("anchor"),
"show_select": field.get_value("select"), "show_select": field.get_value("select"),
"portal_type": portal_type_list, "portal_type": portal_type_list,
...@@ -1428,13 +1430,18 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None, ...@@ -1428,13 +1430,18 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
container = traversed_document.getParentValue() container = traversed_document.getParentValue()
if container != portal: if container != portal:
# Jio does not support fetching the root document for now # Jio does not support fetching the root document for now
if container.getRelativeUrl():
container_name = ensureUTF8(container.getTranslatedTitle())
else:
# for example in portal_skin
container_name = ensureUTF8(container.getTitle())
result_dict['_links']['parent'] = { result_dict['_links']['parent'] = {
"href": default_document_uri_template % { "href": default_document_uri_template % {
"root_url": site_root.absolute_url(), "root_url": site_root.absolute_url(),
"relative_url": container.getRelativeUrl(), "relative_url": container.getRelativeUrl(),
"script_id": script.id "script_id": script.id
}, },
"name": Base_translateString(container.getTitle()), "name": container_name
} }
# Find current action URL and extract embedded view # Find current action URL and extract embedded view
......
...@@ -2973,6 +2973,95 @@ return msg" ...@@ -2973,6 +2973,95 @@ return msg"
result_dict = json.loads(result) result_dict = json.loads(result)
self.assertEqual(result_dict['title'], 'Foo_zhongwen') self.assertEqual(result_dict['title'], 'Foo_zhongwen')
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
'return "application/hal+json"')
@simulate('Base_translateString', 'msg, catalog="ui", encoding="utf8", lang="wo", **kw',
code_string)
@createIndexedDocument()
@changeSkin('Hal')
def test_getHateoasForm_not_adding_parent_title_translation(self, document):
document_title = document.getTitle() + "_not_translate_parent_message_catalog"
document.setTitle(document_title)
message_catalog = self.portal.Localizer.erp5_ui
self.assertFalse(message_catalog.message_exists(document_title))
foo_line = document.newContent(portal_type='Foo Line')
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.ERP5Document_getHateoas(
REQUEST=fake_request, mode="traverse",
relative_url=foo_line.getRelativeUrl(), view="view")
result_dict = json.loads(result)
# The document title includes 'ö' at the last in this test class, so calling decode("UTF-8")
self.assertEqual(result_dict['_links']['parent'],
{"href": "urn:jio:get:%s" % document.getRelativeUrl(), "name": document.getTitle().decode("UTF-8")})
# make sure traversing the child document does not adding the parent title translation
self.assertFalse(message_catalog.message_exists(document_title))
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
'return "application/hal+json"')
@simulate('Base_translateString', 'msg, catalog="ui", encoding="utf8", lang="wo", **kw',
code_string)
@createIndexedDocument()
@changeSkin('Hal')
def test_getHateoasForm_listbox_nested_message_catalog(self, document):
document.Foo_view.listbox.ListBox_setPropertyList(
field_title = 'Foo Lines',
field_list_method = 'objectValues',
field_portal_types = 'Foo Line | Foo Line',
field_stat_method = 'portal_catalog',
field_stat_columns = 'quantity | Foo_statQuantity',
field_editable = 1,
field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date\ncatalog.uid|Uid',
field_editable_columns = 'id|ID\ntitle|Title\nquantity|quantity\nstart_date|Date',
# modify these two for test
field_search_columns = 'id|ID\ntitle|Title_test_nested_message_catalog\nquantity|Quantity\nstart_date|Date',
# if sort_columns is empty search_columns are used for the fallback
field_sort_columns = '',
)
self.portal.Base_addUITestTranslation(message='Title_test_nested_message_catalog',
translation='biaoti_test_nested_message_catalog', language='wo')
message_catalog = self.portal.Localizer.erp5_ui
# Check the 'wo' message is not in the 'en' message_catalog before running the test
self.assertFalse(message_catalog.message_exists('biaoti_test_nested_message_catalog'))
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.ERP5Document_getHateoas(
REQUEST=fake_request, mode="traverse",
relative_url=document.getRelativeUrl(), view="view")
result_dict = json.loads(result)
self.assertEqual(result_dict['_embedded']['_view']['listbox']['column_list'],
[['id', 'ID'], ['title', 'biaoti'], ['quantity', 'Quantity'], ['start_date', 'Date'], ['catalog.uid', 'Uid']])
self.assertEqual(result_dict['_embedded']['_view']['listbox']['editable_column_list'],
[['id', 'ID'], ['title', 'biaoti'], ['quantity', 'quantity'], ['start_date', 'Date']])
self.assertEqual(result_dict['_embedded']['_view']['listbox']['search_column_list'],
[['id', 'ID'], ['title', 'biaoti_test_nested_message_catalog'], ['quantity', 'Quantity'], ['start_date', 'Date']])
# If sort_column is empty, search_column_list is used as the fall-back
self.assertEqual(result_dict['_embedded']['_view']['listbox']['sort_column_list'],
[['id', 'ID'], ['title', 'biaoti_test_nested_message_catalog'], ['quantity', 'Quantity'], ['start_date', 'Date']])
# check traversing listbox does not adding the 'wo' message as a 'en' message
self.assertFalse(message_catalog.message_exists('biaoti_test_nested_message_catalog'))
# reset to default
document.Foo_view.listbox.ListBox_setPropertyList(
field_title = 'Foo Lines',
field_list_method = 'objectValues',
field_portal_types = 'Foo Line | Foo Line',
field_stat_method = 'portal_catalog',
field_stat_columns = 'quantity | Foo_statQuantity',
field_editable = 1,
field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date\ncatalog.uid|Uid',
field_editable_columns = 'id|ID\ntitle|Title\nquantity|quantity\nstart_date|Date',
field_search_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',
)
class TestERP5Action_getHateoas(ERP5HALJSONStyleSkinsMixin): class TestERP5Action_getHateoas(ERP5HALJSONStyleSkinsMixin):
......
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