[Twisted-Python] Woven: access to submodels

Mary mary-twisted at puzzling.org
Sat Jul 12 22:21:36 EDT 2003

I'm trying to replicate the functionality of http://eyes.puzzling.org/
(currently powered by a bunch of Python CGI scripts running on an Apache
webserver) in Woven.

eyes.puzzling.org is a kind of a minimalist blog, with pages that list
recent entries, the authors of the entries, and so on.

At the moment, I'm trying to replicate the authors functionality (2
different sets of pages: one which lists all the authors and links to
their individual page; and one for each author, which lists their
individual articles.

The /authors/ page (listing all the authors and linking to their
individual page) is generated by objects of the AuthorsPage subclass.

For each author, I would like to generate a /author/AUTHORNAME/ page
which lists their individual entries. I am trying to create these pages
from AuthorsPage's getDynamicChild method. The idea is to pass the
already constructed submodel -- a dictionary representing the individual
author -- to the IndividualPage object I will construct to represent the
individual author's page, rather than have IndividualPage make its own
database query for that information.

At present, as you can see below, I'm using getSubmodel to retrieve
the (dictionary) submodel associated with a particular author. However,
this returns a twisted.web.woven.model.DeferredWrapper instance, rather
than the dictionary itself. Should I make IndividualPage add callbacks
to the DeferredWrapper, or am I missing something crucial about
retrieving submodels or is this model more fundamentally incorrect?

For example, should I be passing submodels around like that at all?

If so, is there a higher level call I should make to access the

The two classes following are the AuthorsPage and IndividualPage classes
described above.



class IndividualPage(page.Page):
    # getTemplate munges two template files together
    template = getTemplate("/home/mary/cvs/Projects/Eye/author-page.html")

    def initialize(self, *args, **kwargs):
        self.amodel = kwargs['amodel']

    def wmfactory_author(self, request):
        return self.amodel['author']
class AuthorsPage(page.Page):

    template = getTemplate("/home/mary/cvs/Projects/Eye/authors-page.html")

    def initialize(self, *args, **kwargs):
        self.dbpool = kwargs['pool']
        self.authors = None

    def wmfactory_authors(self, request):
        query = self.dbpool.runQuery("SELECT DISTINCT posts.name, link, password FROM authors, posts WHERE authors.name = posts.name ORDER BY name;")
        return query
    def makeAuthors(self, rows):
        Construct self.authors, return them as a list
        @return a list of author dictionaries
        authors = {}
        for row in rows:
            (name, link, password) = row
            authors[name] = {
                'name' : name,
                'link' : link,
                'password' : password
        return authors

    def wvfactory_alink(self, request, node, model):
        return AuthorLink(model)

    def getDynamicChild(self, name, request):
        return IndividualPage(amodel=self.getSubmodel('authors')[name]))

More information about the Twisted-Python mailing list