[mod_python] Questions on _call_ with mp servlets and python

Daniel Popowich dpopowich at comcast.net
Fri Sep 3 17:04:39 EDT 2004


Vinj Vinj writes:
> I figured out a way to do this, but I'm not sure it is
> too elegant:
> 
> Have your output go to a string in your _call_
> do_some_function()
> 
> so replace self.write with self.result.append
> 
> and in your write_content function have the following:
> 
> self.writeln("".join(self.result))

No, I think you want to do what I said in my previous response: have
your method return True so write_html() is not called.

> Also does anyone know how to redirect the current
> request to another servlet so that the current req
> object is kept intact. The problem in some more
> details is as follows. I have an application which
> checks to see if a user is logged in through the
> session. However, if the user is not logged in I want
> to redirect to the login servlet with the error
> message sent as part of the req object. 
> 
> I can do that by /Login?err_msg="You have to be logged
> in" however then anyone can send messages to my login
> screen which get displayed on the return. I'd rather
> have someway (secure) of passing the err_msg
> internally. 

This is what I've done:

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!

Daniel Popowich
-----------------------------------------------
http://home.comcast.net/~d.popowich/mpservlets/




More information about the Mod_python mailing list