From bed0bfd8711f811378704bbb24c6949b52c46fc9 Mon Sep 17 00:00:00 2001
From: Romain Courteaud <romain@nexedi.com>
Date: Thu, 27 Jan 2005 12:13:00 +0000
Subject: [PATCH] Add dynamic generation of forwarder methods.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@2314 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Form/SelectionTool.py | 53 +++++++++++++++++++------------
 1 file changed, 32 insertions(+), 21 deletions(-)

diff --git a/product/ERP5Form/SelectionTool.py b/product/ERP5Form/SelectionTool.py
index b7656f0940..ae272e8212 100755
--- a/product/ERP5Form/SelectionTool.py
+++ b/product/ERP5Form/SelectionTool.py
@@ -908,27 +908,38 @@ class SelectionTool( UniqueObject, SimpleItem ):
       # Return the search dialog
       return o.Base_viewRelatedObjectList(REQUEST=REQUEST)                      
 
-    # XXX Methods here should be generated dynamically instead each time
-    # _v_relation_field_index is increased
-    security.declareProtected(ERP5Permissions.View, 'viewSearchRelatedDocumentDialog0')
-    def viewSearchRelatedDocumentDialog0(self, form_id, REQUEST=None, **kw):
-      """Forwarder method"""
-      return self.viewSearchRelatedDocumentDialog(0, form_id, REQUEST=REQUEST, **kw)
-
-    security.declareProtected(ERP5Permissions.View, 'viewSearchRelatedDocumentDialog1')
-    def viewSearchRelatedDocumentDialog1(self, form_id, REQUEST=None, **kw):
-      """Forwarder method"""
-      return self.viewSearchRelatedDocumentDialog(1, form_id, REQUEST=REQUEST, **kw)
-    
-    security.declareProtected(ERP5Permissions.View, 'viewSearchRelatedDocumentDialog2')
-    def viewSearchRelatedDocumentDialog2(self, form_id, REQUEST=None, **kw):
-      """Forwarder method"""
-      return self.viewSearchRelatedDocumentDialog(2, form_id, REQUEST=REQUEST, **kw)
-    
-    security.declareProtected(ERP5Permissions.View, 'viewSearchRelatedDocumentDialog9')
-    def viewSearchRelatedDocumentDialog9(self, form_id, REQUEST=None, **kw):
-      """Forwarder method"""
-      return self.viewSearchRelatedDocumentDialog(9, form_id, REQUEST=REQUEST, **kw)
+
+    def __getattr__(self, name):
+      dynamic_method_name = 'viewSearchRelatedDocumentDialog'
+      if name[:len(dynamic_method_name)] == dynamic_method_name:
+        method_count_string = name[len(dynamic_method_name):]
+        # be sure that method name is correct
+        try:
+          import string
+          method_count = string.atoi(method_count_string)
+        except:
+          raise AttributeError, name
+        else:
+          # generate dynamicaly needed forwarder methods
+          def viewSearchRelatedDocumentDialogWrapper(self, form_id, REQUEST=None, **kw):
+            """
+              viewSearchRelatedDocumentDialog Wrapper
+            """
+            return self.viewSearchRelatedDocumentDialog(method_count, form_id, REQUEST=REQUEST, **kw)
+          
+          setattr(self.__class__, name, viewSearchRelatedDocumentDialogWrapper)
+
+          klass = self.__class__
+          if hasattr(klass, 'security'):
+            from Products.ERP5Type import Permissions as ERP5Permissions
+            klass.security.declareProtected(ERP5Permissions.View, name)
+          else:
+            # XXX security declaration always failed....
+            LOG('WARNING ERP5Form SelectionTool, security not defined on',0,klass.__name__)
+
+          return getattr(self, name)
+      else:
+        raise AttributeError, name
 
       
 InitializeClass( SelectionTool )
-- 
2.30.9