Daniel Nouri's Blog

Fri, 16 Jun 2006

pluggablecatalog

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 rss | permanent link | add to del.icio.us or digg it


< June 2006 >
SuMoTuWeThFrSa
     1 2 3
4 5 6 7 8 910
11121314151617
18192021222324
252627282930 

Feed of all categories: rss rss | atom

Categories: