Changeset 3027

Show
Ignore:
Timestamp:
01/10/08 20:07:03 (10 months ago)
Author:
osimons
Message:

AddCommentMacro: Warning + req.redirect() issues fixed.

The macro used the first version warning style (req.warning()), but that was just a left-over from before moving to redirect on successful submit anyway - it would not have displayed to the user no matter what. The line is just removed. Fixes #2368.

The whole req.redirect() idea needed to be reworked as macros cannot really do redirects. A redirect raises RequestDone, and like all other exceptions from macros it gets swallowed by the Formatter - causing an AssertionError to appear in the logs when the request tries to write a second status/header/response output later.

The exception did not display to the user, but was annoying... Now the redirect sets a marker on the req object, and by implementing IRequestFilter it now re-raises the RequestDone there.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • addcommentmacro/0.11/addcomment/macro.py

    r2818 r3027  
    99from trac.util import TracError 
    1010from trac.util.text import to_unicode 
    11 from trac.web.chrome import add_link, add_script 
    12 from trac.wiki.api import parse_args 
     11from trac.web.api import IRequestFilter, RequestDone 
     12from trac.web.chrome import add_script 
     13from trac.wiki.api import parse_args, IWikiMacroProvider 
    1314from trac.wiki.macros import WikiMacroBase 
    1415from trac.wiki.model import WikiPage 
    1516 
     17from macropost.api import IMacroPoster 
    1618 
    1719class AddCommentMacro(WikiMacroBase): 
     
    2628    }}} 
    2729    """ 
    28      
     30    implements(IWikiMacroProvider, IRequestFilter, IMacroPoster) 
     31 
    2932    def expand_macro(self, formatter, name, content): 
    3033         
     
    109112            if submitted: 
    110113                page.text = newtext 
    111                 page.save(authname, 'Comment added', req.environ['REMOTE_ADDR']) 
    112                 req.warning("Comment saved.") 
    113                 req.redirect(page_url) 
     114                page.save(authname, 'Comment added.', req.environ['REMOTE_ADDR']) 
     115                # We can't redirect from macro as it will raise RequestDone 
     116                # which like other macro errors gets swallowed in the Formatter. 
     117                # We need to re-raise it in a post_process_request instead. 
     118                try: 
     119                    req._outheaders = [] 
     120                    req.redirect(page_url) 
     121                except RequestDone: 
     122                    req.addcomment_raise = True 
    114123            else: 
    115124                the_message = tag.div(tag.strong("ERROR: "), "[[AddComment]] " 
     
    160169        return tag.div(the_preview, the_message, the_form, id="commenting") 
    161170     
     171    # IMacroPoster method 
     172     
    162173    def process_macro_post(self, req): 
    163174        self.log.debug('AddCommentMacro: Got a POST') 
    164175 
     176    # IRequestFilter methods 
     177 
     178    def pre_process_request(self, req, handler): 
     179        return handler 
     180 
     181    def post_process_request(self, req, template, data, content_type): 
     182        if hasattr(req, 'addcomment_raise'): 
     183            self.env.log.debug("AddCommentMacro: Re-raising RequestDone from redirect") 
     184            raise RequestDone 
     185        return template, data, content_type