Back to index

plone3  3.1.7
KeepLastNVersionsTool.py
Go to the documentation of this file.
00001 #########################################################################
00002 # Copyright (c) 2004, 2005 Alberto Berti, Gregoire Weber. 
00003 # All Rights Reserved.
00004 # 
00005 # This file is part of CMFEditions.
00006 # 
00007 # CMFEditions is free software; you can redistribute it and/or modify
00008 # it under the terms of the GNU General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or
00010 # (at your option) any later version.
00011 # 
00012 # CMFEditions is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 # GNU General Public License for more details.
00016 # 
00017 # You should have received a copy of the GNU General Public License
00018 # along with CMFEditions; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00020 #########################################################################
00021 """Storage Purge Policy Tool Keeping Only the Last n Versions
00022 
00023 $Id$
00024 """
00025 __version__ = "$Revision$"
00026 
00027 from zope.interface import implements
00028 
00029 from Globals import InitializeClass
00030 from AccessControl import ClassSecurityInfo
00031 
00032 from OFS.PropertyManager import PropertyManager
00033 from OFS.SimpleItem import SimpleItem
00034 
00035 from Products.CMFCore.utils import UniqueObject
00036 from Products.CMFCore.utils import getToolByName
00037 
00038 from Products.CMFEditions.interfaces import IPurgePolicyTool
00039 from Products.CMFEditions.interfaces.IPurgePolicy import IPurgePolicy
00040 
00041 class KeepLastNVersionsTool(UniqueObject, SimpleItem, PropertyManager):
00042     """
00043     """
00044 
00045     __implements__ = (
00046         IPurgePolicy,
00047         SimpleItem.__implements__,
00048     )
00049     implements(IPurgePolicyTool)
00050 
00051     id = 'portal_purgepolicy'
00052     alternative_id = 'portal_keeplastnversions'
00053     
00054     meta_type = "CMFEditions Purge Policy Keeping Only the n last Versions"
00055     
00056     manage_options = PropertyManager.manage_options \
00057       + SimpleItem.manage_options
00058 
00059     maxNumberOfVersionsToKeep = -1 # disabled
00060     
00061     _properties = (
00062         {'id': 'maxNumberOfVersionsToKeep', 'type': 'int', 'mode': 'w',
00063          'label': "maximum number of versions to keep in the storage (set to -1 for infinite)"},
00064     )
00065     security = ClassSecurityInfo()
00066     
00067     # -------------------------------------------------------------------
00068     # methods implementing IPurgePolicy
00069     # -------------------------------------------------------------------
00070 
00071     def beforeSaveHook(self, history_id, obj, metadata={}):
00072         """Purge all but the n most current versions
00073         
00074         Purges old version so that at maximum ``maxNumberOfVersionsToKeep`` 
00075         versions reside in the history.
00076         """
00077         # check if infinite number of versions shall be stored
00078         if self.maxNumberOfVersionsToKeep == -1:
00079             # infinite: do nothing
00080             return True
00081             
00082         storage = getToolByName(self, 'portal_historiesstorage')
00083         currentVersion = len(storage.getHistory(history_id))
00084         while True:
00085             length = len(storage.getHistory(history_id, countPurged=False))
00086             if length < self.maxNumberOfVersionsToKeep:
00087                 break
00088             comment = "purged on save of version %s" % currentVersion
00089             storage.purge(history_id, 0, metadata={'sys_metadata': {
00090                                                            'comment': comment}
00091                                                    },
00092                           countPurged=False)
00093         
00094         # save current version
00095         return True
00096     
00097     def retrieveSubstitute(self, history_id, selector, default=None):
00098         """Retrives the next older version
00099         
00100         If there isn't a next older one returns the next newer one.
00101         """
00102         selector = int(selector)
00103         storage = getToolByName(self, 'portal_historiesstorage')
00104         savedSelector = selector
00105         while selector:
00106             selector -= 1
00107             data = storage.retrieve(history_id, selector, substitute=False)
00108             if data.isValid():
00109                 return data
00110                 
00111         selector = savedSelector
00112         while True:
00113             selector += 1
00114             try:
00115                 data = storage.retrieve(history_id, selector, substitute=False)
00116             except storage.StorageRetrieveError:
00117                 break
00118             
00119             if data.isValid():
00120                 return data
00121                 
00122         return default
00123 
00124 InitializeClass(KeepLastNVersionsTool)