Commit f4a39ef0 authored by Romain Courteaud's avatar Romain Courteaud

erp5_hal_json_style: add support for action triggered on a different context

parent ab41352f
......@@ -344,14 +344,24 @@ def getRealRelativeUrl(document):
return '/'.join(portal.portal_url.getRelativeContentPath(document))
def parseActionUrl(url):
def parseActionUrl(context_relative_url, url):
"""Parse usual ERP5 Action URL into components: ~root, context~, view_id, param_dict, url.
:param url: {str} is expected to be in form https://<site_root>/context/view_id?optional=params
"""
param_dict = {}
url_and_params = url.split(site_root.absolute_url())[-1].split('?')
_, script = url_and_params[0].strip("/ ").rsplit('/', 1)
url_path = url_and_params[0].strip("/ ")
other_context = None
_, script = url_path.rsplit('/', 1)
if context_relative_url is not None:
context_index = url_path.find(context_relative_url)
if context_index > -1:
# Do not forget to remove the '/' after the relative url
url_path = url_path[context_index + len(context_relative_url) + 1:]
if '/' in url_path:
# Check if there is an extra context
other_context, script = url_path.rsplit('/', 1)
if len(url_and_params) > 1:
for param in url_and_params[1].split('&'):
param_name, param_value = param.split('=')
......@@ -373,6 +383,7 @@ def parseActionUrl(url):
param_dict[param_name] = param_value
return {
'view_id': script,
'other_context': other_context,
'params': param_dict,
'url': url
}
......@@ -1326,7 +1337,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
for view_action in erp5_action_dict[erp5_action_key]:
# Try to embed the form in the result
if (view == view_action['id']):
current_action = parseActionUrl('%s' % view_action['url']) # current action/view being rendered
current_action = parseActionUrl(relative_url, '%s' % view_action['url']) # current action/view being rendered
current_action['category_type'] = erp5_action_key
if view and (view != 'view') and (current_action.get('view_id', None) is None):
......@@ -1338,7 +1349,11 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
# If we have current action definition we are able to render embedded view
# which should be a "ERP5 Form" but in reality can be anything
if current_action.get('view_id', ''):
view_instance = getattr(traversed_document, current_action['view_id'])
if current_action.get('other_context', None) is None:
view_context = traversed_document
else:
view_context = traversed_document.restrictedTraverse(current_action['other_context'])
view_instance = getattr(view_context, current_action['view_id'])
if (view_instance is not None):
embedded_dict = {
'_links': {
......@@ -1359,9 +1374,9 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
# then we execute it directly
if "Script" in getattr(view_instance, "meta_type", "Script"):
if current_action.get('category_type', None) == 'object_jio_jump':
view_instance = getattr(traversed_document, 'Base_viewFakeJumpForm')
view_instance = getattr(view_context, 'Base_viewFakeJumpForm')
else:
view_instance = getattr(traversed_document, 'Base_viewFakePythonScriptActionForm')
view_instance = getattr(view_context, 'Base_viewFakePythonScriptActionForm')
if view_instance.pt == "form_dialog":
# If there is a "form_id" in the REQUEST then it means that last view was actually a form
......@@ -1372,7 +1387,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
# REQUEST but expect all (formerly) URL query parameters to appear in their **kw
# thus we send extra_param_json (=rjs way of passing parameters to REQUEST) as
# selection_params so they get into callable's **kw.
renderForm(traversed_document, view_instance, embedded_dict,
renderForm(view_context, view_instance, embedded_dict,
selection_params=extra_param_json, extra_param_json=extra_param_json)
if view_instance.pt in ["form_python_action", "form_jump"]:
......@@ -1384,7 +1399,7 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
embedded_dict['_actions'] = {
'put': {
"href": url_template_dict["form_action"] % {
"traversed_document_url": site_root.absolute_url() + "/" + getRealRelativeUrl(traversed_document),
"traversed_document_url": site_root.absolute_url() + "/" + getRealRelativeUrl(view_context),
"action_id": current_action['view_id']
}
},
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ActionInformation" module="Products.CMFCore.ActionInformation"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>action</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>categories</string> </key>
<value>
<tuple>
<string>action_type/object_jio_action</string>
</tuple>
</value>
</item>
<item>
<key> <string>category</string> </key>
<value> <string>object_jio_action</string> </value>
</item>
<item>
<key> <string>condition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>icon</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>other_context_action</string> </value>
</item>
<item>
<key> <string>permissions</string> </key>
<value>
<tuple>
<string>View</string>
</tuple>
</value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Action Information</string> </value>
</item>
<item>
<key> <string>priority</string> </key>
<value> <float>30.0</float> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Other Context</string> </value>
</item>
<item>
<key> <string>visible</string> </key>
<value> <int>1</int> </value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="Expression" module="Products.CMFCore.Expression"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>text</string> </key>
<value> <string>string:${object_url}/bar_module/Base_viewOtherContextDialog</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
return context.Base_redirect('view', keep_items=dict(portal_status_message=context.getRelativeUrl()))
<?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>**kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_submitOtherContextDialog</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ERP5 Form" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_objects</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>action</string> </key>
<value> <string>Base_submitOtherContextDialog</string> </value>
</item>
<item>
<key> <string>action_title</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>edit_order</string> </key>
<value>
<list/>
</value>
</item>
<item>
<key> <string>encoding</string> </key>
<value> <string>UTF-8</string> </value>
</item>
<item>
<key> <string>enctype</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>group_list</string> </key>
<value>
<list>
<string>left</string>
<string>right</string>
<string>center</string>
<string>bottom</string>
<string>hidden</string>
</list>
</value>
</item>
<item>
<key> <string>groups</string> </key>
<value>
<dictionary>
<item>
<key> <string>bottom</string> </key>
<value>
<list/>
</value>
</item>
<item>
<key> <string>center</string> </key>
<value>
<list/>
</value>
</item>
<item>
<key> <string>hidden</string> </key>
<value>
<list/>
</value>
</item>
<item>
<key> <string>left</string> </key>
<value>
<list>
<string>my_title</string>
</list>
</value>
</item>
<item>
<key> <string>right</string> </key>
<value>
<list/>
</value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_viewOtherContextDialog</string> </value>
</item>
<item>
<key> <string>method</string> </key>
<value> <string>POST</string> </value>
</item>
<item>
<key> <string>name</string> </key>
<value> <string>Base_viewOtherContextDialog</string> </value>
</item>
<item>
<key> <string>pt</string> </key>
<value> <string>form_dialog</string> </value>
</item>
<item>
<key> <string>row_length</string> </key>
<value> <int>4</int> </value>
</item>
<item>
<key> <string>stored_encoding</string> </key>
<value> <string>UTF-8</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Other Context</string> </value>
</item>
<item>
<key> <string>unicode_mode</string> </key>
<value> <int>0</int> </value>
</item>
<item>
<key> <string>update_action</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>update_action_title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -16,6 +16,7 @@ Foo Module | do_nothing_report_jio
Foo Module | empty_mass_action
Foo Module | list
Foo Module | list_ui
Foo Module | other_context_action
Foo Module | search
Foo Module | sort_on
Foo Module | view
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ZopePageTemplate" module="Products.PageTemplates.ZopePageTemplate"/>
</pickle>
<pickle>
<dictionary>
<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_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/html</string> </value>
</item>
<item>
<key> <string>expand</string> </key>
<value> <int>0</int> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>testFormDialogOtherContext</string> </value>
</item>
<item>
<key> <string>output_encoding</string> </key>
<value> <string>utf-8</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <unicode></unicode> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<html xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Workflow Transition with Invalid Form</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">Test dialog with multiple forms</td></tr>
</thead><tbody>
<tal:block metal:use-macro="here/PTZuite_CommonTemplate/macros/init" />
<tr>
<td>open</td>
<td>${base_url}/web_site_module/renderjs_runner/#/foo_module</td>
<td></td>
</tr>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_app_loaded" />
<tal:block tal:define="click_configuration python: {'text': 'Actions'}">
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/click_on_header_link" />
</tal:block>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
<tal:block tal:define="click_configuration python: {'text': 'Other Context'}">
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/click_on_page_link" />
</tal:block>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
<tr>
<td colspan="3"><b>Check that the dialog is rendered on the bar module context</b></td>
</tr>
<tr>
<td>assertValue</td>
<td>field_my_title</td>
<td>Bars</td>
</tr>
<tr>
<td colspan="3"><b>But header/panel are on the Foo Module</b></td>
</tr>
<tr>
<td>assertTextPresent</td>
<td>Foo Module</td>
<td></td>
</tr>
<tr>
<td>assertTextNotPresent</td>
<td>Bar Module</td>
<td></td>
</tr>
<tr>
<td>assertTextPresent</td>
<td>Do Nothing Action</td>
<td></td>
</tr>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/submit_dialog" />
<tal:block tal:define="notification_configuration python: {'class': 'success',
'text': 'bar_module'}">
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_notification" />
</tal:block>
<tal:block metal:use-macro="here/Zuite_CommonTemplateForRenderjsUi/macros/wait_for_content_loaded" />
<tr>
<td colspan="3"><b>Redireted to Bar Module</b></td>
</tr>
<tr>
<td>assertTextNotPresent</td>
<td>Foo Module</td>
<td></td>
</tr>
<tr>
<td>assertTextPresent</td>
<td>Bar Module</td>
<td></td>
</tr>
<tr>
<td>assertTextNotPresent</td>
<td>Do Nothing Action</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>
\ No newline at end of file
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