[Grok-dev] should I submit prototype "trails" helper?

Brandon Craig Rhodes brandon at rhodesmill.org
Fri Sep 21 00:40:02 EDT 2007


The SQL-back-ended Grok application I am writing has lots of dummy
objects that "live at" the halfway points of various URLs.  Writing
all of them, and trying to get Traversers set up correctly to link
them all together, was fun for about the first hour.  Then it's been
getting less fun as I have to adjust and maintain them.

So tonight I sat down and, thanks to the magic of adapter-based
programming, was able to write a simple implementation of something
I've, for the moment, called "trails".  They let you do something like
this (let's imagine a real estate application):

    import grok
    from trails import TrailHead, Trail
    from my_orm import User, House, Bid, HouseComment

    class App(grok.Application, grok.Container):
        pass

    class AppTrailHead(TrailHead):
        grok.context(App)
        trails = [
            Trail('/user/:username', User),
            Trail('/house/:house_id', House),
            Trail('/bid/:house_id/:username', Bid),
            Trail('/comment/:comment_id', HouseComment),
            ]

Thanks to the magic of some Traversers and also a multiply-auto-
registered TrailAbsoluteURL class (so that both traversal forward and
calling view.url() on these ORM objects work), users will find that
they can visit URLs like the following, and view the objects
instantiated as shown on the right:

   /user/esmith3               User(username='esmith3')
   /house/3918                 House(house_id='3918')
   /bid/3918/esmith3           Bid(house_id='3918', username='esmith3')
   /comment/49912              Comment(comment_id='49912')

Improvements upon this scheme could easily be made.  And perhaps it
does something poorly that other web frameworks already have better
patterns for, that we ought to copy?

But, if anyone is interested, I'd love to make this available in
whatever the Grok equivalent is to "z3c".  Let me know.  It is very
limited at the moment, but does handle things like the above example
correctly.

-- 
Brandon Craig Rhodes   brandon at rhodesmill.org   http://rhodesmill.org/brandon


More information about the Grok-dev mailing list