Commit fbac275e authored by Romain Courteaud's avatar Romain Courteaud

[erp5_hal_json_style] Allow to redirect to a specific document view

Add a new url_generator mode

Do not redirect to 'view': let the js handle the default navigation

Do not generate view url if document does not acquire the hal web section
parent 735f63bf
......@@ -29,5 +29,6 @@ return context.ERP5Document_getHateoas(
restricted=1,
extra_param_json=extra_param_json,
portal_status_message=portal_status_message,
portal_status_level=portal_status_level
portal_status_level=portal_status_level,
keep_items=keep_items
)
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>REQUEST=None, response=None, view=None, mode=\'root\', query=None, select_list=None, limit=10, local_roles=None, form=None, form_data=None, relative_url=None, list_method=None, default_param_json=None, form_relative_url=None, bulk_list="[]", group_by=None, sort_on=None, selection_domain=None, extra_param_json=None, portal_status_message=\'\', portal_status_level=None</string> </value>
<value> <string>REQUEST=None, response=None, view=None, mode=\'root\', query=None, select_list=None, limit=10, local_roles=None, form=None, form_data=None, relative_url=None, list_method=None, default_param_json=None, form_relative_url=None, bulk_list="[]", group_by=None, sort_on=None, selection_domain=None, extra_param_json=None, portal_status_message=\'\', portal_status_level=None, keep_items=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
......@@ -8,6 +8,7 @@ from ZTUtils import make_query
import json
request_form = context.REQUEST.form
previous_form_id = request_form.get('form_id', '')
request_form.update(kw)
request_form = context.ERP5Site_filterParameterList(request_form)
request_form.update(keep_items)
......@@ -55,6 +56,24 @@ result_dict = {
}
}
}
# form_id = 'view' means use default document view. Let the JS handle it
# In case of dialog submit, if redirecting to the original form, let the JS handle the navigation history
if (form_id is not None) and \
(form_id not in ['view', 'Base_viewFakePythonScriptActionForm']) and \
(form_id != previous_form_id) and \
(context.isWebMode()):
# Calculate the new view URL
result_dict['_links']['location'] = {
'href': context.ERP5Document_getHateoas(
mode='url_generator',
relative_url=context.getRelativeUrl(),
view=form_id,
keep_items=keep_items
)
}
result = json.dumps(result_dict, indent=2)
if abort_transaction:
response.write(result)
......
......@@ -2253,6 +2253,26 @@ else:
site_root = portal
view_action_type = "object_view"
# Calculate view url
if mode == 'url_generator':
#################################################
# Allow to generator URL from other python scripts
#################################################
if (keep_items is None):
generator_key = 'traverse_generator'
keep_items_json = None
else:
generator_key = 'traverse_generator_action'
keep_items_json = urlsafe_b64encode(
json.dumps(ensureSerializable(keep_items)))
return url_template_dict[generator_key] % {
"root_url": site_root.absolute_url(),
"script_id": 'ERP5Document_getHateoas',
"relative_url": relative_url.replace("/", "%2F"),
"view": view,
"extra_param_json": keep_items_json
}
context.Base_prepareCorsResponse(RESPONSE=response)
response.setHeader('Content-Type', mime_type)
......
......@@ -56,7 +56,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>REQUEST=None, response=None, view=None, mode=\'root\', query=None, select_list=None, limit=10, local_roles=None, form=None, form_data=None, relative_url=None, restricted=0, list_method=None, default_param_json=None, form_relative_url=None, bulk_list="[]", group_by=None, sort_on=None, selection_domain=None, extra_param_json=None, portal_status_message=\'\', portal_status_level=None</string> </value>
<value> <string>REQUEST=None, response=None, view=None, mode=\'root\', query=None, select_list=None, limit=10, local_roles=None, form=None, form_data=None, relative_url=None, restricted=0, list_method=None, default_param_json=None, form_relative_url=None, bulk_list="[]", group_by=None, sort_on=None, selection_domain=None, extra_param_json=None, portal_status_message=\'\', portal_status_level=None, keep_items=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
......@@ -2521,6 +2521,101 @@ class TestERP5Document_getHateoas_mode_worklist(ERP5HALJSONStyleSkinsMixin):
self.assertEqual(result_dict['_debug'], "worklist")
class TestERP5Document_getHateoas_mode_url_generator(ERP5HALJSONStyleSkinsMixin):
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
'return "application/hal+json"')
@createIndexedDocument()
@changeSkin('Hal')
def test_getHateoasUrlGenerator_no_parameter(self, document):
# self._makeDocument()
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.ERP5Document_getHateoas(
REQUEST=fake_request,
mode="url_generator",
relative_url="foo/bar",
view="Foo_viewBar"
)
self.assertEquals(fake_request.RESPONSE.status, 200)
self.assertEqual(
result,
'%s/web_site_module/hateoas/ERP5Document_getHateoas?'
'mode=traverse&relative_url=foo%%2Fbar&view=Foo_viewBar' % self.portal.absolute_url()
)
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
'return "application/hal+json"')
@createIndexedDocument()
@changeSkin('Hal')
def test_getHateoasUrlGenerator_parameter(self, document):
# self._makeDocument()
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.ERP5Document_getHateoas(
REQUEST=fake_request,
mode="url_generator",
relative_url="foo/bar",
view="Foo_viewBar",
keep_items={'foo': 'a', 'bar': 'b'}
)
self.assertEquals(fake_request.RESPONSE.status, 200)
self.assertEqual(
result,
'%s/web_site_module/hateoas/ERP5Document_getHateoas?'
'mode=traverse&relative_url=foo%%2Fbar&view=Foo_viewBar'
'&extra_param_json=eyJmb28iOiAiYSIsICJiYXIiOiAiYiJ9' % self.portal.absolute_url()
)
@simulate('Base_checkOnContextDocument', '*args, **kwargs',
'return context.ERP5Document_getHateoas(relative_url=context.getRelativeUrl(), **kwargs)')
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
'return "application/hal+json"')
@createIndexedDocument()
@changeSkin('Hal')
def test_getHateoasUrlGenerator_web_mode_context(self, document):
document = self._makeDocument()
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.restrictedTraverse(document.getRelativeUrl()).Base_checkOnContextDocument(
REQUEST=fake_request,
mode="url_generator",
view="Foo_viewBar"
)
self.assertEquals(fake_request.RESPONSE.status, 200)
self.assertEqual(
result,
'%s/web_site_module/hateoas/ERP5Document_getHateoas?'
'mode=traverse&relative_url=%s&view=Foo_viewBar' % (self.portal.absolute_url(), document.getRelativeUrl().replace('/', '%2F'))
)
@simulate('Base_checkOnNotContextDocument', '*args, **kwargs',
'return context.getPortalObject().restrictedTraverse(context.getRelativeUrl()).ERP5Document_getHateoas(relative_url=context.getRelativeUrl(), **kwargs)')
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
'return "application/hal+json"')
@createIndexedDocument()
@changeSkin('Hal')
def test_getHateoasUrlGenerator_not_web_mode_context(self, document):
document = self._makeDocument()
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.restrictedTraverse(document.getRelativeUrl()).Base_checkOnNotContextDocument(
REQUEST=fake_request,
mode="url_generator",
view="Foo_viewBar"
)
self.assertEquals(fake_request.RESPONSE.status, 200)
self.assertEqual(
result,
'%s/ERP5Document_getHateoas?'
'mode=traverse&relative_url=%s&view=Foo_viewBar' % (self.portal.absolute_url(), document.getRelativeUrl().replace('/', '%2F'))
)
class TestERP5Document_getHateoas_translation(ERP5HALJSONStyleSkinsMixin):
code_string = "\
from Products.CMFCore.utils import getToolByName\n\
......
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