From 7917e8a8c04bcc1be7aa9636205b8e057f3ea50b Mon Sep 17 00:00:00 2001
From: Yoshinori Okuji <yo@nexedi.com>
Date: Fri, 4 Dec 2009 17:41:37 +0000
Subject: [PATCH] 2009-12-05 yo * Stop raising a string in
 ERP5Type_asSecurityGroupId. * Optimize ERP5Type_asSecurityGroupId a little.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31089 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../erp5_core/ERP5Type_asSecurityGroupId.xml  | 71 +++++++++++--------
 product/ERP5/bootstrap/erp5_core/bt/revision  |  2 +-
 2 files changed, 42 insertions(+), 31 deletions(-)

diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/ERP5Type_asSecurityGroupId.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/ERP5Type_asSecurityGroupId.xml
index 828f3b2e6a..24460893e6 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/ERP5Type_asSecurityGroupId.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/ERP5Type_asSecurityGroupId.xml
@@ -89,6 +89,7 @@ to a Person instance. This is useful to implement user based local role assignme
 instead of abstract security based local roles.\n
 """\n
 portal = context.getPortalObject()\n
+getCategoryValue = portal.portal_categories.getCategoryValue\n
 \n
 # sort the category list lexicographically\n
 # this prevents us to choose the exact order we want,\n
@@ -105,38 +106,44 @@ list_of_list = []\n
 user_list = []\n
 for base_category in category_order:\n
   # It is acceptable for a category not to be defined\n
-  if kw.has_key(base_category):\n
+  try:\n
     category_list = kw[base_category]\n
-    associative_list = []\n
-    if same_type(category_list, \'\'):\n
-      category_list = [category_list]\n
-    for category in category_list:\n
-      if category.endswith(\'*\'):\n
-        category = category[:-1]\n
-        is_child_category = 1\n
-      else:\n
-        is_child_category = 0\n
-      category_path = \'%s/%s\' % (base_category, category)\n
-      category_object = portal.portal_categories.getCategoryValue(category_path)\n
-      if category_object in (None, \'\'):\n
-        raise "SecurityRoleDefinitionError", "Category \'%s\' doesn\'t exist" % (category_path)\n
-      portal_type = category_object.getPortalType()\n
-      if portal_type == \'Person\':\n
-        # We define a person here\n
-        user_name = category_object.getReference()\n
-        if user_name is not None: user_list.append(user_name)\n
-      else:\n
-        category_code = (category_object.getProperty(\'codification\') or\n
-                         category_object.getProperty(\'reference\') or\n
-                         category_object.getId())\n
-        if is_child_category: category_code += \'*\'\n
-        associative_list.append(category_code)\n
-    # Prevent making a cartesian product with an empty set\n
-    if associative_list:\n
-      list_of_list.append(associative_list)\n
+  except KeyError:\n
+    continue\n
+\n
+  associative_list = []\n
+  if isinstance(category_list, str):\n
+    category_list = [category_list]\n
+  for category in category_list:\n
+    if category[-1] == \'*\':\n
+      category = category[:-1]\n
+      is_child_category = 1\n
+    else:\n
+      is_child_category = 0\n
+    category_path = \'%s/%s\' % (base_category, category)\n
+    category_object = getCategoryValue(category_path)\n
+    if category_object is None:\n
+      raise RuntimeError("Security definition error (category %r not found)" % (category_path,))\n
+    portal_type = category_object.getPortalType()\n
+    if portal_type == \'Person\':\n
+      # We define a person here\n
+      user_name = category_object.getReference()\n
+      if user_name is not None:\n
+        user_list.append(user_name)\n
+    else:\n
+      category_code = (category_object.getProperty(\'codification\') or\n
+                        category_object.getProperty(\'reference\') or\n
+                        category_object.getId())\n
+      if is_child_category:\n
+        category_code += \'*\'\n
+      associative_list.append(category_code)\n
+  # Prevent making a cartesian product with an empty set\n
+  if associative_list:\n
+    list_of_list.append(associative_list)\n
 \n
 # Return a list of users if any was defined\n
-if user_list: return user_list\n
+if user_list:\n
+  return user_list\n
 \n
 # Compute the cartesian product and return the codes\n
 # return filter(lambda x: x, map(lambda x: \'_\'.join(x), cartesianProduct(list_of_list)))\n
@@ -182,6 +189,7 @@ return [\'_\'.join(x) for x in cartesianProduct(list_of_list) if x]\n
                             <string>_getattr_</string>
                             <string>context</string>
                             <string>portal</string>
+                            <string>getCategoryValue</string>
                             <string>None</string>
                             <string>list</string>
                             <string>Products.ERP5Type.Utils</string>
@@ -192,12 +200,15 @@ return [\'_\'.join(x) for x in cartesianProduct(list_of_list) if x]\n
                             <string>base_category</string>
                             <string>_getitem_</string>
                             <string>category_list</string>
+                            <string>KeyError</string>
                             <string>associative_list</string>
-                            <string>same_type</string>
+                            <string>isinstance</string>
+                            <string>str</string>
                             <string>category</string>
                             <string>is_child_category</string>
                             <string>category_path</string>
                             <string>category_object</string>
+                            <string>RuntimeError</string>
                             <string>portal_type</string>
                             <string>user_name</string>
                             <string>category_code</string>
diff --git a/product/ERP5/bootstrap/erp5_core/bt/revision b/product/ERP5/bootstrap/erp5_core/bt/revision
index 83a8aedd30..669268d4de 100644
--- a/product/ERP5/bootstrap/erp5_core/bt/revision
+++ b/product/ERP5/bootstrap/erp5_core/bt/revision
@@ -1 +1 @@
-1394
\ No newline at end of file
+1395
\ No newline at end of file
-- 
2.30.9