Note that this blog is discontinued. You can find my new blog here: Daniel Nouri's Blog.
I added a new project called pluggablecatalog to the collective.
pluggablecatalog is a replacement (or rather: a wrapper) for Plone's portal catalog. It adds the ability to plug in default search restrictions without the need to subclass or monkey- patch the catalog.
From the docstring of pluggablecatalog/tool.py:
Wraps CMFPlone's CatalogTool to add default parameters
collected from IQueryDefaults utilities.
>>> from Products.pluggablecatalog.tool import CatalogTool
>>> catalog = self.portal.portal_catalog
>>> isinstance(catalog, CatalogTool)
True
We create two documents and make sure they're indexed:
>>> len(catalog())
0
>>> self.folder.invokeFactory('Document', 'doc1')
'doc1'
>>> self.folder.invokeFactory('Document', 'doc2')
'doc2'
>>> doc1, doc2 = self.folder.doc1, self.folder.doc2
>>> doc1.setTitle('First Document')
>>> doc2.setTitle('Second Document')
>>> doc1.reindexObject(); doc2.reindexObject()
>>> len(catalog())
2
Let's now add a rather stupid IQueryDefaults utility that
restricts searches by default to objects with the Title 'First
Document':
>>> from zope import component
>>> from zope import interface
>>> from Products.pluggablecatalog.interfaces import IQueryDefaults
>>> def myDefaults(context, request):
... return {'Title': 'First Document'}
>>> interface.directlyProvides(myDefaults, IQueryDefaults)
>>> component.getService('Utilities').provideUtility(
... IQueryDefaults, myDefaults)
With this utility in place, we should only retrieve doc1 now,
unless we explicitly provide a 'Title' query parameter:
>>> len(catalog())
1
>>> catalog()[0].getObject().aq_base is doc1.aq_base
True
>>> len(catalog(Title='Second Document'))
1
>>> (catalog(Title='Second Document')[0].getObject().aq_base is
... doc2.aq_base)
True
posted at: 13:07 |
0 comments |
category: /devel/zope
|
permanent link |
add to del.icio.us or digg it
| < | June 2006 | > | ||||
| Su | Mo | Tu | We | Th | Fr | Sa |
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | |
Feed of all categories:
rss |
atom