On Saturday, February 18, 2012 5:12:55 AM UTC+1, Mike Orr wrote:
>
> On Thu, Feb 16, 2012 at 7:57 AM, pipoun wrote:
> > I am trying to find a way to mix a bit of the traversal resource building
> > and the pylons controllers functions that have mapped parameters. I am
> also
> > using sqlachemy as orm (so no ZODB).
>
> > Basically, I'm looking for a way to avoid as much as possible the manual
> > extraction of parameters from the request object (especially
> > request.matchdict).
>
> I'm having trouble understanding your question. Are you saying that
>
> you'd rather use traversal completely, and not use URL dipatch? Do you
> want an example of using traversal with SQLAlchemy? When you say
> "avoid the manual extraction of parameters", do you mean that you
> don't want to use URL dispatch, or that you just don't like
> request.matchdict for some reason?
>
Sorry for my lack of clarity, the different possibilities and guidelines
didn't help me to get my mind cleaned up.
The main goal is 1/ to avoid the manual instantiation of orm objects, and
2/ mapping the rest of the URL to function parameters (i.e. pylons
controller). And I was just wondering if traversal was a better fit for
achieving this, because it seemed more natural.
So as a fact if I use URL dispatch, it will be playing with
request.matchdict.
I think 1/ is actually quite close to your post "Limited traversal" (which
gave me an example of how to do it).
You can use traversal with SQLAlchmey. The Kotti project is the best
> example of it, and you can study the source but it's complex. I don't
> know of a SQLAlchemy-wiki-traversal tutorial, perhaps somebody should
> write one. There are two ways to approach traversal with SQLAlchemy.
>
> If you're doing a single level without recursion; e.g., "/page/ABC",
> your root would have a "page" key, which would be some object that
> queries the Page table and fetches an "ABC" record. That record either
> becomes the resource, or you have some kind of resource class and put
> the record inside it.
>
Yeah I was thinking about one level of recursion, not a generic tree, but
more like a rake.
Anyway, I went to #pyramid to lurk/ask/whine and mcdonc helped a lot.
I've figured out that for a classic traversal with sqla system, I have to
design table with a self referencing foreign key.
Then I was strongly recommended not to mix traversal & url dispatch.
And finally, he showed me that I could do everything with a view mapper.
I've modified a chunk of code from the "Using Hooks" page
{{{
class PylonsControllerViewMapper(object):
def __init__(self, **kw):
self.kw = kw
def __call__(self, view):
attr = self.kw['attr']
def wrapper(context, request):
matchdict = request.matchdict.copy()
matchdict.pop('action', None)
view_inst = view()
if view.orm_type is not None:
orm_id = matchdict.pop('id', None)
if orm_id is not None:
view_inst.orm_inst = DBSession.query(
view.orm_type).filter_by(id=orm_id).one()
meth = getattr(view_inst, attr)
if matchdict:
return meth(**matchdict)
else:
return meth()
return wrapper
class BaseController(object):
__view_mapper__ = PylonsControllerViewMapper
orm_type = None
class PageController(BaseController):
orm_type = models.Page
}}}
The route will be of course following this pattern: '/page/{id}/{action}'.
(This is not the best example as all the subparts of the url are going to
be chomped by the view mapper)
> If you're doing multi-level recursion ("/page/A/B/C" or perhaps
> "/A/B/C"), you would need a self-referential table; i.e., a table with
> a foreign key column pointing to the same table. Then each record can
> point to its parent. Kotti is an example of this.
>
> Traversal is most commonly used with an object database because both
> of them are naturally recursive. But I'm seeing more and more use of
> traversal with SQLAlchemy, so perhaps it will become just as common in
> the future?
>
By the way, when looking at the Akhet documentation (latest), the pylons
guide has been moved to the pyramid cookbook, which is not online yet.
Where can I get it otherwise? Do you know if it will be online soon?
Thanks a lot!
pipoun
> --
> Mike Orr
>
>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/pylons-discuss/-/kpenaiA7tuMJ.
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.