- How to circumvent url to object mapping ? (correction)
I want to have a URL called say, http://www.webstar.com.gh/articles/1998/01/15/Ashanti_Records_Record_Revenue... which should map to a python method/object that queries an external database with the arguments 1998+01+15+Ashanti_Records_Record_Revenues Is there a way to do this *without* using Apache rewrite rules ? ^^^^^^^^^^^^^ I meant to say without but said with ...
Guido Sohne wrote:
I want to have a URL called say,
http://www.webstar.com.gh/articles/1998/01/15/Ashanti_Records_Record_Revenue...
which should map to a python method/object that queries an external database with the arguments 1998+01+15+Ashanti_Records_Record_Revenues
Do you mean 4 arguments? Or do you mean one arguments with strings concatinated with '+'?
Is there a way to do this *without* using Apache rewrite rules ?
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. 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""" ... 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... 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. Jim -- Jim Fulton mailto:jim@digicool.com Technical Director (888) 344-4332 Python Powered! Digital Creations http://www.digicool.com http://www.python.org Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email address may not be added to any commercial mail list with out my permission. Violation of my privacy with advertising or SPAM will result in a suit for a MINIMUM of $500 damages/incident, $1500 for repeats.
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
At 01:42 PM 1/16/99 +0000, Guido Sohne wrote:
I want to have a URL called say,
http://www.webstar.com.gh/articles/1998/01/15/Ashanti_Records_Record_Revenue...
which should map to a python method/object that queries an external database with the arguments 1998+01+15+Ashanti_Records_Record_Revenues
Is there a way to do this *without* using Apache rewrite rules ?
^^^^^^^^^^^^^ I meant to say without but said with ...
If you don't mind making the URL: http://www.webstar.com.gh/articles/1998+01+15+Ashanti_Records_Record_Revenue... Then you can simply give your object a __getitem__ method that does the query. ZPublisher will pass "1998+01+15+Ashanti_Records_Record_Revenues" to the __getitem__ method.
participants (4)
-
Amos Latteier -
Guido Sohne -
Jim Fulton -
Phillip J. Eby