Brandon,<p>
<br />
LOL, I have felt your pain. Although powerful, creating even a medium number of
Traversers get tedious and difficult to maintain. Since creating objects
wouldn't help in my use case, my solution is centered around simple
grok.Views.<br />
<br />
It is tenatively called Traversables and basically acts as a named view
dispatcher. I use it to pull in objects from various parts of the database to
assemble on landing pages and to create urls without creating persistent
objects.<br />
<br />
import grok<br />
from traversables import Traverser, Traversable<br />
<br />
class App(grok.Application, grok.Container):<br />
pass<br />
<br />
class AppTraverse(Traverser):<br />
grok.context(App)<br />
<br />
class UserView(grok.View):<br />
grok.context(Traversable)<br />
grok.name('app::user/++subpath++') # handles /user/esmith3<br />
<br />
def render(self):<br />
return ' '.join(self.subpath)<br />
# self.subpath is similar to zope2's traverse_subpath<br />
# it returns a list of remaining subpath elements<br></br>
# /user/esmith3 yields ['esmith3']<br />
# /user/grok/caveman yields ['grok', 'caveman']<br />
<br />
What if /user is visited a path without subpath elements? Like grok.Traverse <br
/>
it is handeled separately.<br />
<br />
class UserIndex(grok.View):<br />
grok.context(Traversable)<br />
grok.name('app::user') # handles /user but not
/user/esmith3</p><p><br /> class HouseView(grok.View):<br />
grok.context(Traversable)<br />
grok.name('app::house/++subpath++')<br />
<br />
def update(self):<br />
pass :#use update, render and template as normal<br />
<br />
<br />
Although this technique may be considered too radical, it greatly simplified the
big tangled mess that came of using grok.Traverser<br />
<br />Brandon, what I really like about your technique is that all the urls are
in one place. Very similar to Routes.<br />
<br />
AFAIK you can post something in the megrok namespace. Seems like these are good
candidates for the wiki due to their highly experimental nature.<br />
<br />
<br />
Kevin Smith<br />
<br />
<br />
<br />
<br />
Quoting Brandon Craig Rhodes <brandon@rhodesmill.org>:<br />
<br />
> The SQL-back-ended Grok application I am writing has lots of dummy<br />
> objects that "live at" the halfway points of various URLs.
Writing<br />
> all of them, and trying to get Traversers set up correctly to link<br />
> them all together, was fun for about the first hour. Then it's been<br />
> getting less fun as I have to adjust and maintain them.<br />
><br />
> So tonight I sat down and, thanks to the magic of adapter-based<br />
> programming, was able to write a simple implementation of something<br />
> I've, for the moment, called "trails". They let you do something
like<br />
> this (let's imagine a real estate application):<br />
><br />
> import grok<br />
> from trails import TrailHead, Trail<br />
> from my_orm import User, House, Bid, HouseComment<br />
><br />
> class App(grok.Application, grok.Container):<br />
> pass<br />
><br />
> class AppTrailHead(TrailHead):<br />
> grok.context(App)<br />
> trails = [<br />
> Trail('/user/:username', User),<br />
> Trail('/house/:house_id', House),<br />
> Trail('/bid/:house_id/:username', Bid),<br />
> Trail('/comment/:comment_id', HouseComment),<br />
> ]<br />
><br />
> Thanks to the magic of some Traversers and also a multiply-auto-<br />
> registered TrailAbsoluteURL class (so that both traversal forward and<br />
> calling view.url() on these ORM objects work), users will find that<br />
> they can visit URLs like the following, and view the objects<br />
> instantiated as shown on the right:<br />
><br />
> /user/esmith3 User(username='esmith3')<br />
> /house/3918 House(house_id='3918')<br />
> /bid/3918/esmith3 Bid(house_id='3918', username='esmith3')<br
/>
> /comment/49912 Comment(comment_id='49912')<br />
><br />
> Improvements upon this scheme could easily be made. And perhaps it<br />
> does something poorly that other web frameworks already have better<br />
> patterns for, that we ought to copy?<br />
><br />
> But, if anyone is interested, I'd love to make this available in<br />
> whatever the Grok equivalent is to "z3c". Let me know. It is
very<br />
> limited at the moment, but does handle things like the above example<br />
> correctly.<br />
><br />
> --<br />
> Brandon Craig Rhodes brandon@rhodesmill.org <a target="_blank"
href="http://rhodesmill.org/brandon">http://rhodesmill.org/brandon</a><br />
> _______________________________________________<br />
> Grok-dev mailing list<br />
> Grok-dev@zope.org<br />
> <a target="_blank"
href="http://mail.zope.org/mailman/listinfo/grok-dev">http://mail.zope.org/mailman/listinfo/grok-dev</a><br
/>
><br />
<br />
</p>