Commit 52282904 authored by Tomáš Peterka's avatar Tomáš Peterka

[erp5_ui] Filter Action prefer own UI version of Action in case of ID duplicates

parent cd68cb24
"""This script filters duplicate actions for a document.
Duplicate actions are actions with the same ID in the same action category or related XHTML category.
In case of duplicate, the JIO version (or the first in the same category) will be kept.
`actions` is the mapping returned by ActionsTool.listFilteredActionsFor
The script must be called on the context of the document.
"""
from Products.ERP5Type.Cache import CachingMethod
def filterDuplicateActions(actions):
new_actions = {}
for action_category_name, action_list in actions.items():
# with introduction of the new UI some actions can have JIO only versions
# so we have to consider them as duplicates even though they are in different
# category because those categories are rendered together (e.g object_action and object_jio_action)
is_jio_action_category = "_jio_" in action_category_name
other_action_id_set = set()
if is_jio_action_category:
prefix, _, suffix = action_category_name.split("_", 2) # omit the _jio_
other_action_id_set.update(action['id']
for action in actions.get(prefix + "_" + suffix, []))
else:
if "_" in action_category_name:
prefix, suffix = action_category_name.split("_", 1)
other_action_id_set.update(action['id']
for action in actions.get(prefix + "_jio_" + suffix, []))
existing_actions = set()
new_actions[action_category_name] = []
keep_action = new_actions[action_category_name].append
for action in action_list:
if not is_jio_action_category: # prefer JIO version of the same action
if action['id'] in other_action_id_set:
continue
if action['id'] not in existing_actions:
existing_actions.add(action['id'])
keep_action(action)
return new_actions
def hasDuplicateActions(portal_type, user_name):
len_actions = 0
len_filtered_actions = 0
for cat in actions.values():
len_actions += len(cat)
filtered_actions = filterDuplicateActions(actions)
for cat in filtered_actions.values():
len_filtered_actions += len(cat)
return len_actions != len_filtered_actions
hasDuplicateActions = CachingMethod(
hasDuplicateActions,
id='Base_filterDuplicateActions.hasDuplicateActions',
cache_factory='erp5_ui_long')
user_name = getattr(container.REQUEST, 'AUTHENTICATED_USER', '')
if getattr(context, 'getPortalType', None) is not None:
if hasDuplicateActions(context.getPortalType(), user_name):
return filterDuplicateActions(actions)
return actions
<?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>actions</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Base_filterDuplicateActions</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
"""This script filters duplicate actions for a document.
Duplicate actions are actions with the same ID in the same action category.
In case of duplicate, only the first action will be kept.
Duplicate actions are actions with the same ID in the same action category or related JIO category.
In case of duplicate, the XHTML version (or the first in the same category) will be kept.
`actions` is the mapping returned by ActionsTool.listFilteredActionsFor
The script must be called on the context of the document.
"""
from Products.ERP5Type.Cache import CachingMethod
def filterDuplicateActions(actions):
new_actions = {}
for action_category, action_list in actions.items():
for action_category_name, action_list in actions.items():
# with introduction of the new UI some actions can have JIO only versions
# so we have to consider them as duplicates even though they are in different
# category because those categories are rendered together (e.g object_action and object_jio_action)
is_jio_action_category = "_jio_" in action_category_name
other_action_id_set = set()
if is_jio_action_category:
prefix, _, suffix = action_category_name.split("_", 2) # omit the _jio_
other_action_id_set.update(action['id']
for action in actions.get(prefix + "_" + suffix, []))
else:
if "_" in action_category_name:
prefix, suffix = action_category_name.split("_", 1)
other_action_id_set.update(action['id']
for action in actions.get(prefix + "_jio_" + suffix, []))
existing_actions = set()
new_actions[action_category] = []
keep_action = new_actions[action_category].append
new_actions[action_category_name] = []
keep_action = new_actions[action_category_name].append
for action in action_list:
if is_jio_action_category: # prefer XHTML version of the same action
if action['id'] in other_action_id_set:
continue
if action['id'] not in existing_actions:
existing_actions.add(action['id'])
keep_action(action)
return new_actions
......
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