[Zope3-Users] filter possibilities
Rupert Redington
rupert at neontribe.co.uk
Tue Sep 26 07:46:16 EDT 2006
I'm no expert on this... In fact I often find myself struggling to get
cataloging in Zope3 to do what I want in a painless way (which is
probably why hurry exists...). Experts please correct me if I'm doing
anything objectionable...
I'm assuming that you've got an IntIds utility and a catalog,
and I'm assuming that in your catalog is a FieldIndex which uses
IZopeDublinCore...
If not the following _may_ provide a little help:
from zope.app.catalog.interfaces import ICatalog
from zope.app.catalog.catalog import Catalog
from zope.app.catalog.field import FieldIndex
from zope.app.dublincore.interfaces import IZopeDublinCore
from zope.app.component.hooks import getSite
from zope.app.component.site import UtilityRegistration
from zope.app.component.interfaces.registration import ActiveStatus
from zope.component import provideUtility
# Find your site's manager and get the default tool folder
sm = getSite().getSiteManager()
smf = sm['default']
# make a catalog and put it in the folder
catalog = Catalog()
smf['My Catalog'] = catalog
# Register the new catalog so that its available for use
reg = UtilityRegistration('', ICatalog, catalog)
smf.registrationManager.addRegistration(reg)
reg.status = ActiveStatus
# Use provide utility so that the catalog is ready for use
provideUtility(catalog, ICatalog)
# Add a Field index which will catalog the creation time of objects
index = FieldIndex('created', IZopeDublinCore)
catalog['Date Created'] = index
Once your catalog has indexes and they're indexing objects then you can
start trying queries. Here's an attempt at a query to find all indexed
objects created in the last 10 days:
from zope.app.catalog.interfaces import ICatalog
from hurry.query.interfaces import IQuery
from hurry.query import Ge
import datetime
#Grab our catalog and query utilities
catalog = getUtility(ICatalog)
query = getUtility(IQuery)
# make a tuple to tell hurry which catalog and index we want to use
index = (catalog, 'Date Created')
# make a datetime object (in my timezone) to find out when
# 10 days ago was...
now = datetime.datetime.now(pytz.timezone('GB'))
then = now - datetime.timedelta(10)
results = query.searchResults(Ge(index, then))
Good Luck,
Rupert
More information about the Zope3-users
mailing list