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

kevin at mcweekly.com kevin at mcweekly.com
Fri Sep 21 02:42:10 EDT 2007


Brandon,

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.

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.

    import grok
    from traversables import Traverser, Traversable

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

    class AppTraverse(Traverser):
        grok.context(App)

    class UserView(grok.View):
        grok.context(Traversable)
        grok.name('app::user/++subpath++') # handles /user/esmith3

        def render(self):
            return ' '.join(self.subpath)
            # self.subpath is similar to zope2's traverse_subpath
            # it returns a list of remaining subpath elements
              # /user/esmith3 yields ['esmith3']
            # /user/grok/caveman yields ['grok', 'caveman']

What if /user is visited a path without subpath elements? Like
grok.Traverse
it is handeled separately.

    class UserIndex(grok.View):
         grok.context(Traversable)
         grok.name('app::user')  #  handles /user but not
/user/esmith3

     class HouseView(grok.View):
         grok.context(Traversable)
         grok.name('app::house/++subpath++')
     
         def update(self):
             pass :#use update, render and template as normal

Although this technique may be considered too radical, it greatly
simplified the big tangled mess that came of using grok.Traverser

Brandon, what I really like about your technique is that all the urls
are in one place. Very similar to Routes.

AFAIK you can post something in the megrok namespace. Seems like
these are good candidates for the wiki due to their highly
experimental nature.

Kevin Smith

Quoting Brandon Craig Rhodes <brandon at rhodesmill.org>:

> 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
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20070920/e54fc873/attachment.htm


More information about the Grok-dev mailing list