Back to index

plone3  3.1.7
SimpleIntCriterion.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 """ Simple int-matching criterion
00014 
00015 $Id: SimpleIntCriterion.py 38590 2005-09-24 15:24:32Z 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 SimpleIntCriterion( AbstractCriterion ):
00031 
00032     """
00033         Represent a simple field-match for an integer value, including
00034         catalog range searches.
00035     """
00036 
00037     implements(ICriterion)
00038     __implements__ = z2ICriterion
00039 
00040     meta_type = 'Integer Criterion'
00041 
00042     security = ClassSecurityInfo()
00043     _editableAttributes = ( 'value', 'direction' )
00044 
00045     MINIMUM = 'min'
00046     MAXIMUM = 'max'
00047     MINMAX = 'min:max'
00048 
00049     def __init__(self, id, field):
00050         self.id = id
00051         self.field = field
00052         self.value = self.direction = None
00053 
00054     security.declareProtected( ChangeTopics, 'getEditForm' )
00055     def getEditForm( self ):
00056         """
00057             Return the name of skin method which renders the form
00058             used to edit this kind of criterion.
00059         """
00060         return 'sic_edit'
00061 
00062     security.declareProtected( ChangeTopics, 'getValueString' )
00063     def getValueString( self ):
00064         """
00065             Return a string representation of the value for which this
00066             criterion filters.
00067         """
00068         if self.value is None:
00069             return ''
00070 
00071         if self.direction == self.MINMAX:
00072 
00073             value = self.value
00074 
00075             if type( value ) is not type( () ):
00076                 value = ( value, value )
00077 
00078             return '%s %s' % value
00079 
00080         return str( self.value )
00081 
00082     security.declareProtected( ChangeTopics, 'edit' )
00083     def edit( self, value, direction=None ):
00084         """
00085             Update the value to be filtered, and the "direction" qualifier.
00086         """
00087 
00088         if type( value ) == type( '' ):
00089            value = value.strip()
00090 
00091         if not value:
00092             # An empty string was passed in, which evals to None
00093             self.value = self.direction = None
00094 
00095         elif direction:
00096 
00097             if direction == self.MINMAX:
00098 
00099                 if type( value ) == type( '' ):
00100                     minimum, maximum = value.split(' ')
00101                 else:
00102                     minimum, maximum = value
00103 
00104                 self.value = ( int( minimum ), int( maximum ) )
00105 
00106             else:
00107                 self.value = int( value )
00108 
00109             self.direction = direction
00110 
00111         else:
00112             self.value = int( value )
00113             self.direction = None
00114 
00115     security.declareProtected(View, 'getCriteriaItems')
00116     def getCriteriaItems( self ):
00117         """
00118             Return a tuple of query elements to be passed to the catalog
00119             (used by 'Topic.buildQuery()').
00120         """
00121         if self.value is None:
00122             return ()
00123         elif self.direction is None:
00124             return ( ( self.Field(), self.value ), )
00125         else:
00126             return ( ( self.Field(), {'query': self.value,
00127                                       'range': self.direction} ), )
00128 
00129 InitializeClass( SimpleIntCriterion )
00130 
00131 
00132 # Register as a criteria type with the Topic class
00133 Topic._criteriaTypes.append( SimpleIntCriterion )