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

Daniel Popowich dpopowich at comcast.net
Fri Oct 1 11:14:41 EDT 2004

Martin Slouf writes:
> first of all, thanks for the answer.
> ...
> 1. im bound to use PSP -- graphics and gui features are done by
> another guy, we just cooperate on form fields, i think that it is no
> problem with mod_python.servlet (i agree, that code generated HTML
> definitely has its advances, but i cant use it now.)

I Understand, but disagree.  If you take the point of view that a
designer of an interface is just that, a designer, not a coder, then
you do not have to work in PSP-like systems.  If you were writing a
Tkinter app, what would you do if you wanted the services of a graphic
designer to create a usable interface?  Design != Code.

> to use PSP properly i would just call return the psp page from the
> 'write_content' method, right?

Depends on your PSP.  If your psp files are partial pages or page
fragments and you want to use HTMLPage to generate the page proper,
then yes, you can make calls to PSP from within write_content.  BTW,
I would make your calls to run() like this:

    def write_content(self):
	template = psp.PSP(self.req, filename='template.html')

then you can reference servlet inside your psp files and get all the
goodies from form processing (and even make method calls on your
servlet).  I think you want to avoid making any references to "form"
or "session" inside your psp code if using it from mpservlets --
haven't done any testing, but there might be a collision of effort,
since both systems encapsulate those objects -- instead, reference
servlet.form and servlet.session.

If your psp files are complete pages, then you don't want to use
HTMLPage, but rather subclass Servlet directly.  Something like:

    class PSPServlet(Servlet):
	def respond(self):
	   if not Servlet.respond(self):
	      template = psp.PSP(self.req, filename='template.html')
	      return True
> now, finally, the question for you:
> with mod_python.servlet it seems like just one class is involved in
> request processing and im probably unable to "call" another class in
> case of an error, am i right? or can i just instantiate a new class
> (new servlet) to handle the request in 'write_content' method,
> initialize it somehow (how?) and proccess it 'write_content' method?

Don't forget that you're working with pure python inside servlets; you
can instantiate any class (except other servlets) or call any method
or function you want to assist in processing a request.  If it's in
your PYTHONPATH, you can access it.

Also, if you truly want to pass control on to another servlet you can
always call internal_redirect() and the subrequest can get access to
the parent request.

Lastly, don't overlook the power of subclassing and python's ability
to manage methods as first-class objects.  I posted this not too long
ago for someone asking about managing authentication with mpservlets:

Originally posted 9/3/2004:

> Let's assume you have a servlet for your site that all servlets
> inherit from:
>   class MySitePage(HTMLPage):
>      ...
> and let's also assume that certain pages (not all) need
> authentication, then I would create another servlet:
>   class MySiteAuthPage(MySitePage):
>     ...
> 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!

Good luck,

Daniel Popowich

More information about the Mod_python mailing list