[Grok-dev] Re: GrokPaste: a grok pastebin app

Martijn Faassen faassen at startifact.com
Sat Mar 24 16:17:04 EDT 2007


Hey,

Kevin Teague wrote:
> Traversal
> ------------
> 
> Human readable URLs are not yet as easy to work with as other framworks, 
> Rails/Django etc. It might be cool to have some Traversal conventions. 
> Perhaps some convenience methods in the Traversal base class?
> 
> To support URLs such as /tags/foo/bar I wrote:
> 
>     def traverse(self, name):
>         if name == 'tags':
>             self.request.form['tags'] = self.request.getTraversalStack()
>             self.request.setTraversalStack([])
>             return queryMultiAdapter(
>                 (self.context, self.request), name='tagsearch')
>         else:
>             # try to get the item from the container
>             return self.context.get(name)

While I'm not 100% sure what you're trying to do here, I'd use models in 
my design to simplify this a lot:

class Tags(grok.Model):
   def traverse(self, name):
       return Tag(tag)

class Tag(grok.Model):
    def __init__(self, tag):
        self.tag = tag

And then define a view on 'tag'.

If you're trying to deal with multiple tags like /tags/one/two you could 
  define Tag like this:

class Tag(grok.Model):
    def __init__(self, tags):
       self.tags = tags

    def traverse(self, name):
       return Tag(self.tags + [name])

So information get carried forward. Grokstar takes this approach for its 
calendar URLs, where we have Year, Month and Day objects with custom 
traversing behavior.

Grok models don't *have* to be persistent; they won't be persisted if 
you don't put them in a container or attribute of something already 
persistent.  You can do all kinds of fancy stuff with URLs if you model 
things this way, and I find it writes itself quite naturally overall. A 
nice property of this style is that it's very easy to provide some view 
if someone just wrote /tags/ without any actual tags below it.

Perhaps this design occurred to you and you chose not to use it for some 
reason. Share your reason, in that case. :)

This design pattern is definitely one for the tutorial, too.

Regards,

Martijn



More information about the Grok-dev mailing list