How to get REST friendly urls from sql database
Hi. Normally a url to an article in our CMS system would look roughly like this: http://www.dom.tld/aritcles/25245 Where 25245 would be an object of some metatype, say 'article'. Now what if I wanted to get the data for that page from an sql server instead, I would expect to end up with a url like this: http://www.dom.tld/aritcles/index_html?id=25245 Now, having recently read the Roy Fielding paper, and expecting to do some caching, I would much prefer to have it the first way, even when I have to get the data out of a DB. Anybody have any ideas on how this might be done? Regards Gaute Amundsen
--On 20. Februar 2007 16:30:43 +0100 Gaute Amundsen <gaute@div.org> wrote:
Hi.
Normally a url to an article in our CMS system would look roughly like this: http://www.dom.tld/aritcles/25245
Where 25245 would be an object of some metatype, say 'article'.
Now what if I wanted to get the data for that page from an sql server instead, I would expect to end up with a url like this:
http://www.dom.tld/aritcles/index_html?id=25245
Now, having recently read the Roy Fielding paper, and expecting to do some caching, I would much prefer to have it the first way, even when I have to get the data out of a DB.
Anybody have any ideas on how this might be done?
How about Apache rewrite rules or a script called index_html that fetches the object by its ID? That's pretty much boring straight-forward stuff :-) -aj
On Tue, Feb 20, 2007 at 04:37:41PM +0100, Andreas Jung wrote:
--On 20. Februar 2007 16:30:43 +0100 Gaute Amundsen <gaute@div.org> wrote:
Hi.
Normally a url to an article in our CMS system would look roughly like this: http://www.dom.tld/aritcles/25245 (snip) Anybody have any ideas on how this might be done?
How about Apache rewrite rules or a script called index_html that fetches the object by its ID? That's pretty much boring straight-forward stuff :-)
Another option is that the object at /articles could be an instance of a class that looks something like: class MyArticleContainer(): def __before_publishing_traverse__(self, unused, request): # Save the rest of the path as 'traverse_subpath'. request.set('traverse_subpath', reversed(request['TraversalRequestNameStack'])) # Tell the publisher to stop traversing now. request['TraversalRequestNameStack'][:] = [] def __call__(self, *args, **kw): subpath = self.REQUEST['traverse_subpath'] data = get_data_however_you_like(subpath) return data For background, read the stuff about traversal hooks at http://wiki.zope.org/zope2/ZPublisher -- Paul Winkler http://www.slinkp.com
On Tuesday 20 February 2007 17:39, Paul Winkler wrote: <snip>
Another option is that the object at /articles could be an instance of a class that looks something like:
class MyArticleContainer():
def __before_publishing_traverse__(self, unused, request): # Save the rest of the path as 'traverse_subpath'. request.set('traverse_subpath', reversed(request['TraversalRequestNameStack'])) # Tell the publisher to stop traversing now. request['TraversalRequestNameStack'][:] = []
def __call__(self, *args, **kw): subpath = self.REQUEST['traverse_subpath'] data = get_data_however_you_like(subpath) return data
For background, read the stuff about traversal hooks at http://wiki.zope.org/zope2/ZPublisher
Hm.. that looks more like what I had in mind when I posted, but allso quite a bit more overhead than rewriterules. At least for me that has never written a product. I will have to think about that one.. Would you care to elaborate a little bit on what the tradeoffs would be? If I wanted my articles in many variants, ie. a large state space, I guess this way would be good? Gaute
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Gaute Amundsen wrote:
On Tuesday 20 February 2007 17:39, Paul Winkler wrote: <snip>
Another option is that the object at /articles could be an instance of a class that looks something like:
class MyArticleContainer():
def __before_publishing_traverse__(self, unused, request): # Save the rest of the path as 'traverse_subpath'. request.set('traverse_subpath', reversed(request['TraversalRequestNameStack'])) # Tell the publisher to stop traversing now. request['TraversalRequestNameStack'][:] = []
def __call__(self, *args, **kw): subpath = self.REQUEST['traverse_subpath'] data = get_data_however_you_like(subpath) return data
For background, read the stuff about traversal hooks at http://wiki.zope.org/zope2/ZPublisher
Or you can give 'MyArticleContainer' a '__getitem__' method which returns a non-persistent instance fabricated from the key passed to it (assuming you need only that one bit of information). Tres. - -- =================================================================== Tres Seaver +1 540-429-0999 tseaver@palladion.com Palladion Software "Excellence by Design" http://palladion.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFF3F8g+gerLs4ltQ4RAkkgAJ9pTuCKKAOrPSFOUVvrVI9FYhhxOQCg0Cwz JAg6TDKquR2kKhvJoFoIUUE= =OGlZ -----END PGP SIGNATURE-----
On Wed, Feb 21, 2007 at 12:32:30PM +0100, Gaute Amundsen wrote:
On Tuesday 20 February 2007 17:39, Paul Winkler wrote: <snip>
Another option is that the object at /articles could be an instance of a class that looks something like:
class MyArticleContainer():
def __before_publishing_traverse__(self, unused, request): # Save the rest of the path as 'traverse_subpath'. request.set('traverse_subpath', reversed(request['TraversalRequestNameStack'])) # Tell the publisher to stop traversing now. request['TraversalRequestNameStack'][:] = []
def __call__(self, *args, **kw): subpath = self.REQUEST['traverse_subpath'] data = get_data_however_you_like(subpath) return data
For background, read the stuff about traversal hooks at http://wiki.zope.org/zope2/ZPublisher
Hm.. that looks more like what I had in mind when I posted, but allso quite a bit more overhead than rewriterules. At least for me that has never written a product.
Well, it may feel like a lot of work to you the first time :-)
I will have to think about that one.. Would you care to elaborate a little bit on what the tradeoffs would be?
My approach might be good if: - the logic for looking up articles is more complex than can be comfortably expressed using apache rewrite rules, and/or - the logic for looking up articles requires state that's internal to zope. You could instead follow Andreas' suggestion and use a through-the-web Script. It might look something like: article_id = traverse_subpath[-1] data = context.get_the_data_somehow(article_id) return data Your URLs would then look like: http://example.com/folder_id/script_id/article_id Note that naming the script index_html doesn't really help, because http://example.com/folder_id/article_id wouldn't invoke the script. -- Paul Winkler http://www.slinkp.com
On Wednesday 21 February 2007 16:27, Paul Winkler wrote:
On Wed, Feb 21, 2007 at 12:32:30PM +0100, Gaute Amundsen wrote:
On Tuesday 20 February 2007 17:39, Paul Winkler wrote: <snip> <snip>
You could instead follow Andreas' suggestion and use a through-the-web Script. It might look something like:
article_id = traverse_subpath[-1] data = context.get_the_data_somehow(article_id) return data
Your URLs would then look like:
I had no idea it could be that simple. And certainly not from Andreas riddles ;) That will do nicely for now. Thanks Gaute
--
Paul Winkler http://www.slinkp.com _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Tuesday 20 February 2007 16:37, Andreas Jung wrote:
--On 20. Februar 2007 16:30:43 +0100 Gaute Amundsen <gaute@div.org> wrote:
Hi.
Normally a url to an article in our CMS system would look roughly like this: http://www.dom.tld/aritcles/25245
Where 25245 would be an object of some metatype, say 'article'.
Now what if I wanted to get the data for that page from an sql server instead, I would expect to end up with a url like this:
http://www.dom.tld/aritcles/index_html?id=25245
Now, having recently read the Roy Fielding paper, and expecting to do some caching, I would much prefer to have it the first way, even when I have to get the data out of a DB.
Anybody have any ideas on how this might be done?
How about Apache rewrite rules or a script called index_html that fetches the object by its ID? That's pretty much boring straight-forward stuff :-)
-aj
I was certain that the "index_html" way required the "?" preceding the arguments to work. Easy enoug to test. Will do. Hm.. but rewrite rules would be better I guess. I was actually considering that solution for something else*, just before posting this.. D'oh! (* the task of having the same objects appear under different urls in different states ) Thanks Gaute
Surely it should make no difference to caching as they are both GETs? The first is certainly prettier, if you want URLs like that then ZSQL methods should be able to help you (read the online zope book), or roll your own looking at the traverse subpath. Laurence Gaute Amundsen wrote:
Hi.
Normally a url to an article in our CMS system would look roughly like this: http://www.dom.tld/aritcles/25245
Where 25245 would be an object of some metatype, say 'article'.
Now what if I wanted to get the data for that page from an sql server instead, I would expect to end up with a url like this:
http://www.dom.tld/aritcles/index_html?id=25245
Now, having recently read the Roy Fielding paper, and expecting to do some caching, I would much prefer to have it the first way, even when I have to get the data out of a DB.
Anybody have any ideas on how this might be done?
Regards
Gaute Amundsen _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
participants (5)
-
Andreas Jung -
Gaute Amundsen -
Laurence Rowe -
Paul Winkler -
Tres Seaver