From b3afd86ea2aec4fea66e2c1d353444299712f85d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Fri, 17 Aug 2007 16:25:07 +0000
Subject: [PATCH] Filter duplicate actions defined on a document. Duplicate
 actions are actions from the same category with the same ID. In the case of
 duplicate actions, only the first action is shown.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@15737 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../Base_filterDuplicateActions.xml           | 200 ++++++++++++++++++
 .../erp5_xhtml_style/global_definitions.xml   |   2 +-
 .../bootstrap/erp5_xhtml_style/bt/revision    |   2 +-
 3 files changed, 202 insertions(+), 2 deletions(-)
 create mode 100644 product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_filterDuplicateActions.xml

diff --git a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_filterDuplicateActions.xml b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_filterDuplicateActions.xml
new file mode 100644
index 0000000000..284c98d388
--- /dev/null
+++ b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_filterDuplicateActions.xml
@@ -0,0 +1,200 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <tuple>
+        <tuple>
+          <string>Products.PythonScripts.PythonScript</string>
+          <string>PythonScript</string>
+        </tuple>
+        <none/>
+      </tuple>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>Python_magic</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>Script_magic</string> </key>
+            <value> <int>3</int> </value>
+        </item>
+        <item>
+            <key> <string>__ac_local_roles__</string> </key>
+            <value>
+              <none/>
+            </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>_body</string> </key>
+            <value> <string>"""This script filters duplicate actions for a document.\n
+Duplicate actions are actions with the same ID in the same action category.\n
+In case of duplicate, only the first action will be kept.\n
+\n
+`actions` is the mapping returned by ActionsTool.listFilteredActionsFor\n
+The script must be called on the context of the document.\n
+"""\n
+\n
+def filterDuplicateActions(actions):\n
+  new_actions = dict()\n
+\n
+  for action_category, action_list in actions.items():\n
+    existing_actions = dict()\n
+    new_actions[action_category] = []\n
+    keep_action = new_actions[action_category].append\n
+\n
+    for action in action_list:\n
+      if action[\'id\'] not in existing_actions:\n
+        existing_actions[action[\'id\']] = 1\n
+        keep_action(action)\n
+  return new_actions\n
+\n
+\n
+def hasDuplicateActions(portal_type, user_name):\n
+  len_actions = 0\n
+  len_filtered_actions = 0\n
+  for cat in actions.values():\n
+    len_actions += len(cat)\n
+  filtered_actions = filterDuplicateActions(actions)\n
+  for cat in filtered_actions.values():\n
+    len_filtered_actions += len(cat)\n
+  return len_actions != len_filtered_actions\n
+\n
+\n
+from Products.ERP5Type.Cache import CachingMethod\n
+hasDuplicateActions = CachingMethod(\n
+                          hasDuplicateActions,\n
+                          id=\'Base_filterDuplicateActions.hasDuplicateActions\',\n
+                          cache_factory=\'erp5_ui_long\')\n
+\n
+user_name = str(container.REQUEST.AUTHENTICATED_USER)\n
+if hasDuplicateActions(context.getPortalType(), user_name):\n
+  return filterDuplicateActions(actions)\n
+return actions\n
+</string> </value>
+        </item>
+        <item>
+            <key> <string>_code</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>_filepath</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>_owner</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string>actions</string> </value>
+        </item>
+        <item>
+            <key> <string>errors</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+        <item>
+            <key> <string>func_code</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="FuncCode" module="Shared.DC.Scripts.Signature"/>
+                </klass>
+                <tuple/>
+                <state>
+                  <dictionary>
+                    <item>
+                        <key> <string>co_argcount</string> </key>
+                        <value> <int>1</int> </value>
+                    </item>
+                    <item>
+                        <key> <string>co_varnames</string> </key>
+                        <value>
+                          <tuple>
+                            <string>actions</string>
+                            <string>filterDuplicateActions</string>
+                            <string>hasDuplicateActions</string>
+                            <string>Products.ERP5Type.Cache</string>
+                            <string>CachingMethod</string>
+                            <string>str</string>
+                            <string>_getattr_</string>
+                            <string>container</string>
+                            <string>user_name</string>
+                            <string>context</string>
+                          </tuple>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>func_defaults</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>Base_filterDuplicateActions</string> </value>
+        </item>
+        <item>
+            <key> <string>warnings</string> </key>
+            <value>
+              <tuple/>
+            </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/global_definitions.xml b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/global_definitions.xml
index 2f636fe182..599db49b07 100644
--- a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/global_definitions.xml
+++ b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/global_definitions.xml
@@ -60,7 +60,7 @@
     global portal_path         portal/portal_url/getPortalPath;\n
            local_parameter_list  local_parameter_list | python: {};\n
 \n
-    global actions          python: portal.portal_actions.listFilteredActionsFor(here);\n
+    global actions          python: here.Base_filterDuplicateActions(portal.portal_actions.listFilteredActionsFor(here));\n
     global url              here/absolute_url;\n
     global current_form_id  python: local_parameter_list.get(\'dialog_id\', local_parameter_list.get(\'form_id\', \'view\'));\n
            dummy            python: request.set(\'current_form_id\', current_form_id);\n
diff --git a/product/ERP5/bootstrap/erp5_xhtml_style/bt/revision b/product/ERP5/bootstrap/erp5_xhtml_style/bt/revision
index 8d4011ad25..5c9fe11c04 100644
--- a/product/ERP5/bootstrap/erp5_xhtml_style/bt/revision
+++ b/product/ERP5/bootstrap/erp5_xhtml_style/bt/revision
@@ -1 +1 @@
-412
\ No newline at end of file
+413
\ No newline at end of file
-- 
2.30.9