Back to index

plone3  3.1.7
__init__.py
Go to the documentation of this file.
00001 # Copyright (C) 2003 by Dr. Dieter Maurer, Eichendorffstr. 23, D-66386 St. Ingbert, Germany
00002 # see "LICENSE.txt" for details
00003 #       $Id: __init__.py,v 1.5 2006/06/25 19:11:24 dieter Exp $
00004 '''Advanced Query
00005 
00006 see 'AdvancedQuery.html' for documentation.
00007 '''
00008 
00009 from AccessControl import allow_module as _allow_module, ClassSecurityInfo
00010 from Globals import InitializeClass
00011 
00012 from AdvancedQuery import Eq, In, Le, Ge, \
00013      MatchGlob, MatchRegexp, \
00014      Between, Not, And, Or, Generic, Indexed, \
00015      _CompositeQuery, LiteralResultSet
00016 
00017 from eval import eval as _eval
00018 
00019 from ranking import RankByQueries_Sum, RankByQueries_Max
00020 
00021 
00022 ############################################################################
00023 ## Security
00024 _allow_module('Products.AdvancedQuery')
00025 _s = ClassSecurityInfo(); _s.declarePublic('addSubquery')
00026 _CompositeQuery._s = _s; InitializeClass(_CompositeQuery)
00027 
00028 
00029 ############################################################################
00030 ## ZCatalog extension
00031 def _makeAdvancedQuery(self,catalogSearchSpec):
00032   '''advanced query corresponding to *catalogSearchSpec* (a dictionary).'''
00033   q = And(); get = catalogSearchSpec.get
00034   for i in self.Indexes.objectIds():
00035     vi = get(i)
00036     if vi is None or vi == '': continue
00037     if not (isinstance(vi, dict) and vi.get('query') is not None
00038             or getattr(vi, 'query', None) is not None):
00039       usage = get(i+'_usage')
00040       if usage is not None:
00041         if not usage.startswith('range:'):
00042           raise ValueError, 'unsupported usage spec: %s' % usage
00043         vi = {'query' : vi, 'range' : usage[6:]}
00044     q.addSubquery(Generic(i,vi))
00045   return q
00046 
00047 from Products.ZCatalog.ZCatalog import ZCatalog
00048 ZCatalog.evalAdvancedQuery = _eval
00049 ZCatalog.makeAdvancedQuery = _makeAdvancedQuery
00050 del ZCatalog
00051 
00052 
00053 ############################################################################
00054 ## CatalogTool extension
00055 try: from Products.CMFCore.CatalogTool import CatalogTool
00056 except ImportError: CatalogTool= None
00057 if CatalogTool:
00058 
00059   from Products.CMFCore.CatalogTool import _getAuthenticatedUser, _checkPermission, AccessInactivePortalContent
00060 
00061   def _evalAdvancedQuery(self,query,sortSpecs=()):
00062     '''evaluate *query* for 'CatalogTool' and sort results according to *sortSpec*.'''
00063     query = query._clone()
00064 
00065     # taken from 'CatalogTool.searchResults'
00066     user = _getAuthenticatedUser(self)
00067     query &= In('allowedRolesAndUsers',self._listAllowedRolesAndUsers(user))
00068     if not _checkPermission(AccessInactivePortalContent,self):
00069       now= self.ZopeTime()
00070       if 'ValidityRange' in self.Indexes.objectIds():
00071         query &= Eq('ValidityRange', now)
00072       else:
00073         query &= Le('effective',now)
00074         query &= Ge('expires',now)
00075     return _eval(self,query,sortSpecs)
00076 
00077   CatalogTool.evalAdvancedQuery= _evalAdvancedQuery
00078 del CatalogTool
00079 
00080 
00081