Back to index

plone3  3.1.7
autosort.py
Go to the documentation of this file.
00001 #  ATContentTypes http://plone.org/products/atcontenttypes/
00002 #  Archetypes reimplementation of the CMF core types
00003 #  Copyright (c) 2003-2006 AT Content Types development team
00004 #
00005 #  This program is free software; you can redistribute it and/or modify
00006 #  it under the terms of the GNU General Public License as published by
00007 #  the Free Software Foundation; either version 2 of the License, or
00008 #  (at your option) any later version.
00009 #
00010 #  This program is distributed in the hope that it will be useful,
00011 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 #  GNU General Public License for more details.
00014 #
00015 #  You should have received a copy of the GNU General Public License
00016 #  along with this program; if not, write to the Free Software
00017 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 #
00019 """Auto sorting / auto ordering support
00020 """
00021 __author__  = 'Christian Heimes <tiran@cheimes.de>'
00022 __docformat__ = 'restructuredtext'
00023 
00024 from ExtensionClass import Base
00025 from Globals import InitializeClass
00026 from Products.CMFCore.permissions import View
00027 from Products.CMFCore.permissions import ModifyPortalContent
00028 from Products.CMFCore.utils import getToolByName
00029 from AccessControl import ClassSecurityInfo
00030 _marker = object()
00031 from Products.Archetypes.OrderedBaseFolder import OrderedBaseFolder
00032 from Products.Archetypes.OrderedBaseFolder import OrderedContainer
00033 from Products.ATContentTypes.interfaces import IAutoSortSupport
00034 from Products.ATContentTypes.interfaces import IAutoOrderSupport
00035 # implementation
00036 
00037 class AutoSortSupport(Base):
00038 
00039     __implements__ = (IAutoSortSupport, )
00040 
00041     security = ClassSecurityInfo()
00042 
00043     _default_sort_key = 'Title'
00044     _default_sort_reverse = False
00045     _default_sort_folderish_first = True
00046     _default_sort_auto = True
00047 
00048     security.declareProtected(ModifyPortalContent, 'setDefaultSorting')
00049     def setDefaultSorting(self, key, reverse=_marker):
00050         """Set default sorting key and direction.
00051         """
00052         self._default_sort_key = key
00053         if reverse is not _marker:
00054             self._default_sort_reverse = bool(reverse)
00055 
00056     security.declareProtected(View, 'getDefaultSorting')
00057     def getDefaultSorting(self):
00058         """Get default sorting key and direction.
00059         """
00060         return self._default_sort_key, self._default_sort_reverse
00061 
00062     security.declareProtected(ModifyPortalContent, 'setSortFolderishFirst')
00063     def setSortFolderishFirst(self, value):
00064         """Set the value for sorting folderish objects before ordinary items
00065         """
00066         self._default_sort_folderish_first = bool(value)
00067 
00068     security.declareProtected(View, 'getSortFolderishFirst')
00069     def getSortFolderishFirst(self):
00070         """Get the value for sorting folderish objects before ordinary items
00071         """
00072         return self._default_sort_folderish_first
00073 
00074     security.declareProtected(ModifyPortalContent, 'setSortReverse')
00075     def setSortReverse(self, value):
00076         """Set reverse sort setting
00077         """
00078         self._default_sort_reverse = bool(value)
00079 
00080     security.declareProtected(View, 'getSortReverse')
00081     def getSortReverse(self):
00082         """Get reverse sort setting
00083         """
00084         return self._default_sort_reverse
00085 
00086     security.declareProtected(ModifyPortalContent, 'setSortAuto')
00087     def setSortAuto(self, value):
00088        """Set auto sort setting
00089        """
00090        self._default_sort_auto = bool(value)
00091 
00092     security.declareProtected(View, 'getSortAuto')
00093     def getSortAuto(self):
00094         """Get auto sort setting
00095         """
00096         return self._default_sort_auto
00097 
00098 InitializeClass(AutoSortSupport)
00099 
00100 
00101 class AutoOrderSupport(AutoSortSupport, OrderedContainer):
00102 
00103     __implements__ = (IAutoOrderSupport, )
00104 
00105     security = ClassSecurityInfo()
00106 
00107     security.declarePrivate('autoOrderItems')
00108     def autoOrderItems(self):
00109         """Auto order all containing items according to the settings
00110         """
00111         if not self.getSortAuto():
00112             return
00113         key, reverse = self.getDefaultSorting()
00114         folderish_first = self.getSortFolderishFirst()
00115 
00116         self.orderObjects(key, reverse)
00117         # TODO: folderish first is missing, use the catalog to resort it!
00118 
00119     security.declareProtected(ModifyPortalContent, 'moveObject')
00120     def moveObject(self, id, position):
00121         obj_idx  = self.getObjectPosition(id)
00122         if obj_idx == position:
00123             return None
00124         elif position < 0:
00125             position = 0
00126 
00127         metadata = list(self._objects)
00128         obj_meta = metadata.pop(obj_idx)
00129         metadata.insert(position, obj_meta)
00130         self._objects = tuple(metadata)
00131 
00132     def moveObjectsByDelta(self, ids, delta, subset_ids=None, disable_auto_sort=True):
00133         """Move specified sub-objects by delta.
00134 
00135         Overwritten to disable auto sort
00136         """
00137         OrderedContainer.moveObjectsByDelta(self, ids, delta, subset_ids=subset_ids)
00138         self.setSortAuto(disable_auto_sort)
00139 
00140     def manage_renameObject(self, id, new_id, REQUEST=None):
00141         """Rename a particular sub-object without changing its position.
00142 
00143         Overwritten to keep auto sort
00144         """
00145         old_position = self.getObjectPosition(id)
00146         old_sort_auto = self.getSortAuto()
00147         result = OrderedBaseFolder.manage_renameObject(self, id, new_id, REQUEST)
00148         self.moveObjectToPosition(new_id, old_position)
00149         putils = getToolByName(self, 'plone_utils')
00150         putils.reindexOnReorder(self)
00151         self.setSortAuto(old_sort_auto)
00152         return result
00153 
00154     security.declarePrivate('manage_afterAdd')
00155     def manage_afterAdd(self, item, container):
00156         """after add hook
00157 
00158         Overwritten to auto sort items
00159         CAUTION: Make sure that you call this method explictly!
00160         """
00161         # XXX: disabled
00162         # we need a proper event system to make it work
00163         #if item.aq_inner.aq_parent == self:
00164         #    self.autoOrderItems()
00165 
00166 InitializeClass(AutoOrderSupport)