Back to index

plone3  3.1.7
SortCriterion.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Allow topic to specify sorting.
00014 
00015 $Id: SortCriterion.py 77186 2007-06-28 19:06:19Z yuppie $
00016 """
00017 
00018 from AccessControl import ClassSecurityInfo
00019 from Globals import InitializeClass
00020 from zope.interface import implements
00021 
00022 from AbstractCriterion import AbstractCriterion
00023 from interfaces import Criterion as z2ICriterion
00024 from interfaces import ICriterion
00025 from permissions import ChangeTopics
00026 from permissions import View
00027 from Topic import Topic
00028 
00029 
00030 class SortCriterion( AbstractCriterion ):
00031 
00032     """
00033         Represent a mock criterion, to allow spelling the sort order
00034         and reversal items in a catalog query.
00035     """
00036 
00037     implements(ICriterion)
00038     __implements__ = z2ICriterion
00039 
00040     meta_type = 'Sort Criterion'
00041 
00042     security = ClassSecurityInfo()
00043 
00044     field = None # Don't prevent use of field in other criteria
00045 
00046     _editableAttributes = ( 'reversed', )
00047 
00048     def __init__( self, id, index ):
00049         self.id = id
00050         self.index = index
00051         self.reversed = 0
00052 
00053     # inherit permissions
00054     def Field( self ):
00055         """
00056             Map the stock Criterion interface.
00057         """
00058         return self.index
00059 
00060     security.declareProtected( ChangeTopics, 'getEditForm' )
00061     def getEditForm( self ):
00062         """
00063             Return the name of skin method which renders the form
00064             used to edit this kind of criterion.
00065         """
00066         return 'sort_edit'
00067 
00068     security.declareProtected( ChangeTopics, 'edit' )
00069     def edit( self, reversed ):
00070         """
00071             Update the value we are to match up against.
00072         """
00073         self.reversed = bool(reversed)
00074 
00075     security.declareProtected(View, 'getCriteriaItems')
00076     def getCriteriaItems( self ):
00077         """
00078             Return a tuple of query elements to be passed to the catalog
00079             (used by 'Topic.buildQuery()').
00080         """
00081         result = [ ( 'sort_on', self.index ) ]
00082 
00083         if self.reversed:
00084             result.append( ( 'sort_order', 'reverse' ) )
00085 
00086         return tuple( result )
00087 
00088 InitializeClass( SortCriterion )
00089 
00090 # Register as a criteria type with the Topic class
00091 Topic._criteriaTypes.append( SortCriterion )