trac_forcecomment / forcecomment / forcecomment.py

edgimar 77d9fc5 
















edgimar 475af23 











edgimar 77d9fc5 









edgimar b29dd0f 
edgimar 77d9fc5 



















edgimar 475af23 







edgimar 77d9fc5 



edgimar 475af23 
edgimar 77d9fc5 

edgimar 475af23 
edgimar 77d9fc5 
# Copyright 2009 Mark Edgington
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""  ForceComment - forces the user to provide a comment for wiki changes

To customize, add the following section to your trac.ini file, and any of
the following settings (defaults are used otherwise):

[forcecomment]
min_comment_length = 10
cosmetic_change_comment = "cosmetic"
error_text = "Sorry, but a comment is required."

The 'cosmetic_change_comment' is a string or phrase which can be less than
the minimum length, but will be accepted, since it indicates that nothing
comment-worthy was changed.

"""
from trac.core import *
from trac.wiki.api import IWikiPageManipulator

__author__ = 'Mark Edgington'
__author_email__ = 'edgimar@gmail.com'

class ForceCommentPlugin(Component):
    implements(IWikiPageManipulator)

    def prepare_wiki_page(self, req, page, fields):
        """Not currently called, but should be provided for future
        compatibility."""
        pass

    def validate_wiki_page(self, req, page):
        """Validate a wiki page after it's been populated from user input.
       
        Must return a list of `(field, message)` tuples, one for each problem
        detected. `field` can be `None` to indicate an overall problem with the
        page. Therefore, a return value of `[]` means everything is OK.
        
        """
        #if req.perm.has_permission('WIKI_ADMIN'):
        #    # An administrator is allowed to have an empty comment
        #    return []

        if ('preview' in req.args) or ('diff' in req.args):
            # Only a preview or diff, no need to filter the submission yet
            return []

        # get the config options set in the trac config file
        c = lambda key, default: self.env.config.get('forcecomment', key, default)
        minLen = int(c('min_comment_length', '10'))
        cosmeticString = c('cosmetic_change_comment', 'cosmetic')
        errTextDefault = "A full comment must be supplied for this change," + \
          " or use '" + cosmeticString + "' if no actual informational content has changed."
        errorText = c('error_text', errTextDefault)

        #cur_page = WikiPage(self.env, name=page.name, version=page.version)
        #author = req.args.get('author', req.authname)
        comment = req.args['comment']

        if (comment.lower() == cosmeticString) or len(comment) >= minLen:
            return []
        else:
            return [(None, errorText)]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.