quick search:
 

getCatInfo: Get Catalog info for current Plone object

Submitted by: lupa
Last Edited: 2005-11-17

Category: Python(Script)

Average rating is: 4.67 out of 5 (3 ratings)

Description:
Problem: you need to know the Index values and Metadata from the portal_catalog for ONE specific object,
the one you're looking at right now. You *could* look in the Catalog tab of the portal_catalog, but
that can be frustratingly slow if you have many objects cataloged.

Solution: add this script to your portal_skins/custom folder and then type the name of this script
after the URL of your Plone content, and voila you get the standard view of index and metadata values.


Source (Text):
#INSTRUCTIONS: give this script an id of "getCatInfo"
# then type "/getCatInfo" at the end of any URL for an object in your portal
# to get a printout of the Catalog index values and metadata for that object

from string import join
obid = context.id
path = join(context.getPhysicalPath(), '/')
results = container.portal_catalog(path=path,getId=obid,sort_on='created',sort_order='reverse')
rid = ''
for r in results:
    rid = r.getRID()
if rid:
    print container.portal_catalog.manage_objectInformation(rid=rid)
else:
    print "Not in the catalog, apparently."
return printed

Explanation:
This could probably be done in about two lines if I was smarter pythonically. What we do is search the portal_catalog to find the
current object (the one showing in the Plone site page). To find it, we use the "path" index and the "getId" index combined.

That combination of path and getId will work for nearly all objects, unless you have a folder named "fred" and inside it a document
named "fred". To get that I added the "created" sorting. That way the earliest content (the folder named "fred") should show up,
rather than the document inside "fred". So now it works for all portal content, and returns a friendly string if nothing was
returned from the catalog (try it with a DTML document, for example).

We can't just ask the portal_catalog for this info using an object ID of course, because it's not unique. The catalog uses
record IDs (RIDs) internally, and most methods asking the catalog for specific info use RIDs to ask. So we look in the
search results to get the RID of the current object returned.

Finally, we use the manage_objectInformation method to bring up the info we want.

NEWBIES: If nothing else, use this code snippet to remind yourself that anything you can do through the ZMI, you can do with
a script. The URL that looks at catalog information contains this method... so when you want to script something that you
usually do in the ZMI, look at the code in the URL and in the pages and forms that ZMI uses to get your clues on how to script it.

LAST NOTE: It works properly if you put this script in your PORTAL root. If you want to put it into your Zope instance root, so that
all of your portals can use it, then change the code where it says "container" to read "context", so that it will find your local
portal_catalog. Leave "container" in there, and the script looks in the Zope root for portal_catalog, and it ain't gonna find one there.


Comments:

Beautimous by corwin - 2005-04-07
Great time saver. Why didn't I think of that?