From d2fdd47ce3f0500660044963f54247ff027c1d03 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Mon, 6 Nov 2006 15:04:18 +0000
Subject: [PATCH] Preference now clear the cache automatically when edited and
 when the workflow state change.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11126 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../scripts/disableOtherPreferences.xml       |  5 +----
 product/ERP5/bootstrap/erp5_core/bt/revision  |  2 +-
 product/ERP5Form/Document/Preference.py       | 20 +++++++++++++++++++
 product/ERP5Form/tests/testPreferences.py     | 16 ++++++---------
 4 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/product/ERP5/bootstrap/erp5_core/WorkflowTemplateItem/portal_workflow/preference_workflow/scripts/disableOtherPreferences.xml b/product/ERP5/bootstrap/erp5_core/WorkflowTemplateItem/portal_workflow/preference_workflow/scripts/disableOtherPreferences.xml
index 5e97df4391..c30da8d33a 100644
--- a/product/ERP5/bootstrap/erp5_core/WorkflowTemplateItem/portal_workflow/preference_workflow/scripts/disableOtherPreferences.xml
+++ b/product/ERP5/bootstrap/erp5_core/WorkflowTemplateItem/portal_workflow/preference_workflow/scripts/disableOtherPreferences.xml
@@ -68,8 +68,7 @@
         </item>
         <item>
             <key> <string>_body</string> </key>
-            <value> <string>from Products.ERP5Type.Cache import clearCache\n
-from Products.ERP5Type.Message import Message\n
+            <value> <string>from Products.ERP5Type.Message import Message\n
 \n
 pref = sci.object\n
 portal = sci.getPortal()\n
@@ -91,8 +90,6 @@ for p in portal.portal_preferences.searchFolder(spec=(\'ERP5 Preference\',)) :\n
                                    mapping={\'preference_title\': context.getTitle()}) )\n
   except \'Unauthorized\', a :\n
     LOG("not authorized to deactivate %s"%p)\n
-\n
-clearCache(method_id="PreferenceTool.CachingMethod.")\n
 </string> </value>
         </item>
         <item>
diff --git a/product/ERP5/bootstrap/erp5_core/bt/revision b/product/ERP5/bootstrap/erp5_core/bt/revision
index b912dc118c..34bba940d1 100644
--- a/product/ERP5/bootstrap/erp5_core/bt/revision
+++ b/product/ERP5/bootstrap/erp5_core/bt/revision
@@ -1 +1 @@
-155
\ No newline at end of file
+156
\ No newline at end of file
diff --git a/product/ERP5Form/Document/Preference.py b/product/ERP5Form/Document/Preference.py
index bcb7441979..fbb80a2df4 100644
--- a/product/ERP5Form/Document/Preference.py
+++ b/product/ERP5Form/Document/Preference.py
@@ -30,6 +30,7 @@ from AccessControl import ClassSecurityInfo
 
 from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
 from Products.ERP5Type.Document.Folder import Folder
+from Products.ERP5Type.Cache import clearCache
 
 class Priority:
   """ names for priorities """
@@ -63,3 +64,22 @@ class Preference( Folder ):
   security = ClassSecurityInfo()
   security.declareObjectProtected(Permissions.AccessContentsInformation)
 
+  def _clearCache(self):
+    """Clear caches used by methods of this preference
+    # TODO: clear different caches according to the preference priority
+    """
+    clearCache()
+
+  def _edit(self, **kw):
+    """edit and clear all caches"""
+    self._clearCache()
+    Folder._edit(self, **kw)
+
+  def enable(self):
+    """Workflow method"""
+    self._clearCache()
+
+  def disable(self):
+    """Workflow method"""
+    self._clearCache()
+  
diff --git a/product/ERP5Form/tests/testPreferences.py b/product/ERP5Form/tests/testPreferences.py
index 7c23bca1c9..2ec06d07cf 100644
--- a/product/ERP5Form/tests/testPreferences.py
+++ b/product/ERP5Form/tests/testPreferences.py
@@ -173,14 +173,12 @@ class TestPreferences(ERP5TypeTestCase):
     self.assertEquals(
       site.getPreferredAccountingTransactionSimulationState(), None)
 
-    from Products.ERP5Type.Cache import clearCache
-    clearCache()
     self.assertEquals(len(pref_tool.getPreference(
       'preferred_accounting_transaction_simulation_state_list')), 0)
     
-    site.setPreferredAccountingTransactionSimulationStateList(
-            ['stopped', 'delivered'])
-    clearCache() # FIXME: the cache should be cleared automatically
+    site.edit(
+      preferred_accounting_transaction_simulation_state_list=
+      ['stopped', 'delivered'])
     self.assertEquals(list(pref_tool.getPreference(
       'preferred_accounting_transaction_simulation_state_list')),
       list(site.getPreferredAccountingTransactionSimulationStateList()))
@@ -193,22 +191,20 @@ class TestPreferences(ERP5TypeTestCase):
       self.assertEquals(pref_tool.getPreference(prop),
                         site.getProperty(prop))
     
-    group.setPreferredAccountingTransactionSimulationStateList(['draft'])
-    clearCache()
+    group.edit(
+      preferred_accounting_transaction_simulation_state_list=['draft'])
     self.assertEquals(list(pref_tool.getPreference(
       'preferred_accounting_transaction_simulation_state_list')),
       list(group.getPreferredAccountingTransactionSimulationStateList()))
     
-    person1.setPreferredAccountingTransactionSimulationStateList(
+    person1.edit(preferred_accounting_transaction_simulation_state_list=
               ['cancelled'])
-    clearCache()
     self.assertEquals(list(pref_tool.getPreference(
       'preferred_accounting_transaction_simulation_state_list')),
       list(person1.getPreferredAccountingTransactionSimulationStateList()))
     # disable person -> group is selected
     self.getWorkflowTool().doActionFor(person1,
             'disable_action', wf_id='preference_workflow')
-    clearCache()
     self.assertEquals(list(pref_tool.getPreference(
       'preferred_accounting_transaction_simulation_state_list')),
       list(group.getPreferredAccountingTransactionSimulationStateList()))
-- 
2.30.9