Back to index

plone3  3.1.7
ListCriterion.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 """ List Criterion: A criterion that is a list
00014 
00015 $Id: ListCriterion.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 ListCriterion( AbstractCriterion ):
00031 
00032     """
00033         Represent a criterion which is a list of values (for an
00034         'OR' search).
00035     """
00036 
00037     implements(ICriterion)
00038     __implements__ = z2ICriterion
00039 
00040     meta_type = 'List Criterion'
00041     operator = None
00042     value = ( '', )
00043 
00044     security = ClassSecurityInfo()
00045 
00046     _editableAttributes = ( 'value', 'operator' )
00047 
00048     def __init__( self, id, field ):
00049         self.id = id
00050         self.field = field
00051         self._clear()
00052 
00053     security.declarePrivate( '_clear' )
00054     def _clear( self ):
00055         """
00056             Restore to original value.
00057         """
00058         self.value = ( '', )    # *Not* '()', which won't do at all!
00059         self.operator = None
00060 
00061     security.declareProtected( ChangeTopics, 'getEditForm' )
00062     def getEditForm( self ):
00063         """
00064             Return the name of skin method which renders the form
00065             used to edit this kind of criterion.
00066         """
00067         return "listc_edit"
00068 
00069     security.declareProtected( ChangeTopics, 'edit' )
00070     def edit( self, value=None, operator=None ):
00071         """
00072             Update the value we match against.
00073         """
00074         if value is None:
00075             self._clear()
00076         else:
00077             if type( value ) == type( '' ):
00078                 value = value.split('\n')
00079             self.value = tuple( value )
00080 
00081         if not operator:
00082             operator = None
00083 
00084         self.operator = operator
00085 
00086     security.declareProtected(View, 'getCriteriaItems')
00087     def getCriteriaItems( self ):
00088         """
00089             Return a tuple of query elements to be passed to the catalog
00090             (used by 'Topic.buildQuery()').
00091         """
00092         # filter out empty strings
00093         result = []
00094 
00095         value = tuple( filter( None, self.value ) )
00096         if not value:
00097             return ()
00098         result.append( ( self.field, self.value ), )
00099 
00100         if self.operator is not None:
00101             result.append( ( '%s_operator' % self.field, self.operator ) )
00102 
00103         return tuple( result )
00104 
00105 InitializeClass( ListCriterion )
00106 
00107 # Register as a criteria type with the Topic class
00108 Topic._criteriaTypes.append( ListCriterion )