Hi Mike,

|--==> On Wed, 8 Feb 2012 11:19:49 -0800, Mike Orr <[email protected]> said:

  MO> On Wed, Feb 8, 2012 at 9:37 AM, Free Ekanayaka <[email protected]> 
wrote:
  >>Hi Nikolaos,
  >>
  >>|--==> On Tue, 7 Feb 2012 13:46:25 -0800 (PST), papagr 
<[email protected]> said:
  >>
  >> [...]
  >> p> That's right. A ProjectRepository plays the role of a Context.
  >> p> Unfortunately, I have to work with a legacy SQL database and traversal
  >> p> is not a natural fit. It requires extra coding to setup a custom Root
  >> p> object where Repository instances are registered during startup.
  >>
  >>Yes, there's a bit of overhead, but I think it pays off. I've started
  >>writing a python_contextroutes package that should help with this use
  >>case, if you're interested in it I can send it to you or publish it
  >>somewhere (it's basically done, but lacks documentation and packaging).

  MO> What will this contextroutes package do, or what kinds of cases would
  MO> it be applicable for?

  MO> I have come up with some cases where I might use a bit of traversal,
  MO> or traversal after a route (/admin/*traverse), or traversal on a
  MO> non-URL path specified by the route (traverse=). Where would
  MO> contextroutes fit into this, or which cases would it be useful in?

Maybe yes, it indeed mixes traversal and routes (i.e. url dispatch
routes). For example, let's say you have some Article model objects
stored in a database (possibly using an ORM), along with some Comment
model objects too, with a one-to-many relationship between them (an
article can be associated with several comments).

No you want to have urls like:

- /article/<name>: with traversal returning an Article object with the
  given name and a view registered to adapt an Article context object

- /article/<name>/comment/<n>: with traversal returning the nth
  Comment model object for the given article

The idea is to create ContextRoutes classes for traversing your model,
for example:


class RootRoutes(ContextRoutes):

    @route("/article/{name}")
    def article(self, name):
        # self.context is an instance of your traversal Root
        return self.context.get_article(name)


class ArticleRoutes(ContextRoutes):


    @route("/comment/{n}")
    def comment(self, n):
        # self.context is an instance of Article, returned by
        # RootRoutes
        return self.context.get_comment(n)


where Root.get_article and Article.get_comment possibly perform SQL
lookups via your ORM.

Then you need to register your ContextRoutes, associating them with
certain contexts (pretty much as you would do when registering views for
certain contexts):

registry.registerAdapter(RootRoutes, (Root, IRequest))
registry.registerAdapter(ArticleRoutes, (Root, IRequest))

Finally you can use a ContextRoutesTraverser to take context routes into
account, for example:

request = DummyRequest()
request.environ["PATH_INFO"] = "/article/hello/comment/1"
traverser = ContextRoutesTraverser(Root())
info = traverser(request)
info["context"]  # This would be set with the expected Context object

The code is very simple and makes use pyramid.urldispatch.Route under
the hood:

https://gist.github.com/1772990
https://gist.github.com/1773008 (tests)

Hope it helps,

Free

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to