[Moin-user] http_redirect from within a macro?

Neal Holtz nholtz at cee.carleton.ca
Mon Aug 15 19:35:47 EDT 2011


On August 15, 2011 04:38:04 PM Paul Boddie wrote:
> On Monday 15 August 2011 20:52:42 Neal Holtz wrote:
> >
> > Is it possible to request an http redirect from within a macro
> > handler?  Using moinmoin 1.9.3, when I try:
> >
> > def macro_Quiz( macro, filename ):
> > ....
> >           macro.request.http_redirect( 'http://www.disney.com', code=301 )
> >
> >
> > <<Quiz: execution failed [proxy exception for
> > <werkzeug.wrappers.BaseResponse object at 0xaacbb4c>] (see also the log)>>
> 
> What does the log say? 

The traceback is:

2011-08-15 19:22:03,910 ERROR MoinMoin.macro:132 Macro Quiz (page: 'TestQuiz') raised an exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/MoinMoin/macro/__init__.py", line 122, in execute
    return execute(self, args)
  File "/usr/local/lib/python2.7/dist-packages/MoinMoin/macro/__init__.py", line 107, in <lambda>
    _self.request, call, _args, [_self])
  File "/usr/local/lib/python2.7/dist-packages/MoinMoin/wikiutil.py", line 1954, in invoke_extension_function
    return function(*fixed_args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/MoinMoin/macro/Quiz.py", line 39, in macro_Quiz
    macro.request.http_redirect( 'http://www.disney.com', code=301 )
  File "/usr/local/lib/python2.7/dist-packages/MoinMoin/web/contexts.py", line 276, in http_redirect
    abort(redirect(url, code=code))
  File "/usr/local/lib/python2.7/dist-packages/MoinMoin/support/werkzeug/exceptions.py", line 436, in __call__
    raise _ProxyException(code)
_ProxyException: proxy exception for <werkzeug.wrappers.BaseResponse object at 0x32b3490>



> I looked at some actions and you'd use the 
> http_redirect method on the request in that environment, but I don't know if 
> that works for macros, mostly because macros are executed during output 
> production, at which point it's probably a bit late to attempt a redirect: 
> the HTTP status code will have been set and page content partially written.

>From a quick look at the code, because this is done partially by raising an
exception, I was hoping that it would work even under those conditions.  I haven't
dug into the macro handling code, but the some relevant lines for doing the
redirect are:

In contexts.py:

    def http_redirect(self, url, code=302):
        """ Raise a simple redirect exception. """
        # werkzeug >= 0.6 does iri-to-uri transform if it gets unicode, but our
        # url is already url-quoted, so we better give it str to have same behaviour
        # with werkzeug 0.5.x and 0.6.x:
        url = str(url) # if url is unicode, it should contain ascii chars only
        abort(redirect(url, code=code))


In: support/werkzeug/exceptions.py:

class Aborter(object):
    """
    When passed a dict of code -> exception items it can be used as
    callable that raises exceptions.  If the first argument to the
    callable is a integer it will be looked up in the mapping, if it's
    a WSGI application it will be raised in a proxy exception.

    The rest of the arguments are forwarded to the exception constructor.
    """

    def __init__(self, mapping=None, extra=None):
        if mapping is None:
            mapping = default_exceptions
        self.mapping = dict(mapping)
        if extra is not None:
            self.mapping.update(extra)

    def __call__(self, code, *args, **kwargs):
        if not args and not kwargs and not isinstance(code, (int, long)):
            raise _ProxyException(code)
        if code not in self.mapping:
            raise LookupError('no exception for %r' % code)
        raise self.mapping[code](*args, **kwargs)

abort = Aborter()

I wonder if the macro executor is expected to handle that _ProxyException ...

I'll have a look at invoke_extension_function ...

> 
> Paul
> 
> ------------------------------------------------------------------------------
> uberSVN's rich system and user administration capabilities and model 
> configuration take the hassle out of deploying and managing Subversion and 
> the tools developers use with it. Learn more about uberSVN and get a free 
> download at:  http://p.sf.net/sfu/wandisco-dev2dev
> _______________________________________________
> Moin-user mailing list
> Moin-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/moin-user
> 

-- 
Neal Holtz                             http://cee.carleton.ca/~nholtz
Dept. of Civil and Environmental Engineering,   Carleton University,   
Ottawa, Ontario, Canada K1S 5B6.               nholtz at cee.carleton.ca
Public Key:  http://holtz3.cee.carleton.ca/~nholtz/pubkey.asc
Office-Hours: http://holtz3.cee.carleton.ca/~nholtz/office-hours.html
Free-Busy: http://holtz3.cee.carleton.ca/~nholtz/free-busy.cgi




More information about the Moin-user mailing list