Commit d90bf5bb authored by 's avatar

- some minor plugin indexes cleanup

parent 09be733c
......@@ -8,6 +8,8 @@ Zope Changes
Restructuring
- Indexes: Removed unused parameters from '_apply_index' methods.
- Fixed Collector #2190: Calls to
zope.security.management.checkPermission aren't rerouted to
Zope 2's security policy.
......@@ -78,6 +80,8 @@ Zope Changes
Bugs Fixed
- PluginIndexes: Fixed 'parseIndexRequest' for false values.
- Collector #2269: fixed broken ZPT FTP support
- Collector #2261: Acquisition when creating objects via Webdav.
......
......@@ -19,7 +19,6 @@ import time
from logging import getLogger
from datetime import date, datetime
from datetime import tzinfo, timedelta
from types import StringType, FloatType, IntType
import BTrees.Length
from BTrees.IIBTree import IISet, union, intersection, multiunion
......@@ -90,7 +89,7 @@ class DateIndex(UnIndex, PropertyManager):
implements(IDateIndex)
meta_type = 'DateIndex'
query_options = ['query', 'range']
query_options = ('query', 'range')
index_naive_time_as_local = True # False means index as UTC
_properties=({'id':'index_naive_time_as_local',
......@@ -157,14 +156,14 @@ class DateIndex(UnIndex, PropertyManager):
return returnStatus
def _apply_index( self, request, cid='', type=type ):
def _apply_index(self, request):
"""Apply the index to query parameters given in the argument
Normalize the 'query' arguments into integer values at minute
precision before querying.
"""
record = parseIndexRequest( request, self.id, self.query_options )
if record.keys == None:
record = parseIndexRequest(request, self.id, self.query_options)
if record.keys is None:
return None
keys = map( self._convert, record.keys )
......@@ -215,22 +214,17 @@ class DateIndex(UnIndex, PropertyManager):
else:
setlist = index.values(lo)
#for k, set in setlist:
#if type(set) is IntType:
#set = IISet((set,))
#r = set_func(r, set)
# XXX: Use multiunion!
r = multiunion(setlist)
else: # not a range search
for key in keys:
set = index.get(key, None)
if set is not None:
if type(set) is IntType:
if isinstance(set, int):
set = IISet((set,))
r = set_func(r, set)
if type(r) is IntType:
if isinstance(r, int):
r = IISet((r,))
if r is None:
......@@ -242,20 +236,20 @@ class DateIndex(UnIndex, PropertyManager):
"""Convert Date/Time value to our internal representation"""
# XXX: Code patched 20/May/2003 by Kiran Jonnalagadda to
# convert dates to UTC first.
if isinstance( value, DateTime ):
if isinstance(value, DateTime):
t_tup = value.toZone('UTC').parts()
elif type( value ) in (FloatType, IntType):
elif isinstance(value, (float, int)):
t_tup = time.gmtime( value )
elif type( value ) is StringType and value:
elif isinstance(value, str) and value:
t_obj = DateTime( value ).toZone('UTC')
t_tup = t_obj.parts()
elif type( value ) is date:
t_tup = value.timetuple()
elif type( value ) is datetime:
elif isinstance(value, datetime):
if self.index_naive_time_as_local and value.tzinfo is None:
value = value.replace(tzinfo=Local)
# else if tzinfo is None, naive time interpreted as UTC
t_tup = value.utctimetuple()
elif isinstance(value, date):
t_tup = value.timetuple()
else:
return default
......
......@@ -63,6 +63,7 @@ class DateRangeIndex(UnIndex):
security = ClassSecurityInfo()
meta_type = "DateRangeIndex"
query_options = ('query',)
manage_options= ( { 'label' : 'Properties'
, 'action' : 'manage_indexProperties'
......@@ -70,8 +71,6 @@ class DateRangeIndex(UnIndex):
,
)
query_options = ['query']
since_field = until_field = None
def __init__(self, id, since_field=None, until_field=None,
......@@ -213,7 +212,6 @@ class DateRangeIndex(UnIndex):
t2 = self._until_only
result = []
IntType = type( 0 )
if not withLengths:
......@@ -224,7 +222,7 @@ class DateRangeIndex(UnIndex):
for key in t1.keys():
set = t1[ key ]
if type( set ) is IntType:
if isinstance(set, int):
length = 1
else:
length = len( set )
......@@ -232,7 +230,7 @@ class DateRangeIndex(UnIndex):
for key in t2.keys():
set = t2[ key ]
if type( set ) is IntType:
if isinstance(set, int):
length = 1
else:
length = len( set )
......@@ -240,12 +238,12 @@ class DateRangeIndex(UnIndex):
return tuple( result )
def _apply_index( self, request, cid='' ):
def _apply_index(self, request):
"""
Apply the index to query parameters given in 'request', which
should be a mapping object.
If the request does not contain the needed parametrs, then
If the request does not contain the needed parameters, then
return None.
If the request contains a parameter with the name of the
......@@ -257,7 +255,7 @@ class DateRangeIndex(UnIndex):
second object is a tuple containing the names of all data fields
used.
"""
record = parseIndexRequest( request, self.getId() )
record = parseIndexRequest(request, self.id, self.query_options)
if record.keys is None:
return None
......@@ -396,10 +394,10 @@ class DateRangeIndex(UnIndex):
def _convertDateTime( self, value ):
if value is None:
return value
if type( value ) == type( '' ):
if isinstance(value, str):
dt_obj = DateTime( value )
value = dt_obj.millis() / 1000 / 60 # flatten to minutes
if isinstance( value, DateTime ):
if isinstance(value, DateTime):
value = value.millis() / 1000 / 60 # flatten to minutes
result = int( value )
if isinstance(result, long): # this won't work (Python 2.3)
......
......@@ -15,7 +15,6 @@
$Id$
"""
from types import StringType, ListType, TupleType
from logging import getLogger
from Globals import Persistent, DTMLFile
......@@ -56,6 +55,7 @@ class PathIndex(Persistent, SimpleItem):
implements(IPathIndex, IUniqueValueIndex)
meta_type="PathIndex"
query_options = ('query', 'level', 'operator')
manage_options= (
{'label': 'Settings',
......@@ -63,8 +63,6 @@ class PathIndex(Persistent, SimpleItem):
'help': ('PathIndex','PathIndex_Settings.stx')},
)
query_options = ("query", "level", "operator")
def __init__(self,id,caller=None):
self.id = id
self.operators = ('or','and')
......@@ -108,7 +106,7 @@ class PathIndex(Persistent, SimpleItem):
else:
path = f
if not isinstance(path, (StringType, TupleType)):
if not isinstance(path, (str, tuple)):
raise TypeError('path value must be string or tuple of strings')
else:
try:
......@@ -116,7 +114,7 @@ class PathIndex(Persistent, SimpleItem):
except AttributeError:
return 0
if isinstance(path, (ListType, TupleType)):
if isinstance(path, (list, tuple)):
path = '/'+ '/'.join(path[1:])
comps = filter(None, path.split('/'))
......@@ -165,8 +163,7 @@ class PathIndex(Persistent, SimpleItem):
level >= 0 starts searching at the given level
level < 0 not implemented yet
"""
if isinstance(path, StringType):
if isinstance(path, str):
level = default_level
else:
level = int(path[1])
......@@ -207,17 +204,15 @@ class PathIndex(Persistent, SimpleItem):
def __len__(self):
return self._length()
def _apply_index(self, request, cid=''):
def _apply_index(self, request):
""" hook for (Z)Catalog
'request' -- mapping type (usually {"path": "..." }
additionaly a parameter "path_level" might be passed
to specify the level (see search())
'cid' -- ???
"""
record = parseIndexRequest(request,self.id,self.query_options)
if record.keys==None: return None
record = parseIndexRequest(request, self.id, self.query_options)
if record.keys is None:
return None
level = record.get("level",0)
operator = record.get('operator',self.useOperator).lower()
......
......@@ -82,6 +82,7 @@ class TextIndex(Persistent, Implicit, SimpleItem):
implements(ITextIndex, IPluggableIndex)
meta_type='TextIndex'
query_options = ('query', 'operator')
manage_options= (
{'label': 'Settings',
......@@ -89,8 +90,6 @@ class TextIndex(Persistent, Implicit, SimpleItem):
'help': ('TextIndex','TextIndex_Settings.stx')},
)
query_options = ["query","operator"]
def __init__(self, id, ignore_ex=None, call_methods=None, lexicon=None,
caller=None, extra=None):
"""Create an index
......@@ -440,7 +439,7 @@ class TextIndex(Persistent, Implicit, SimpleItem):
return r
def _apply_index(self, request, cid=''):
def _apply_index(self, request):
""" Apply the index to query parameters given in the argument,
request
......@@ -454,9 +453,9 @@ class TextIndex(Persistent, Implicit, SimpleItem):
records. The second object is a tuple containing the names of
all data fields used.
"""
record = parseIndexRequest(request,self.id,self.query_options)
if record.keys==None: return None
record = parseIndexRequest(request, self.id, self.query_options)
if record.keys is None:
return None
# Changed for 2.4
# We use the default operator that can me managed via the ZMI
......
......@@ -46,7 +46,7 @@ class TopicIndex(Persistent, SimpleItem):
implements(ITopicIndex, IPluggableIndex)
meta_type="TopicIndex"
query_options = ('query','operator')
query_options = ('query', 'operator')
manage_options= (
{'label': 'FilteredSets',
......@@ -91,14 +91,13 @@ class TopicIndex(Persistent, SimpleItem):
if self.filteredSets.has_key(filter_id):
return self.filteredSets[filter_id].getIds()
def _apply_index(self, request, cid=''):
def _apply_index(self, request):
""" hook for (Z)Catalog
'request' -- mapping type (usually {"topic": "..." }
'cid' -- ???
"""
record = parseIndexRequest(request,self.id,self.query_options)
if record.keys is None: return None
record = parseIndexRequest(request, self.id, self.query_options)
if record.keys is None:
return None
operator = record.get('operator', self.defaultOperator).lower()
if operator == 'or': set_func = union
......
......@@ -304,7 +304,7 @@ class UnIndex(SimpleItem):
LOG.debug('Attempt to unindex nonexistent document'
' with id %s' % documentId,exc_info=True)
def _apply_index(self, request, cid='', type=type):
def _apply_index(self, request):
"""Apply the index to query parameters given in the request arg.
The request argument should be a mapping object.
......@@ -344,7 +344,8 @@ class UnIndex(SimpleItem):
up in a tuple ala: request = {'id':('',)}
"""
record = parseIndexRequest(request, self.id, self.query_options)
if record.keys==None: return None
if record.keys is None:
return None
index = self._index
r = None
......
##############################################################################
#
# Copyright (c) 2007 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Unit tests for util module.
$Id$
"""
import unittest
from ZPublisher.HTTPRequest import record as Record
class parseIndexRequestTests(unittest.TestCase):
def _getTargetClass(self):
from Products.PluginIndexes.common.util import parseIndexRequest
return parseIndexRequest
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_get_record(self):
record = Record()
record.query = 'foo'
record.level = 0
record.operator = 'and'
request = {'path': record}
parser = self._makeOne(request, 'path', ('query','level','operator'))
self.assertEqual(parser.get('keys'), ['foo'])
self.assertEqual(parser.get('level'), 0)
self.assertEqual(parser.get('operator'), 'and')
def test_get_dict(self):
request = {'path': {'query': 'foo', 'level': 0, 'operator': 'and'}}
parser = self._makeOne(request, 'path', ('query','level','operator'))
self.assertEqual(parser.get('keys'), ['foo'])
self.assertEqual(parser.get('level'), 0)
self.assertEqual(parser.get('operator'), 'and')
def test_get_string(self):
request = {'path': 'foo', 'path_level': 0, 'path_operator': 'and'}
parser = self._makeOne(request, 'path', ('query','level','operator'))
self.assertEqual(parser.get('keys'), ['foo'])
self.assertEqual(parser.get('level'), 0)
self.assertEqual(parser.get('operator'), 'and')
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(parseIndexRequestTests))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
......@@ -7,20 +7,21 @@
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
# FOR A PARTICULAR PURPOSE.
#
#############################################################################
__version__ = '$Id$'
##############################################################################
"""PluginIndexes utils.
$Id$
"""
from warnings import warn
from types import StringType,ListType,TupleType,DictType,InstanceType
from types import InstanceType
from DateTime import DateTime
SequenceTypes = (TupleType, ListType)
class parseIndexRequest:
"""
This class provides functionality to hide the internals of a request
send from the Catalog/ZCatalog to an index._apply_index() method.
......@@ -31,7 +32,6 @@ class parseIndexRequest:
the request directory where the index name is the name of the key.
Additional parameters for an index could be passed as index+"_usage" ...
- dictionary-style parameters specify a query for an index as
an entry in the request dictionary where the key corresponds to the
name of the index and the key is a dictionary with the parameters
......@@ -43,16 +43,13 @@ class parseIndexRequest:
other parameters depend on the the index
- record-style parameters specify a query for an index as instance of the
Record class. This happens usually when parameters from a web form use
the "record" type e.g. <input type="text" name="path.query:record:string">.
All restrictions of the dictionary-style parameters apply to the record-style
parameters
"""
ParserException = 'IndexRequestParseError'
def __init__(self, request, iid, options=[]):
......@@ -73,13 +70,16 @@ class parseIndexRequest:
usage_param = iid + '_usage'
if request.has_key(usage_param):
self.usage = request[usage_param]
warn("\nZCatalog query using '%s' detected.\nUsing query parameters ending with '_usage' is deprecated.\nConsider using record-style parameters instead (see lib/python/Products/PluginIndexes/README.txt for details)" % usage_param, DeprecationWarning)
warn("ZCatalog query using '%s' detected.\n"
"Using query parameters ending with '_usage' is deprecated.\n"
"Consider using record-style parameters instead "
"(see lib/python/Products/PluginIndexes/README.txt for "
"details)" % usage_param, DeprecationWarning)
param = request[iid]
keys = None
t = type(param)
if t is InstanceType and not isinstance(param, DateTime):
if isinstance(param, InstanceType) and not isinstance(param, DateTime):
""" query is of type record """
record = param
......@@ -90,7 +90,7 @@ class parseIndexRequest:
"'query' attribute" % self.id)
keys = record.query
if type(keys) is StringType:
if isinstance(keys, str):
keys = [keys.strip()]
for op in options:
......@@ -99,11 +99,11 @@ class parseIndexRequest:
if hasattr(record, op):
setattr(self, op, getattr(record, op))
elif t is DictType:
elif isinstance(param, dict):
""" query is a dictionary containing all parameters """
query = param.get("query", ())
if type(query) in SequenceTypes:
if isinstance(query, (tuple, list)):
keys = query
else:
keys = [ query ]
......@@ -117,7 +117,7 @@ class parseIndexRequest:
else:
""" query is tuple, list, string, number, or something else """
if t in SequenceTypes:
if isinstance(param, (tuple, list)):
keys = param
else:
keys = [param]
......@@ -129,21 +129,9 @@ class parseIndexRequest:
self.keys = keys
def get(self,k,default_v=None):
if hasattr(self,k):
v = getattr(self,k)
if v: return v
else: return default_v
else:
return default_v
def test():
r = parseIndexRequest({'path':{'query':"","level":2,"operator":'and'}},'path',['query',"level","operator"])
for k in dir(r):
print k,getattr(r,k)
if __name__=="__main__":
test()
def get(self, k, default_v=None):
if hasattr(self, k):
v = getattr(self, k)
if v != '':
return v
return default_v
......@@ -43,12 +43,12 @@ class IPluggableIndex(Interface):
def unindex_object(documentId):
"""Remove the documentId from the index."""
def _apply_index(request, cid=''):
def _apply_index(request):
"""Apply the index to query parameters given in 'request'.
The argument should be a mapping object.
If the request does not contain the needed parametrs, then
If the request does not contain the needed parameters, then
None is returned.
If the request contains a parameter with the name of the column
......
......@@ -61,13 +61,12 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem):
## Magic class attributes ##
meta_type = 'ZCTextIndex'
query_options = ('query',)
manage_options = (
{'label': 'Overview', 'action': 'manage_main'},
)
query_options = ['query']
security = ClassSecurityInfo()
security.declareObjectProtected(manage_zcatalog_indexes)
......@@ -204,7 +203,7 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem):
if self.index.has_doc(docid):
self.index.unindex_doc(docid)
def _apply_index(self, request, cid=''):
def _apply_index(self, request):
"""Apply query specified by request, a mapping containing the query.
Returns two object on success, the resultSet containing the
......@@ -216,6 +215,7 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem):
record = parseIndexRequest(request, self.id, self.query_options)
if record.keys is None:
return None
query_str = ' '.join(record.keys)
if not query_str:
return None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment