Commit b05a67b3 authored by Romain Courteaud's avatar Romain Courteaud

[erp5_hal_json_style] Embed form definition while rendering it

The goal is to reduce the number of HTTP query triggered by the UI.

Keep the original link to ensure compatibility with the UI.
parent effbd624
...@@ -102,7 +102,7 @@ def renderHiddenField(form, name, value): ...@@ -102,7 +102,7 @@ def renderHiddenField(form, name, value):
form['_embedded'] = {} form['_embedded'] = {}
form['_embedded']['_view'] = {} form['_embedded']['_view'] = {}
if '_embedded' in form: if ('_embedded' in form) and ('_view' in form['_embedded']):
field_dict = form['_embedded']['_view'] field_dict = form['_embedded']['_view']
else: else:
field_dict = form field_dict = form
...@@ -1055,18 +1055,27 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti ...@@ -1055,18 +1055,27 @@ def renderForm(traversed_document, form, response_dict, key_prefix=None, selecti
} }
form_relative_url = getFormRelativeUrl(form) form_relative_url = getFormRelativeUrl(form)
# Kept for compatibility
response_dict['_links']['form_definition'] = { response_dict['_links']['form_definition'] = {
# "href": default_document_uri_template % {
# "root_url": site_root.absolute_url(),
# "script_id": script.id,
# "relative_url": getFormRelativeUrl(form)
# },
"href": default_document_uri_template % { "href": default_document_uri_template % {
"relative_url": form_relative_url "relative_url": form_relative_url
}, },
'name': form.id 'name': form.id
} }
response_dict['_embedded'] = {
'form_definition': calculateHateoas(
traversed_document=form,
relative_url=form_relative_url,
is_site_root=False,
is_portal=False,
mode='traverse',
restricted=1,
view='view'
)
}
# Go through all groups ("left", "bottom", "hidden" etc.) and add fields from # Go through all groups ("left", "bottom", "hidden" etc.) and add fields from
# them into form. # them into form.
for group in form.Form_getGroupTitleAndId(): for group in form.Form_getGroupTitleAndId():
...@@ -1586,6 +1595,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None, ...@@ -1586,6 +1595,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
traversed_document_portal_type = traversed_document.getPortalType() traversed_document_portal_type = traversed_document.getPortalType()
if traversed_document_portal_type in ("ERP5 Form", "ERP5 Report"): if traversed_document_portal_type in ("ERP5 Form", "ERP5 Report"):
renderFormDefinition(traversed_document, result_dict) renderFormDefinition(traversed_document, result_dict)
if response is not None:
response.setHeader("Cache-Control", "private, max-age=1800") response.setHeader("Cache-Control", "private, max-age=1800")
response.setHeader("Vary", "Cookie,Authorization,Accept-Encoding") response.setHeader("Vary", "Cookie,Authorization,Accept-Encoding")
response.setHeader("Last-Modified", DateTime().rfc822()) response.setHeader("Last-Modified", DateTime().rfc822())
......
...@@ -645,6 +645,18 @@ class TestERP5Document_getHateoas_mode_traverse(ERP5HALJSONStyleSkinsMixin): ...@@ -645,6 +645,18 @@ class TestERP5Document_getHateoas_mode_traverse(ERP5HALJSONStyleSkinsMixin):
self.assertEqual(result_dict['_embedded']['_view']['_links']['form_definition']['href'], 'urn:jio:get:portal_skins/erp5_ui_test/Foo_view') self.assertEqual(result_dict['_embedded']['_view']['_links']['form_definition']['href'], 'urn:jio:get:portal_skins/erp5_ui_test/Foo_view')
self.assertEqual(result_dict['_embedded']['_view']['_links']['form_definition']['name'], 'Foo_view') self.assertEqual(result_dict['_embedded']['_view']['_links']['form_definition']['name'], 'Foo_view')
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['group_list'][0][0],
'left'
)
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['group_list'][0][1][0],
['my_id', {'meta_type': 'ProxyField'}]
)
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['pt'],
'form_view'
)
self.assertEqual(result_dict['_embedded']['_view']['_actions']['put']['href'], '%s/web_site_module/hateoas/%s/Base_edit' % ( self.assertEqual(result_dict['_embedded']['_view']['_actions']['put']['href'], '%s/web_site_module/hateoas/%s/Base_edit' % (
self.portal.absolute_url(), self.portal.absolute_url(),
...@@ -757,6 +769,18 @@ class TestERP5Document_getHateoas_mode_traverse(ERP5HALJSONStyleSkinsMixin): ...@@ -757,6 +769,18 @@ class TestERP5Document_getHateoas_mode_traverse(ERP5HALJSONStyleSkinsMixin):
self.assertEqual(result_dict['_embedded']['_view']['_links']['form_definition']['href'], 'urn:jio:get:portal_skins/erp5_ui_test/Foo_view') self.assertEqual(result_dict['_embedded']['_view']['_links']['form_definition']['href'], 'urn:jio:get:portal_skins/erp5_ui_test/Foo_view')
self.assertEqual(result_dict['_embedded']['_view']['_links']['form_definition']['name'], 'Foo_view') self.assertEqual(result_dict['_embedded']['_view']['_links']['form_definition']['name'], 'Foo_view')
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['group_list'][0][0],
'left'
)
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['group_list'][0][1][0],
['my_id', {'meta_type': 'ProxyField'}]
)
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['pt'],
'form_view'
)
self.assertFalse(result_dict['_embedded']['_view'].has_key('_actions')) self.assertFalse(result_dict['_embedded']['_view'].has_key('_actions'))
...@@ -889,6 +913,18 @@ class TestERP5Document_getHateoas_mode_traverse(ERP5HALJSONStyleSkinsMixin): ...@@ -889,6 +913,18 @@ class TestERP5Document_getHateoas_mode_traverse(ERP5HALJSONStyleSkinsMixin):
self.assertEqual(result_dict['_embedded']['_view']['report_section_list'][1]['_links']['form_definition']['href'], 'urn:jio:get:portal_skins/erp5_core/Base_viewWorkflowHistory') self.assertEqual(result_dict['_embedded']['_view']['report_section_list'][1]['_links']['form_definition']['href'], 'urn:jio:get:portal_skins/erp5_core/Base_viewWorkflowHistory')
self.assertEqual(result_dict['_embedded']['_view']['report_section_list'][1]['_links']['form_definition']['name'], 'Base_viewWorkflowHistory') self.assertEqual(result_dict['_embedded']['_view']['report_section_list'][1]['_links']['form_definition']['name'], 'Base_viewWorkflowHistory')
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['group_list'][0][0],
'center'
)
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['group_list'][0][1][0],
['your_zodb_history', {'meta_type': 'LinkField'}]
)
self.assertEqual(
result_dict['_embedded']['_view']['_embedded']['form_definition']['pt'],
'report_view'
)
self.assertSameSet(result_dict['_embedded']['_view']['report_section_list'][1]['listbox']['default_params'].keys(), ['checked_permission', 'ignore_unknown_columns', 'workflow_id', 'workflow_title']) self.assertSameSet(result_dict['_embedded']['_view']['report_section_list'][1]['listbox']['default_params'].keys(), ['checked_permission', 'ignore_unknown_columns', 'workflow_id', 'workflow_title'])
self.assertTrue(result_dict['_embedded']['_view']['report_section_list'][1]['listbox']['default_params']['ignore_unknown_columns']) self.assertTrue(result_dict['_embedded']['_view']['report_section_list'][1]['listbox']['default_params']['ignore_unknown_columns'])
...@@ -1825,6 +1861,18 @@ class TestERP5Document_getHateoas_mode_bulk(ERP5HALJSONStyleSkinsMixin): ...@@ -1825,6 +1861,18 @@ class TestERP5Document_getHateoas_mode_bulk(ERP5HALJSONStyleSkinsMixin):
self.assertEqual(result_dict['result_list'][0]['_embedded']['_view']['_links']['form_definition']['href'], 'urn:jio:get:portal_skins/erp5_ui_test/Foo_view') self.assertEqual(result_dict['result_list'][0]['_embedded']['_view']['_links']['form_definition']['href'], 'urn:jio:get:portal_skins/erp5_ui_test/Foo_view')
self.assertEqual(result_dict['result_list'][0]['_embedded']['_view']['_links']['form_definition']['name'], 'Foo_view') self.assertEqual(result_dict['result_list'][0]['_embedded']['_view']['_links']['form_definition']['name'], 'Foo_view')
self.assertEqual(
result_dict['result_list'][0]['_embedded']['_view']['_embedded']['form_definition']['group_list'][0][0],
'left'
)
self.assertEqual(
result_dict['result_list'][0]['_embedded']['_view']['_embedded']['form_definition']['group_list'][0][1][0],
['my_id', {'meta_type': 'ProxyField'}]
)
self.assertEqual(
result_dict['result_list'][0]['_embedded']['_view']['_embedded']['form_definition']['pt'],
'form_view'
)
self.assertEqual(result_dict['result_list'][0]['_embedded']['_view']['_actions']['put']['href'], '%s/web_site_module/hateoas/%s/Base_edit' % ( self.assertEqual(result_dict['result_list'][0]['_embedded']['_view']['_actions']['put']['href'], '%s/web_site_module/hateoas/%s/Base_edit' % (
self.portal.absolute_url(), self.portal.absolute_url(),
......
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