[albatross-users] Changing the status code

Dave Cole djc at object-craft.com.au
Thu Nov 28 10:09:43 EST 2002


> I'm having trouble setting the status of a page though albatross
> using mod_python.

Albatross does not really help you here.  At the moment all
non-exception processing returns status 200.  The culprit is the run()
method in the Application class.

The status() method on the Request class translates an HTTP status
code to something that can be returned as a status code to the
execution framework.  That seems a little obscure...  Consider the way
that a mod_python application works:

from albatross.apacheapp import Request
   :
   :
app = App()

def handler(req):
    return app.run(Request(req))

mod_python expects the return value for the handler() function to be
zero if everything is OK.  The status() method in apacheapp.Request
simply translates the 200 from the Application.run() method to zero.

> I've snipped out extra lines here, but the idea is to set a cookie
> with the redirect.  The ctx.request.redirect function ignores any
> custom headers, so I had to roll my own here.

Something which we could certainly improve is the interface for
manipulating headers.

> and then in the StdForm class:
> 
>     def page_process(self, ctx):
> 
>         ctx.request.status(apache.HTTP_MOVED_PERMANENTLY)

The status() method does not change the status which is returned by
the handler function.  Currently we do not have a way to do this.

If we do not get any objections, I suggest that we make the following
changes:

In all Request classes:

def Request:
    def __init__(self, req):
        self.__status = 200

    def set_status(self, status):
        self.__status = status

    def status(self):
        # For mod_python...
        if self.__status == 200:
            return 0
        return self.__status

Then in the Application.run() method:

    def run(self, req):
          :
          :
        return req.status()

This would allow you to simply change the returned status by doing a
small variation of your code above:

    def page_process(self, ctx):
        ctx.request.set_status(apache.HTTP_MOVED_PERMANENTLY)

- Dave

-- 
http://www.object-craft.com.au




More information about the Albatross-users mailing list