Show
Ignore:
Timestamp:
05/27/08 14:20:18 (8 months ago)
Author:
osimons
Message:

AddCommentMacro: Now calls wiki page manipulators to check content before saving. This means support for Spam Filter plugin and similar plugins that can veto any wiki page change.

Report and patch by Thomas Moschny. Closes #3076.

Files:

Legend:

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

    r3027 r3723  
    1414from trac.wiki.macros import WikiMacroBase 
    1515from trac.wiki.model import WikiPage 
     16from trac.wiki.web_ui import WikiModule 
    1617 
    1718from macropost.api import IMacroPoster 
     
    112113            if submitted: 
    113114                page.text = newtext 
    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. 
     115                 
     116                # Let the wiki page manipulators have a look at the 
     117                # submission. 
     118                valid = True 
     119                req.args.setdefault('comment', 'Comment added.') 
    118120                try: 
    119                     req._outheaders = [] 
    120                     req.redirect(page_url) 
    121                 except RequestDone: 
    122                     req.addcomment_raise = True 
     121                    for manipulator in WikiModule(self.env).page_manipulators: 
     122                        for field, message in manipulator.validate_wiki_page(req, page): 
     123                            valid = False 
     124                            if field: 
     125                                the_message += tag.div(tag.strong("invalid field '%s': " % field), 
     126                                                       message, 
     127                                                       class_="system-message") 
     128                            else: 
     129                                the_message += tag.div(tag.strong("invalid: "), 
     130                                                       message, 
     131                                                       class_="system-message") 
     132 
     133                # The TracSpamfilterPlugin does not generate messages, 
     134                # but throws RejectContent. 
     135                except TracError, s: 
     136                    valid = False 
     137                    the_message += tag.div(tag.strong("ERROR: "), s, class_="system-message") 
     138 
     139                if valid:         
     140                    page.save(authname, req.args['comment'], req.environ['REMOTE_ADDR']) 
     141                    # We can't redirect from macro as it will raise RequestDone 
     142                    # which like other macro errors gets swallowed in the Formatter. 
     143                    # We need to re-raise it in a post_process_request instead. 
     144                    try: 
     145                        req._outheaders = [] 
     146                        req.redirect(page_url) 
     147                    except RequestDone: 
     148                        req.addcomment_raise = True 
    123149            else: 
    124150                the_message = tag.div(tag.strong("ERROR: "), "[[AddComment]] "