Back to index

plone3  3.1.7
selection.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 """ Topic:
00020 
00021 """
00022 
00023 __author__  = 'Alec Mitchell'
00024 __docformat__ = 'restructuredtext'
00025 __old_name__ = 'Products.ATContentTypes.types.criteria.ATSelectionCriterion'
00026 
00027 from Products.CMFCore.permissions import View
00028 from Products.CMFCore.utils import getToolByName
00029 from AccessControl import ClassSecurityInfo
00030 
00031 from Products.Archetypes.atapi import Schema
00032 from Products.Archetypes.atapi import LinesField
00033 from Products.Archetypes.atapi import MultiSelectionWidget
00034 from Products.Archetypes.atapi import StringField
00035 from Products.Archetypes.atapi import SelectionWidget
00036 from Products.Archetypes.atapi import DisplayList
00037 
00038 from Products.ATContentTypes.criteria import registerCriterion
00039 from Products.ATContentTypes.criteria import LIST_INDICES
00040 from Products.ATContentTypes.interfaces import IATTopicSearchCriterion
00041 from Products.ATContentTypes.permission import ChangeTopics
00042 from Products.ATContentTypes.criteria.base import ATBaseCriterion
00043 from Products.ATContentTypes.criteria.schemata import ATBaseCriterionSchema
00044 
00045 from Products.ATContentTypes import ATCTMessageFactory as _
00046 
00047 CompareOperators = DisplayList((
00048                     ('and', _(u'and'))
00049                   , ('or', _(u'or'))
00050     ))
00051 
00052 ATSelectionCriterionSchema = ATBaseCriterionSchema + Schema((
00053     LinesField('value',
00054                 required=1,
00055                 mode="rw",
00056                 write_permission=ChangeTopics,
00057                 accessor="Value",
00058                 mutator="setValue",
00059                 default=[],
00060                 vocabulary="getCurrentValues",
00061                 widget=MultiSelectionWidget(
00062                     label=_(u'label_criteria_values', default=u'Values'),
00063                     description=_(u'help_criteria_values', default=u'Existing values.')
00064                     ),
00065                 ),
00066     StringField('operator',
00067                 required=1,
00068                 mode="rw",
00069                 write_permission=ChangeTopics,
00070                 default='or',
00071                 vocabulary=CompareOperators,
00072                 widget=SelectionWidget(
00073                     label=_(u'label_list_criteria_operator', default=u'operator name'),
00074                     description=_(u'help_list_criteria_operator',
00075                                   default=u'Operator used to join the tests on each value.')
00076                     ),
00077                 ),
00078     ))
00079 
00080 class ATSelectionCriterion(ATBaseCriterion):
00081     """A selection criterion"""
00082 
00083     __implements__ = ATBaseCriterion.__implements__ + (IATTopicSearchCriterion, )
00084     security       = ClassSecurityInfo()
00085     schema         = ATSelectionCriterionSchema
00086     meta_type      = 'ATSelectionCriterion'
00087     archetype_name = 'Selection Criterion'
00088     shortDesc      = 'Select values from list'
00089 
00090     def getCurrentValues(self):
00091         catalog = getToolByName(self, 'portal_catalog')
00092         options = catalog.uniqueValuesFor(self.Field())
00093         # AT is currently broken, and does not accept ints as
00094         # DisplayList keys though it is supposed to (it should
00095         # probably accept Booleans as well) so we only accept strings
00096         # for now
00097         options = [(o.lower(),o) for o in options if isinstance(o, basestring)]
00098         options.sort()
00099         return [o[1] for o in options]
00100 
00101     security.declareProtected(View, 'getCriteriaItems')
00102     def getCriteriaItems(self):
00103         # filter out empty strings
00104         result = []
00105 
00106         value = tuple([ value for value in self.Value() if value ])
00107         if not value:
00108             return ()
00109         result.append((self.Field(), { 'query': value, 'operator': self.getOperator()}),)
00110 
00111         return tuple(result)
00112 
00113 registerCriterion(ATSelectionCriterion, LIST_INDICES)