From 8e57ca29505b5dd016b8a429abc8a3cfbd1ea71b Mon Sep 17 00:00:00 2001
From: Jean-Paul Smets <jp@nexedi.com>
Date: Sat, 30 Dec 2006 15:20:57 +0000
Subject: [PATCH] Implemented dynamic selection of preference property sheets.
 It is no longe necessary to put all propertysheet definitions in
 ERP5/PropertySheet

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11803 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Form/Document/Preference.py | 18 ++++++++---------
 product/ERP5Form/PreferenceTool.py      | 26 ++++++++++++++++++++-----
 product/ERP5Type/Base.py                |  7 ++++++-
 3 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/product/ERP5Form/Document/Preference.py b/product/ERP5Form/Document/Preference.py
index fbb80a2df4..2485462a0b 100644
--- a/product/ERP5Form/Document/Preference.py
+++ b/product/ERP5Form/Document/Preference.py
@@ -37,11 +37,15 @@ class Priority:
   SITE  = 1
   GROUP = 2
   USER  = 3
-   
+
 class Preference( Folder ):
-  """ An user preference 
   """
-    
+    A system or user preference
+
+    TODO:
+      - migrate to ERP5Type the whole preference system
+  """
+
   # CMF Type Definition
   meta_type       = 'ERP5 Preference'
   portal_type     = 'Preference'
@@ -50,14 +54,10 @@ class Preference( Folder ):
   isRADContent    = 1
   
   # Declarative properties
-  property_sheets = ( PropertySheet.Base
+  property_sheets = \
+                    ( PropertySheet.Base
                     , PropertySheet.XMLObject
                     , PropertySheet.DublinCore
-                    , PropertySheet.Preference
-                    , PropertySheet.AccountingPreference
-                    , PropertySheet.SubversionPreference
-                    , PropertySheet.HtmlStylePreference
-                    , PropertySheet.DMSPreference
                     )
   
   # Declarative security
diff --git a/product/ERP5Form/PreferenceTool.py b/product/ERP5Form/PreferenceTool.py
index d5a257e5ce..092848100f 100644
--- a/product/ERP5Form/PreferenceTool.py
+++ b/product/ERP5Form/PreferenceTool.py
@@ -33,8 +33,9 @@ from zLOG import LOG, INFO, PROBLEM
 
 from Products.CMFCore.utils import getToolByName
 from Products.ERP5Type.Tool.BaseTool import BaseTool
-from Products.ERP5Type import Permissions
+from Products.ERP5Type import Permissions, PropertySheet
 from Products.ERP5Type.Cache import CachingMethod
+from Products.ERP5Type.Base import Base
 from Products.ERP5Type.Utils import convertToUpperCase
 from Products.ERP5Type.Accessor.TypeDefinition import list_types
 from Products.ERP5Form.Document.Preference import Preference
@@ -45,8 +46,13 @@ from Products.ERP5Form.Document.Preference import Priority
 class func_code: pass
 
 def createPreferenceMethods(portal) :
-  """Initialize all Preference methods on the preference tool.
-  This method must be called on startup.
+  """
+    Initialize all Preference methods on the preference tool.
+    This method must be called on startup.
+
+    This tool is capable of updating the list of Preference
+    property sheets by looking at all registered property sheets
+    and considering those which name ends with 'Preference'
   """
   attr_list = []
   typestool = getToolByName(portal, 'portal_types')
@@ -65,9 +71,19 @@ def createPreferenceMethods(portal) :
            'unable to import Property Sheet %s' % property_sheet, e)
   # 'Static' property sheets defined on the class
   class_property_sheet_list = Preference.property_sheets
+  # Time to lookup for preferences defined on other modules
+  property_sheets = list(class_property_sheet_list)
+  for id in dir(PropertySheet):
+    if id.endswith('Preference'):
+      ps = getattr(PropertySheet, id)
+      if ps not in property_sheets:
+        property_sheets.append(ps)
+  class_property_sheet_list = tuple(property_sheets)
+  Preference.property_sheets = class_property_sheet_list
+  # We can now merge
   for property_sheet in ( tuple(zmi_property_sheet_list) +
                                 class_property_sheet_list ) :
-    # then generate common method names 
+    # then generate common method names
     for prop in property_sheet._properties :
       if not prop.get('preference', 0) :
         # only properties marked as preference are used
@@ -79,7 +95,7 @@ def createPreferenceMethods(portal) :
       for attribute_name in attr_list:
         method = PreferenceMethod(attribute_name)
         setattr(PreferenceTool, attribute_name, method)
-
+  
 class PreferenceMethod(Method) :
   """ A method object that lookup the attribute on preferences. """
   # This is required to call the method form the Web
diff --git a/product/ERP5Type/Base.py b/product/ERP5Type/Base.py
index 16325533be..6d3fee8da3 100644
--- a/product/ERP5Type/Base.py
+++ b/product/ERP5Type/Base.py
@@ -376,6 +376,7 @@ class Base( CopyContainer,
   isIndexable = 1     # If set to 0, reindexing will not happen (useful for optimization)
   isPredicate = 0     #
   isTemplate = 0      #
+  isDocument = 0      #
 
   # Dynamic method acquisition system (code generation)
   aq_method_generated = {}
@@ -500,16 +501,20 @@ class Base( CopyContainer,
 
       Base.aq_related_generated = 1
 
+    # Generate preference methods (since side effect is to reset Preference accessors)
     if not Base.aq_preference_generated:
       try :
         from Products.ERP5Form.PreferenceTool import createPreferenceMethods
+        from Products.ERP5Form.Document.Preference import Preference
         createPreferenceMethods(self.getPortalObject())
+        # Force update of Preference accessors
+        initializePortalTypeDynamicProperties(self, Preference, Preference.portal_type)
       except ImportError, e :
         LOG('Base._aq_dynamic', WARNING,
             'unable to create methods for PreferenceTool', e)
         raise
       Base.aq_preference_generated = 1
-
+    
     # Always try to return something after generation
     if generated:
       # We suppose that if we reach this point
-- 
2.30.9