Mercurial > p > roundup > code
changeset 1562:b975da59cd11
handle invalid data input in forms better
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Wed, 26 Mar 2003 06:46:17 +0000 |
| parents | cf5a5357a7ea |
| children | e2a8ce4d2317 |
| files | roundup/cgi/client.py test/test_cgi.py |
| diffstat | 2 files changed, 42 insertions(+), 31 deletions(-) [+] |
line wrap: on
line diff
--- a/roundup/cgi/client.py Wed Mar 26 06:36:11 2003 +0000 +++ b/roundup/cgi/client.py Wed Mar 26 06:46:17 2003 +0000 @@ -1,4 +1,4 @@ -# $Id: client.py,v 1.110 2003-03-26 03:35:00 richard Exp $ +# $Id: client.py,v 1.111 2003-03-26 06:46:17 richard Exp $ __doc__ = """ WWW request handler (also used in the stand-alone server). @@ -1732,36 +1732,41 @@ # other types should be None'd if there's no value value = None else: - if isinstance(proptype, hyperdb.String): - if (hasattr(value, 'filename') and - value.filename is not None): - # skip if the upload is empty - if not value.filename: - continue - # this String is actually a _file_ - # try to determine the file content-type - filename = value.filename.split('\\')[-1] - if propdef.has_key('name'): - props['name'] = filename - # use this info as the type/filename properties - if propdef.has_key('type'): - props['type'] = mimetypes.guess_type(filename)[0] - if not props['type']: - props['type'] = "application/octet-stream" - # finally, read the content - value = value.value - else: - # normal String fix the CRLF/CR -> LF stuff - value = fixNewlines(value) + # handle ValueErrors for all these in a similar fashion + try: + if isinstance(proptype, hyperdb.String): + if (hasattr(value, 'filename') and + value.filename is not None): + # skip if the upload is empty + if not value.filename: + continue + # this String is actually a _file_ + # try to determine the file content-type + fn = value.filename.split('\\')[-1] + if propdef.has_key('name'): + props['name'] = fn + # use this info as the type/filename properties + if propdef.has_key('type'): + props['type'] = mimetypes.guess_type(fn)[0] + if not props['type']: + props['type'] = "application/octet-stream" + # finally, read the content + value = value.value + else: + # normal String fix the CRLF/CR -> LF stuff + value = fixNewlines(value) - elif isinstance(proptype, hyperdb.Date): - value = date.Date(value, offset=timezone) - elif isinstance(proptype, hyperdb.Interval): - value = date.Interval(value) - elif isinstance(proptype, hyperdb.Boolean): - value = value.lower() in ('yes', 'true', 'on', '1') - elif isinstance(proptype, hyperdb.Number): - value = float(value) + elif isinstance(proptype, hyperdb.Date): + value = date.Date(value, offset=timezone) + elif isinstance(proptype, hyperdb.Interval): + value = date.Interval(value) + elif isinstance(proptype, hyperdb.Boolean): + value = value.lower() in ('yes', 'true', 'on', '1') + elif isinstance(proptype, hyperdb.Number): + value = float(value) + except ValueError, msg: + raise ValueError, _('Error with %s property: %s')%( + propname, msg) # get the old value if nodeid and not nodeid.startswith('-'):
--- a/test/test_cgi.py Wed Mar 26 06:36:11 2003 +0000 +++ b/test/test_cgi.py Wed Mar 26 06:46:17 2003 +0000 @@ -8,7 +8,7 @@ # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # -# $Id: test_cgi.py,v 1.13 2003-03-18 00:37:25 richard Exp $ +# $Id: test_cgi.py,v 1.14 2003-03-26 06:46:17 richard Exp $ import unittest, os, shutil, errno, sys, difflib, cgi, re @@ -385,6 +385,9 @@ ({('test', None): {}}, [])) self.assertRaises(ValueError, self.parseForm, {'number': ['', '']}) + def testInvalidNumber(self): + self.assertRaises(ValueError, self.parseForm, {'number': 'hi, mum!'}) + def testSetNumber(self): self.assertEqual(self.parseForm({'number': '1'}), ({('test', None): {'number': 1}}, [])) @@ -415,6 +418,9 @@ ({('test', None): {}}, [])) self.assertRaises(ValueError, self.parseForm, {'date': ['', '']}) + def testInvalidDate(self): + self.assertRaises(ValueError, self.parseForm, {'date': '12'}) + def testSetDate(self): self.assertEqual(self.parseForm({'date': '2003-01-01'}), ({('test', None): {'date': date.Date('2003-01-01')}}, []))
