[mod_python] using callable classes instead of plain functions in handlers

Terry MacDonald terry.macdonald at dsl.pipex.com
Sat Oct 2 00:11:55 EDT 2004


> > Notice how it subclasses MySitePage.  In MySiteAuthPage.prep() I
> > check the session for proper authentication and set a boolean, say,
> > authenticated.  Then I override write_html() in MySiteAuthPage:
> > 
> >     def write_html(self):
> >         if not self.authenticated:
> >             save, self.write_content = self.write_content, self.genloginform
> >             MySitePage.write_html(self)
> >             self.write_content = save
> >         else:
> >             MySitePage.write_html(self)
> > 
> > where genloginform() makes calls to self.writeln() writing the login
> > form to the browser.  What this does is, if not authenticated,
> > momentarily replaces the write_content method of the servlet with the
> > login form, otherwise, if authenticated, just writes out the page.
> > 
> > Then all my pages for my app either subclass MySitePage or
> > MySiteAuthPage depending on whether or not the page needs
> > authentication.  No calls to internal_redirect are necessary!

Daniel,

I'm in the process of working with mod_servlet after working with
mod_publisher for a while.  I like it but I'm still working out for
myself the discernable benefits it has over publisher. Anyhow...

...I tried recreating the above page login/authentication, however, it
only worked when I overrode write_body_parts instead of write_html.
Overriding write_html produced nothing.  The browser page just stayed on
the page it was on before selecting the protected linked page.  I'm
still trying to understand why...any pointers?

Currently I use HTMLPage to form my page. But I use precompiled Cheetah
templates for the body of each page. e.g. my index page looks like
this...
from SitePage import SitePage
from let_index import let_index
                                                                                                                   class index(SitePage):

    def write_content(self):
        let_index.req = self.req
        self.write(let_index())

As an aside as I am using cheetah this way I take it I will have lots of
small files like this if I have lots of pages, whereas in publisher I
could have one index module holding many different page rendering
functions. Is this the way it has to be with the class/servlet based
approach?

Keep up the good work!

cheers
-- 
Terry
Registered Linux User # 311806





More information about the Mod_python mailing list