vendor/CMF/1.5.5-beta/CMFTopic

view DateCriteria.py @ 0:4341d8feb2f9

Vendor import of CMF 1.5.5-beta
author fguillaume
date Mon, 07 Nov 2005 22:03:41 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Various date criteria
15 $Id: DateCriteria.py 37998 2005-08-18 22:15:42Z jens $
16 """
18 from AccessControl import ClassSecurityInfo
19 from DateTime.DateTime import DateTime
20 from Globals import InitializeClass
22 from permissions import View
23 from permissions import ChangeTopics
24 from AbstractCriterion import AbstractCriterion
25 from interfaces import Criterion
26 from Topic import Topic
29 class FriendlyDateCriterion( AbstractCriterion ):
30 """
31 Put a friendly interface on date range searches, like
32 'where effective date is less than 5 days old'.
33 """
34 __implements__ = ( Criterion, )
36 meta_type = 'Friendly Date Criterion'
38 security = ClassSecurityInfo()
40 _editableAttributes = ( 'value', 'operation', 'daterange' )
42 _defaultDateOptions = ( ( 0, 'Now' )
43 , ( 1, '1 Day' )
44 , ( 2, '2 Days' )
45 , ( 5, '5 Days' )
46 , ( 7, '1 Week' )
47 , ( 14, '2 Weeks' )
48 , ( 31, '1 Month' )
49 , ( 31*3, '3 Months' )
50 , ( 31*6, '6 Months' )
51 , ( 365, '1 Year' )
52 , ( 365*2, '2 years' )
53 )
55 def __init__( self, id, field ):
57 self.id = id
58 self.field = field
59 self.value = None
60 self.operation = 'min'
61 self.daterange = 'old'
63 security.declarePublic( 'defaultDateOptions' )
64 def defaultDateOptions( self ):
65 """
66 Return a list of default values and labels for date options.
67 """
68 return self._defaultDateOptions
70 security.declareProtected( ChangeTopics, 'getEditForm' )
71 def getEditForm( self ):
72 """
73 Return the name of the skin method used by Topic to edit
74 criteria of this type.
75 """
76 return 'friendlydatec_editform'
78 security.declareProtected( ChangeTopics, 'edit' )
79 def edit( self
80 , value=None
81 , operation='min'
82 , daterange='old'
83 ):
84 """
85 Update the values to match against.
86 """
87 if value in ( None, '' ):
88 self.value = None
89 else:
90 try:
91 self.value = int( value )
92 except:
93 raise ValueError, 'Supplied value should be an int'
95 if operation in ( 'min', 'max', 'within_day' ):
96 self.operation = operation
97 else:
98 raise ValueError, 'Operation type not in set {min,max,within_day}'
100 if daterange in ( 'old', 'ahead' ):
101 self.daterange = daterange
102 else:
103 raise ValueError, 'Date range not in set {old,ahead}'
105 security.declareProtected(View, 'getCriteriaItems')
106 def getCriteriaItems( self ):
107 """
108 Return a sequence of items to be used to build the catalog query.
109 """
110 if self.value is not None:
111 field = self.Field()
112 value = self.value
113 operation = self.operation
115 # Negate the value for 'old' days
116 if self.daterange == 'old' and value != 0:
117 value = -value
119 # Also reverse the operator to match what a user would expect.
120 # Queries such as "More than 2 days ago" should match dates
121 # *earlier* than "today minus 2", and "Less than 2 days ago"
122 # would be expected to return dates *later* then "today minus
123 # two".
124 if operation == 'max':
125 operation = 'min'
126 elif operation == 'min':
127 operation = 'max'
129 date = DateTime() + value
131 if operation == 'within_day':
132 # When items within a day are requested, the range is between
133 # the earliest and latest time of that particular day
134 range = ( date.earliestTime(), date.latestTime() )
135 return ( ( field, {'query': range, 'range': 'min:max'} ), )
137 elif operation == 'min':
138 if value != 0:
139 if self.daterange == 'old':
140 date_range = (date, DateTime())
141 return ( ( field, { 'query': date_range
142 , 'range': 'min:max'
143 } ), )
144 else:
145 return ( ( field, { 'query': date.earliestTime()
146 , 'range': operation
147 } ), )
148 else:
149 # Value 0 means "Now", so get everything from now on
150 return ( ( field, {'query': date,'range': operation } ), )
152 elif operation == 'max':
153 if value != 0:
154 if self.daterange == 'old':
155 return ((field, {'query': date, 'range': operation}),)
156 else:
157 date_range = (DateTime(), date.latestTime())
158 return ( ( field, { 'query': date_range
159 , 'range': 'min:max'
160 } ), )
161 else:
162 # Value is 0, meaning "Now", get everything before "Now"
163 return ( ( field, {'query': date, 'range': operation} ), )
164 else:
165 return ()
167 InitializeClass(FriendlyDateCriterion)
170 # Register as a criteria type with the Topic class
171 Topic._criteriaTypes.append( FriendlyDateCriterion )