[Zope3-Users] querying the catalog
Duncan McGreggor
duncan.mcgreggor at gmail.com
Fri Sep 30 18:33:43 EDT 2005
I have a multiple sites on a zope3 instance (folders at the top level
that have their own site managers). In one of them, there is page
content object with a custom view for portraying search results from
its (local) catalog. However, I have had a great deal of difficulty
getting the catalog I want. Here is the pertinent code for three
attempts, the last of which "worked".
Attempt #1
class HomePageView(BrowserView):
def inventoryInStock(self):
catalog = zapi.getUtility(ICatalog,
name='accumac_catalog',
context=self.context)
return catalog.searchResults(equipment_in_stock=[True])
Attempt #2
class HomePageView(BrowserView):
def inventoryInStock(self):
sm = zapi.traverse(self.context, '++etc++site')
catalog = sm['default']['accumac_catalog']
return catalog.searchResults(equipment_in_stock=[True])
Attempt #3
class HomePageView(BrowserView):
def inventoryInStock(self):
sm = zapi.traverse(self.context, '/accumac/++etc++site')
catalog = sm['default']['accumac_catalog']
return catalog.searchResults(equipment_in_stock=[True])
The first attempt got the catalog at the root of the zope 3 instance if
I passed no name; if I passed the name of the local catalog, I got the
following error:
ComponentLookupError: (<InterfaceClass
zope.app.catalog.interfaces.ICatalog>, 'accumac_catalog')
A clue as to why this happened may be in attempt #2. The error for
attempt #2 was:
TraversalError: (<accumacsite.homepage.HomePage object at 0x2e688f0>,
'site')
And this might be a clue; it made me think that if the HomePage content
object was traversable, in both attempt #1 and #2, the correct catalog
would have been found.
In attempt #3, I had zope traverse to the absolute location of the
local site manager, and from there got the correct catalog, with the
results being returned properly. However, this last attempt seems
rather hackish, as it uses a path and hard-coded names. I'd really like
to do this the right way.
Does anyone have advice on this?
Thanks!
Duncan
More information about the Zope3-users
mailing list