[Zope] ZCatalog: linking to directory not file in directory?

Casey Duncan cduncan@kaivo.com
Thu, 12 Apr 2001 08:36:21 -0600


Andy Gimblett wrote:
> 
> Hi there,
> 
> One of my colleagues has just come to me with a ZCatalog problem
> which I don't know how to solve. Fundamentally, we want to be
> able to catalogue the site so that for files with a certain name
> ("content_html"), the ZCatalog entries should link to the
> directory containing the file, not the file itself.
> 
> Here's the situation:
> 
> * The site has just one "index_html" at the site root, which
>   contains a <dtml-var content_html> tag.
> * Every directory in the site contains a "content_html" DTML
>   Method which contains that directory's desired contents.
> * So, a call to http://site/foo/ involves acquiring index_html
>   from the site root directory, and then inserting foo's
>   content_html into that.
> 
> The problem is that when cataloging the site, we get lots of
> links like /foo/content_html, /bar/content_html, etc., when
> what we really want are /foo/, /bar/, etc., or maybe even
> /foo/index_html, /bar/index_html - if you see what I mean. :-)
> 
> Unfortunately neither my colleague nor myself knows how to do this,
> or even if it's possible.
> 
> Thanks in advance,
> 
> Andy

The way to solve this is by creating a script at the root of your Zope
installation that returns the path of the object being indexed. For
objects that are named "content_html" it will return the path of the
parent object (the folder) instead of the direct path. You would then
add a metadata element to your catalog with the name of this script.
Here is a tested Python script that returns an adjusted path for any
given object:

import string
if context.getId() == 'content_html':
	return string.join(context.aq_parent.getPhysicalPath(),'/') + '/'
else:
	return string.join(context.getPhysicalPath(),'/')

Let's say you named this script "my_path". Add "my_path" as a metadata
element to your Catalog, update it and voila, the adjusted path is saved
for each object. You could then use the "my_path" value to create the
link to the object when the Catalog is queried.

hth
-- 
| Casey Duncan
| Kaivo, Inc.
| cduncan@kaivo.com
`------------------>