Edgewall Software
Modify

Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#5992 closed enhancement (fixed)

[patch] More graceful wiki page validation with req.warning

Reported by: hyuga <hyugaricdeau@…> Owned by: Christian Boos
Priority: high Milestone: 0.11
Component: wiki system Version: devel
Severity: normal Keywords: wiki, validation
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description

This does away with the InvalidWikiPage exception entirely, and instead of raising an error upon finding the first invalid field, all page_manipulators are allowed to perform validation, and add warnings to req.warning. This was all warnings are displayed, and can also be displayed during preview, diff, and when trying to submit:

  • trac/wiki/web_ui.py

     
    4343from trac.wiki.model import WikiPage
    4444
    4545
    46 class InvalidWikiPage(TracError):
    47     """Exception raised when a Wiki page fails validation."""
    48 
    49 
    5046class WikiModule(Component):
    5147
    5248    implements(IContentConverter, INavigationContributor, IPermissionRequestor,
     
    130126                    if a in req.args:
    131127                        action = a
    132128                        break
    133                 if action == 'edit' and not has_collision:
     129                valid = self._validate(context)
     130                if action == 'edit' and not has_collision and valid:
    134131                    self._do_save(context)
    135132                else:
    136133                    return self._render_editor(context, action, has_collision)
     
    167164
    168165    # Internal methods
    169166
     167    def _validate(self, context):
     168        req = context.req
     169        page = context.resource
     170       
     171        valid = True
     172        # Give the manipulators a pass at post-processing the page
     173        for manipulator in self.page_manipulators:
     174            for field, message in manipulator.validate_wiki_page(req, page):
     175                valid = False
     176                if field:
     177                    req.warning(_("The Wiki page field '%(field)s' is "
     178                                  "invalid: %(message)s",
     179                                  field=field, message=message))
     180                else:
     181                    req.warning(_("Invalid Wiki page: %(message)s",
     182                                  message=message))
     183        return valid
     184
    170185    def _page_data(self, context, action=''):
    171186        page_name = context.name()
    172187        title = context.summary()
     
    253268            # WIKI_ADMIN
    254269            page.readonly = int('readonly' in req.args)
    255270
    256         # Give the manipulators a pass at post-processing the page
    257         for manipulator in self.page_manipulators:
    258             for field, message in manipulator.validate_wiki_page(req, page):
    259                 if field:
    260                     raise InvalidWikiPage(_("The Wiki page field '%(field)s' "
    261                                             "is invalid: %(message)s",
    262                                             field=field, message=message))
    263                 else:
    264                     raise InvalidWikiPage(_("Invalid Wiki page: %(message)s",
    265                                             message=message))
    266 
    267271        try:
    268272            page.save(get_reporter_id(req, 'author'), req.args.get('comment'),
    269273                      req.remote_addr)

Attachments (3)

wiki_validation_improvement_r5999.patch (2.6 KB ) - added by hyuga <hyugaricdeau@…> 17 years ago.
Same patch as displayed in the ticket description.
wiki_graceful_validation_r7021.patch (3.1 KB ) - added by ebray <hyugaricdeau@…> 17 years ago.
Here's an updated version of the patch.
wiki_graceful_validation_r7021.2.patch (3.1 KB ) - added by ebray <hyugaricdeau@…> 17 years ago.
Here's an updated version of the patch (the previous version of this had a small typo).

Download all attachments as: .zip

Change History (9)

by hyuga <hyugaricdeau@…>, 17 years ago

Same patch as displayed in the ticket description.

comment:1 by ThurnerRupert, 17 years ago

Milestone: 0.11

could be applied immediately, no?

comment:2 by ThurnerRupert, 17 years ago

Milestone: 0.110.11.1

i mean immediately after release :)

comment:3 by Christian Boos, 17 years ago

Priority: normalhigh

Looks like this was a missing bit of #152 / #4100 related functionality, so it would be nice to get in 0.11 itself for consistency.

ebray, can you please look into updating the patch?

by ebray <hyugaricdeau@…>, 17 years ago

Here's an updated version of the patch.

by ebray <hyugaricdeau@…>, 17 years ago

Here's an updated version of the patch (the previous version of this had a small typo).

comment:4 by Christian Boos, 17 years ago

Milestone: 0.11.10.11

Thanks for the updated patch!

comment:5 by Christian Boos, 17 years ago

Resolution: fixed
Status: newclosed

Applied in r7033.

Only thing modified is that I left the InvalidWikiPage exception in, but marked it deprecated.

comment:6 by Christian Boos, 17 years ago

Oh, by the way, for testing the change I had to write a little IWikiPageManipulator plugin, and as I think it can be universally used, I'll donate it to the world:

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

class NeedSpam(Component):
    """Enforce feeding the wiki comment with SPAM!"""

    implements(IWikiPageManipulator)

    # IWikiPageManipulator methods

    def prepare_wiki_page(self, req, page, fields):
        pass

    def validate_wiki_page(self, req, page):
        if 'spam' not in req.args.get('comment', ''):
            yield ('comment', 'No SPAM provided!')

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Christian Boos.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Christian Boos to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.