[Zope] How to setStatus(301) for zException Redirect

Norbert Marrale norbertmarrale at yahoo.com
Fri Oct 8 16:32:38 EDT 2010


Using Zope 2.10.8 I'd like to let an access rule trigger a 301 'moved 
permanently' redirect.

I created an external method to raise the exception, which almost does the trick 
except for the fact that it generates a 302 Moved Temporarily status code.

from zExceptions import Redirect
def myDispatcher(self, url):
  raise Redirect(url)

Tried request.setStatus(301) both in my access rule and the external method, but 
this effectively disables the redirect. Also tried modifying my external method 
similar to request.response.redirect() as follows. Still no luck.

from zExceptions import Redirect
def myDispatcher(self, url, status, lock):
  raise Redirect(url, status, lock)

I finally traced it to HTTPResponse.py starting around line 763-790 the 302 
status seems to be hardcoded. How would I go about changing this? I'd rather not 
hack directly in the Zope code...

        self.setStatus(t)
        if self.status >= 300 and self.status < 400:
            if isinstance(v, str) and absuri_match(v) is not None:
                if self.status == 300:
                    self.setStatus(302)
                self.setHeader('location', v)
                tb = None # just one path covered
                return self
            elif isinstance(v, Redirect): # death to string exceptions!
                if self.status == 300:
                    self.setStatus(302)
                self.setHeader('location', v.args[0])
                self.setBody('')
                tb = None
                return self
            else:
                try:
                    l, b = v
                    if (isinstance(l, str)
                        and absuri_match(l) is not None):
                        if self.status == 300:
                            self.setStatus(302)
                        self.setHeader('location', l)
                        self.setBody(b)
                        tb = None # one more patch covered
                        return self
                except:
                    pass # tb is not cleared in this case



Norbert


      


More information about the Zope mailing list