[Zope] - How to circumvent url to object mapping ?
(correction)
Amos Latteier
amos@aracnet.com
Mon, 18 Jan 1999 10:06:38 -0800
At 11:15 AM 1/18/99 -0500, Jim Fulton wrote:
>Sure. The most direct way is to use the __bobo_traverse__ protocol.
>
>If an object implements:
>
> def __bobo_traverse__(self, REQUEST, name):
> "Return an object for the given name"
> ....
>
>Then this method will be called when traversing URLs.
Note: you don't need to define __bobo_traverse__ in order to have
ZPublisher traverse your object. Normally, when ZPublisher is looking for a
sub-object it will try getattr and getitem. Providing __bobo_traverse__ is
a hook which allows you to override ZPublisher's normal traversal mechanism
for a given object.
>Something like the following should work:
>
> class Articles(...):
> ...
>
> def __bobo_traverse__(self, REQUEST, name):
> return ArticleFinder(name)
>
> class ArticleFinder:
> def __init__(self, name):
> self._args=(name,)
>
> def __bobo_traverse__(self, REQUEST, name):
> self._args=self._args+(name,)
> if len(self._args)==4:
> # We have enough to find an article:
> return self._getArticle()
> return self
>
> def _getArticle(self):
> """Get an article from an external database,
> given self._args"""
> ...
Jim has outlined a very cool trick here. Normally what you'd do with
__bobo_traverse__ is to use the REQUEST object and the name argument to
find some sub-object and return that. However, the ArticleFinder object
collects the name argument and returns itself. It continues this loop until
it's gotten enough arguments and then it returns an article.
>Note that if you had a Z SQL Method, named 'articles'
>that took arguments 'year', 'month', 'day', and
>'subject', you could call it with a URL like:
>
>
http://www.webstar.com.gh/articles/year/1998/month/01/day/15/subject/Ashanti
_Records_Record_Revenues
>
>and it would extract an object from the database, assuming
>that the object was uniquely identified by the arguments.
>
>You could also add additional steps in the URL to traverse
>article methods, subobject, etc.
This is another very cool thing: SQL Methods can be treated like containers.
So in general, you won't need to do tricks like the __bobo_traverse__
thing. In general you can reference database objects via URLs. (The
__bobo_traverse__ trick was only necessary to parse a weird pre-existing
URL schema.)
-Amos