[Zope] url path remainder as a script parameter?

Phillip Hutchings sitharus at gmail.com
Tue Feb 22 15:14:06 EST 2005


On Tue, 22 Feb 2005 01:34:59 -0800, Kent Watsen <kent at watsen.net> wrote:
> 
> Dieter, thank you for your time and patience!
> 
> I found a product called DirArg that seems to do the trick, but its own
> documentation says that it is deprecated by traverse_subpath...
> 
> I also found http://zope.org/Members/lalo/scriptclass and have now
> written a simple Python-based product, which I am now modifying to
> derive off the Script class, but the instructions are a little
> cryptic...   But I think that I'm still missing something fundamental -
> a normal product has a class that has an index_html method - is the goal
> for my Script-derived product class's index_html method to have the
> traverse_subpath variable defined?
> 
> Also, the normal product's index_html is initialized using something like:
>     index_html = DTMLFile("index_html", globals())
> Is there such a thing as:
>     index_html = PythonScriptFile("index_html", globals())
> ??? I googled, but couldn't find anything...
> 
> Again, my high-level goal is to have a Product that uses
> traverse_subpath to form a query against another application - surely
> this is not uncommon...

Actually, it appears to be. I had the same problem with my as-yet
incomplete blog that I wanted to have URLS much as you describe. I
figured it out though. traverse_subpath is only set for ZPT, DTML and
Script (Python) objects, as far as I can see, so I snarfed the code
from the python script class. You need this code for it:

This will allow Zope to traverse down as far as it likes and add it to
a variable in request called 'traverse_subpath'

    def __before_publishing_traverse__(self, self2, request):
        path = request['TraversalRequestNameStack']
        if path and hasattr(self.aq_base, path[-1]):
            return
        subpath = path[:]
        path[:] = []
        subpath.reverse()
        request.set('traverse_subpath', subpath)

and then in your index_html method you need to decode the subpath and
then do what you want with it, here's what I do at the moment:
    def index_html(self, REQUEST=None, *args, **kw):
        """Serve up blog requests"""
        traverse_subpath = REQUEST['traverse_subpath']
        if len(traverse_subpath) == 0:
            # We return the index page
            return self.blog_index_html(self, REQUEST, **kw)
        elif len(traverse_subpath) == 1:
            item = traverse_subpath[0]
            if re.match(r"[1-2][0-9]{3}", item) is not None:
                return "Year match %s" % item
            else:
                return "Other match %s" % item
        else:
            return "Deep nesting, item is %s" % '/'.join(traverse_subpath)

Hope this helps a bit. The individual path elements are stored as an
array, starting from the left most element after the last 'real'
object, ie the instance of this class.
-- 
Phillip Hutchings
http://www.sitharus.com/
sitharus at gmail.com / sitharus at sitharus.com


More information about the Zope mailing list