Back to index

plone3  3.1.7
date.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 
00022 __author__  = 'Christian Heimes <tiran@cheimes.de>'
00023 __docformat__ = 'restructuredtext'
00024 __old_name__ = 'Products.ATContentTypes.types.criteria.ATDateCriteria'
00025 
00026 from DateTime import DateTime
00027 from Products.CMFCore.permissions import View
00028 from AccessControl import ClassSecurityInfo
00029 
00030 from Products.Archetypes.atapi import Schema
00031 from Products.Archetypes.atapi import IntegerField
00032 from Products.Archetypes.atapi import StringField
00033 from Products.Archetypes.atapi import SelectionWidget
00034 from Products.Archetypes.atapi import DisplayList
00035 from Products.Archetypes.atapi import IntDisplayList
00036 
00037 from Products.ATContentTypes.criteria import registerCriterion
00038 from Products.ATContentTypes.criteria import DATE_INDICES
00039 from Products.ATContentTypes.interfaces import IATTopicSearchCriterion
00040 from Products.ATContentTypes.permission import ChangeTopics
00041 from Products.ATContentTypes.criteria.base import ATBaseCriterion
00042 from Products.ATContentTypes.criteria.schemata import ATBaseCriterionSchema
00043 
00044 from Products.ATContentTypes import ATCTMessageFactory as _
00045 
00046 DateOptions = IntDisplayList((
00047                     (     0, _(u'Now')      )
00048                   , (     1, _(u'1 Day')    )
00049                   , (     2, _(u'2 Days')   )
00050                   , (     5, _(u'5 Days')   )
00051                   , (     7, _(u'1 Week')   )
00052                   , (    14, _(u'2 Weeks')  )
00053                   , (    31, _(u'1 Month')  )
00054                   , (  31*3, _(u'3 Months') )
00055                   , (  31*6, _(u'6 Months') )
00056                   , (   365, _(u'1 Year')   )
00057                   , ( 365*2, _(u'2 Years')  )
00058     ))
00059 
00060 CompareOperations = DisplayList((
00061                     ('more', _(u'More than'))
00062                   , ('less', _(u'Less than'))
00063                   , ('within_day', _(u'On the day'))
00064     ))
00065 
00066 RangeOperations = DisplayList((
00067                     ('-', _(u'in the past'))
00068                   , ('+', _(u'in the future'))
00069     ))
00070 
00071 ATDateCriteriaSchema = ATBaseCriterionSchema + Schema((
00072     IntegerField('value',
00073                 required=1,
00074                 mode="rw",
00075                 accessor="Value",
00076                 mutator="setValue",
00077                 write_permission=ChangeTopics,
00078                 default=None,
00079                 vocabulary=DateOptions,
00080                 widget=SelectionWidget(
00081                     label=_(u'label_date_criteria_value', default=u'Which day'),
00082                     description=_(u'help_date_criteria_value',
00083                                   default=u'Select the date criteria value.')
00084                     ),
00085                 ),
00086     StringField('dateRange',
00087                 required=1,
00088                 mode="rw",
00089                 write_permission=ChangeTopics,
00090                 default=None,
00091                 vocabulary=RangeOperations,
00092                 enforceVocabulary=1,
00093                 widget=SelectionWidget(
00094                     label=_(u'label_date_criteria_range',
00095                             default=u'In the past or future'),
00096                     description=_(u'help_date_criteria_range',
00097                                   default=u"Select the date criteria range. Ignore this if you selected 'Now' above."),
00098                     format="select"),
00099                 ),
00100     StringField('operation',
00101                 required=1,
00102                 mode="rw",
00103                 default=None,
00104                 write_permission=ChangeTopics,
00105                 vocabulary=CompareOperations,
00106                 enforceVocabulary=1,
00107                 widget=SelectionWidget(
00108                     label=_(u'label_date_criteria_operation', default=u'More or less'),
00109                     description=_(u'help_date_criteria_operation',
00110                                   default=u'Select the date criteria operation.'),
00111                     format="select"),
00112                 ),
00113     ))
00114 
00115 class ATDateCriteria(ATBaseCriterion):
00116     """A relative date criterion"""
00117 
00118 
00119     __implements__ = ATBaseCriterion.__implements__ + (IATTopicSearchCriterion, )
00120 
00121     security       = ClassSecurityInfo()
00122     schema         = ATDateCriteriaSchema
00123     meta_type      = 'ATFriendlyDateCriteria'
00124     archetype_name = 'Friendly Date Criteria'
00125     shortDesc      = 'Relative date'
00126 
00127     security.declareProtected(View, 'getCriteriaItems')
00128     def getCriteriaItems(self):
00129         """Return a sequence of items to be used to build the catalog query.
00130         """
00131         if self.value is not None:
00132             field = self.Field()
00133             value = self.Value()
00134 
00135             # Negate the value for 'old' days
00136             if self.getDateRange() == '-':
00137                 value = -value
00138 
00139             date = DateTime() + value
00140             current_date = DateTime()
00141 
00142             operation = self.getOperation()
00143             if operation == 'within_day':
00144                 date_range = ( date.earliestTime(), date.latestTime() )
00145                 return ( ( field, {'query': date_range, 'range': 'min:max'} ), )
00146             elif operation == 'more':
00147                 if value != 0:
00148                     range_op = (self.getDateRange() == '-' and 'max') or 'min'
00149                     return ( ( field, {'query': date.earliestTime(), 'range': range_op} ), )
00150                 else:
00151                     return ( ( field, {'query': date, 'range': 'min'} ), )
00152             elif operation == 'less':
00153                 if value != 0:
00154                     date_range = (self.getDateRange() == '-' and
00155                                   (date.earliestTime(), current_date)
00156                                   ) or (current_date, date.latestTime())
00157                     return ( ( field, {'query': date_range, 'range': 'min:max'} ), )
00158                 else:
00159                     return ( ( field, {'query': date, 'range': 'max'} ), )
00160         else:
00161             return ()
00162 
00163 registerCriterion(ATDateCriteria, DATE_INDICES)