Back to index

plone3  3.1.7
simpleint.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # ATContentTypes http://plone.org/products/atcontenttypes/
00004 # Archetypes reimplementation of the CMF core types
00005 #
00006 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00007 # Copyright (c) 2003-2006 AT Content Types development team
00008 #
00009 # This software is subject to the provisions of the Zope Public License,
00010 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
00011 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00012 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00013 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00014 # FOR A PARTICULAR PURPOSE
00015 #
00016 ##############################################################################
00017 """ Topic:
00018 
00019 """
00020 
00021 __author__  = 'Christian Heimes <tiran@cheimes.de>'
00022 __docformat__ = 'restructuredtext'
00023 __old_name__ = 'Products.ATContentTypes.types.criteria.ATSimpleIntCriterion'
00024 
00025 from Products.CMFCore.permissions import View
00026 from AccessControl import ClassSecurityInfo
00027 
00028 from Products.Archetypes.atapi import Schema
00029 from Products.Archetypes.atapi import IntegerField
00030 from Products.Archetypes.atapi import IntegerWidget
00031 from Products.Archetypes.atapi import StringField
00032 from Products.Archetypes.atapi import SelectionWidget
00033 from Products.Archetypes.atapi import DisplayList
00034 
00035 from Products.ATContentTypes.criteria import registerCriterion
00036 from Products.ATContentTypes.criteria import LIST_INDICES
00037 from Products.ATContentTypes.interfaces import IATTopicSearchCriterion
00038 from Products.ATContentTypes.permission import ChangeTopics
00039 from Products.ATContentTypes.criteria.base import ATBaseCriterion
00040 from Products.ATContentTypes.criteria.schemata import ATBaseCriterionSchema
00041 
00042 from Products.ATContentTypes import ATCTMessageFactory as _
00043 
00044 DirectionOperations = DisplayList((
00045                     ('', _(u'Equal to'))
00046                   , ('min', _(u'Greater than'))
00047                   , ('max', _(u'Less than'))
00048                   , ('min:max', _(u'Between'))
00049     ))
00050 
00051 ATSimpleIntCriterionSchema = ATBaseCriterionSchema + Schema((
00052     IntegerField('value',
00053                 required=1,
00054                 mode="rw",
00055                 write_permission=ChangeTopics,
00056                 accessor="Value",
00057                 mutator="setValue",
00058                 default=None,
00059                 widget=IntegerWidget(
00060                     label=_(u'label_int_criteria_value', default=u'Value'),
00061                     description=_(u'help_int_criteria_value',
00062                                   default=u'An integer number.')
00063                     ),
00064                 ),
00065     IntegerField('value2',
00066                 required=0,
00067                 mode="rw",
00068                 write_permission=ChangeTopics,
00069                 accessor="Value2",
00070                 mutator="setValue2",
00071                 default=None,
00072                 widget=IntegerWidget(
00073                     label=_(u'label_int_criteria_value2', default=u'Second Value'),
00074                     description=_(u'help_int_criteria_value2',
00075                                   default=u'An integer number used as the maximum value if the between direction is selected.')
00076                     ),
00077                 ),
00078     StringField('direction',
00079                 required=0,
00080                 mode="rw",
00081                 write_permission=ChangeTopics,
00082                 default='',
00083                 vocabulary=DirectionOperations,
00084                 enforceVocabulary=1,
00085                 widget=SelectionWidget(
00086                     label=_(u'label_int_criteria_direction', default=u'Direction'),
00087                     description=_(u'help_int_criteria_direction',
00088                                   default=u'Specify whether you want to find values lesser than, greater than, equal to, or between the chosen value(s).')
00089                     ),
00090                 ),
00091     ))
00092 
00093 class ATSimpleIntCriterion(ATBaseCriterion):
00094     """A simple int criterion"""
00095 
00096     __implements__ = ATBaseCriterion.__implements__ + (IATTopicSearchCriterion, )
00097     security       = ClassSecurityInfo()
00098     schema         = ATSimpleIntCriterionSchema
00099     meta_type      = 'ATSimpleIntCriterion'
00100     archetype_name = 'Simple Int Criterion'
00101     shortDesc      = 'Integer value or range'
00102 
00103     security.declareProtected(View, 'getCriteriaItems')
00104     def getCriteriaItems(self):
00105         result = []
00106         val = self.Value()
00107         direction = self.getDirection()
00108         if val or val == 0:
00109             if direction == 'min:max':
00110                 val = tuple([int(val), int(self.Value2())])
00111             else:
00112                 val = int(val)
00113             if direction:
00114                 result.append((self.Field(), {'query': val,  'range': direction}))
00115             else:
00116                 result.append((self.Field(), {'query': val}))
00117 
00118         return tuple(result)
00119 
00120     security.declareProtected(View, 'post_validate')
00121     def post_validate(self, REQUEST, errors):
00122         """Check that Value2 is set if range is set to min:max"""
00123         direction = REQUEST.get('direction', self.getDirection())
00124         val2 = REQUEST.get('value2', self.Value2())
00125         if direction == 'min:max' and not val2 and not val2 == 0:
00126             errors['value2']='You must enter a second value to do a "Between" search.'
00127         errors['value2']='You must enter a second value to do a "Between" search.'
00128         return errors
00129 
00130 registerCriterion(ATSimpleIntCriterion, LIST_INDICES)