Commit c52e28c5 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

erp5_hal_json_style: add preference to filter actions

Add a Hateoas preference field to filter available actions through a custom script.
parent 47ce31fb
......@@ -29,6 +29,8 @@ def filterActions(actions):
filtered_actions[action_category_name].extend(action_list)
else:
filtered_actions[action_category_name] = action_list
if filter_action_script_id:
filtered_actions = getattr(context, filter_action_script_id)(filtered_actions)
return {action_category_name: sorted(action_list, key=lambda x: x.get('priority', 1.0))
for action_category_name, action_list in filtered_actions.items()}
......
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>actions</string> </value>
<value> <string>actions, filter_action_script_id=None</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
......@@ -1472,8 +1472,12 @@ def calculateHateoas(is_portal=None, is_site_root=None, traversed_document=None,
}
# Find current action URL and extract embedded view
filter_action_script_id = ''
if hasattr(site_root, 'getLayoutProperty'):
filter_action_script_id = site_root.getLayoutProperty('configuration_filter_action_script_id', default='')
erp5_action_dict = portal.Base_filterDuplicateActions(
portal.portal_actions.listFilteredActionsFor(traversed_document))
portal.portal_actions.listFilteredActionsFor(traversed_document),
filter_action_script_id)
for erp5_action_key in erp5_action_dict.keys():
for view_action in erp5_action_dict[erp5_action_key]:
# Try to embed the form in the result
......
......@@ -78,6 +78,7 @@
<list>
<string>my_configuration_login</string>
<string>my_configuration_logout</string>
<string>my_configuration_filter_action_script_id</string>
</list>
</value>
</item>
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="ProxyField" module="Products.ERP5Form.ProxyField"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>delegated_list</string> </key>
<value>
<list>
<string>title</string>
</list>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>my_configuration_filter_action_script_id</string> </value>
</item>
<item>
<key> <string>message_values</string> </key>
<value>
<dictionary>
<item>
<key> <string>external_validator_failed</string> </key>
<value> <string>The input failed the external validator.</string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>overrides</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>target</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>tales</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>target</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</value>
</item>
<item>
<key> <string>values</string> </key>
<value>
<dictionary>
<item>
<key> <string>field_id</string> </key>
<value> <string>my_view_mode_reference</string> </value>
</item>
<item>
<key> <string>form_id</string> </key>
<value> <string>Base_viewFieldLibrary</string> </value>
</item>
<item>
<key> <string>target</string> </key>
<value> <string>Click to edit the target</string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string>Filter Action Script Id</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -709,6 +709,38 @@ class TestERP5Document_getHateoas_mode_traverse(ERP5HALJSONStyleSkinsMixin):
document.getRelativeUrl()))
self.assertEqual(result_dict['_embedded']['_view']['_actions']['put']['method'], 'POST')
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
'return "application/hal+json"')
@changeSkin('Hal')
def test_getHateoasDocument_filtered_view(self):
document = self._makeDocument()
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',
field_domain_root_list='foo_category|FooCat\nfoo_domain|FooDomain\nnot_existing_domain|NotExisting',)
parent = document.getParentValue()
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")
self.assertEqual(fake_request.RESPONSE.status, 200)
self.assertEqual(fake_request.RESPONSE.getHeader('Content-Type'),
"application/hal+json"
)
result_dict = json.loads(result)
self.assertEqual(result_dict['_links']['self'], {"href": "http://example.org/bar"})
self.assertEqual(result_dict['_links']['parent'],
{"href": "urn:jio:get:%s" % parent.getRelativeUrl(), "name": parent.getTitle()})
@simulate('Base_getRequestUrl', '*args, **kwargs',
'return "http://example.org/bar"')
@simulate('Base_getRequestHeader', '*args, **kwargs',
......
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