#5992 closed enhancement (fixed)
[patch] More graceful wiki page validation with req.warning
Reported by: | 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
43 43 from trac.wiki.model import WikiPage 44 44 45 45 46 class InvalidWikiPage(TracError):47 """Exception raised when a Wiki page fails validation."""48 49 50 46 class WikiModule(Component): 51 47 52 48 implements(IContentConverter, INavigationContributor, IPermissionRequestor, … … 130 126 if a in req.args: 131 127 action = a 132 128 break 133 if action == 'edit' and not has_collision: 129 valid = self._validate(context) 130 if action == 'edit' and not has_collision and valid: 134 131 self._do_save(context) 135 132 else: 136 133 return self._render_editor(context, action, has_collision) … … 167 164 168 165 # Internal methods 169 166 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 170 185 def _page_data(self, context, action=''): 171 186 page_name = context.name() 172 187 title = context.summary() … … 253 268 # WIKI_ADMIN 254 269 page.readonly = int('readonly' in req.args) 255 270 256 # Give the manipulators a pass at post-processing the page257 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 267 271 try: 268 272 page.save(get_reporter_id(req, 'author'), req.args.get('comment'), 269 273 req.remote_addr)
Attachments (3)
Change History (9)
by , 17 years ago
Attachment: | wiki_validation_improvement_r5999.patch added |
---|
comment:3 by , 17 years ago
Priority: | normal → high |
---|
by , 17 years ago
Attachment: | wiki_graceful_validation_r7021.patch added |
---|
Here's an updated version of the patch.
by , 17 years ago
Attachment: | wiki_graceful_validation_r7021.2.patch added |
---|
Here's an updated version of the patch (the previous version of this had a small typo).
comment:5 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Applied in r7033.
Only thing modified is that I left the InvalidWikiPage exception in, but marked it deprecated.
comment:6 by , 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!')
Same patch as displayed in the ticket description.