durusmail: quixote-users: Question about _q_getname
Question about _q_getname
2002-01-24
2002-01-24
2002-01-24
2002-01-24
2002-01-25
2002-01-25
Question about _q_getname
David Ascher
2002-01-24
I'm building a "photo album" site with Quixote, as part of research for
a chapter in Learning Python, 2nd ed.  I'm stumbling on a problem which
is puzzling me.

One of the objects in the app is a "Folder" object.  the Folder can have
images in it, and other Folders.  Folders show snapshots of the images,
and clicking on an image takes you to a "view" of that image.

The names of the folders and images are dynamic (they correspond to
directories on disk), so I want to override _q_getname and do something
like:

    def _q_getname(self, request, component):
        if component in self._dirs:
            return Folder(request, component)
        if component in self._files:
            return Image(request, component)

so that given the _q_index output for folder a, which would include
links to:

        a/pic1.jpg
        a/pic2.jpg
        a/b/

Now, this fails unless I do something like:

    def __init__(self, request):
        self._path = request.get_path()

    def _q_getname(self, request, component):
        if request.get_path() == self._path
            return self
        if component in self._dirs:
            return Folder(request, component)
        if component in self._files:
            return Image(request, component)

So, I have a few questions:

1) why is _q_getname called when I expect _q_index to suffice?

2) if I put a debugging statement before the "return self" and then
lookup object a/b/  (listing of folder b inside folder a), I get _four_
printed lines -- a, b, a, b.  What's going on?

Thanks for any insight.

--david


reply