Changeset 4183
- Timestamp:
- 03/01/08 05:51:02 (5 months ago)
- Files:
-
- branches/1.0/turbogears/tests/test_validators.py (modified) (4 diffs)
- branches/1.0/turbogears/validators.py (modified) (2 diffs)
- branches/1.1/turbogears/tests/test_validators.py (modified) (5 diffs)
- branches/1.1/turbogears/validators.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.0/turbogears/tests/test_validators.py
r3366 r4183 27 27 assert date == dt.to_python(date), "Accepts datetime OK" 28 28 assert date == dt.to_python(dt.from_python(date)), "Good datetime passes validation" 29 30 29 try: 31 30 dt.to_python("foo") … … 38 37 origlist = ["Foo", "Bar", "Baz"] 39 38 json = v.from_python(origlist) 40 print json41 39 assert json == '["Foo", "Bar", "Baz"]' 42 l = v.to_python(json) 43 print l 44 assert origlist == l 40 jsonlist = v.to_python(json) 41 assert origlist == jsonlist 45 42 46 43 def test_unicodestring_validator(): … … 56 53 try: 57 54 # feed cp1251 encoded data when utf8 is expected 58 print `v.to_python('\xf0\xf3\xeb\xe8\xf2')`55 print repr(v.to_python('\xf0\xf3\xeb\xe8\xf2')) 59 56 assert 0, 'malformed data not detected' 60 57 except validators.Invalid: … … 65 62 # test for ticket #955 66 63 assert validators.Number.to_python(45) == 45 64 65 def test_empty_field_storage(): 66 from cgi import FieldStorage 67 empty_field_storage = FieldStorage() 68 try: 69 v = validators.FieldStorageUploadConverter( 70 not_empty=True).to_python(empty_field_storage) 71 except validators.Invalid: 72 v = 'invalid' 73 assert v == 'invalid', 'mandatory filename not ensured' 74 # test for ticket #1705 75 try: 76 v = validators.FieldStorageUploadConverter( 77 not_empty=False).to_python(empty_field_storage) 78 except validators.Invalid: 79 v = 'invalid' 80 assert v is None, 'optional filename not validated' branches/1.0/turbogears/validators.py
r4155 r4183 22 22 from formencode import validators # to disambiguate the Number validator... 23 23 24 # this import is needed for our custom FieldStorageUploadConverter25 import cgi26 24 27 25 def _(s): return s # dummy … … 137 135 return value 138 136 139 # see #1464357 on FE bugtracker (http://tinyurl.com/lm9ae). 140 # Custom version of FieldStorage validator that does not break FE schema validator. 137 138 # Improved FieldStorageUploadConverter heeding not_empty=False 139 # (see TurboGears ticket #1705, FormEncode bug #1905250) 140 141 141 class FieldStorageUploadConverter(TgFancyValidator): 142 def __init__(self, *args, **kwargs): 143 super(TgFancyValidator, self).__init__(*args, **kwargs) 144 self.not_empty = kwargs.get('not_empty') 142 143 messages = { 144 'notEmpty': _("Filename must not be empty"), 145 } 145 146 146 147 def _to_python(self, value, state=None): 147 if isinstance(value, cgi.FieldStorage): 148 if value.filename: 149 return value 150 if self.not_empty: 151 raise Invalid('invalid', value, state) 148 try: 149 value = value.filename 150 except AttributeError: 151 value = None 152 if not value and self.not_empty: 153 raise Invalid(self.message('notEmpty', state), value, state) 152 154 return value 153 155 branches/1.1/turbogears/tests/test_validators.py
r3367 r4183 14 14 assert b.to_python("") is False 15 15 assert b.to_python(None) is False 16 17 16 try: 18 17 b.to_python("foobar") … … 27 26 assert date == dt.to_python(date), "Accepts datetime OK" 28 27 assert date == dt.to_python(dt.from_python(date)), "Good datetime passes validation" 29 30 28 try: 31 29 dt.to_python("foo") … … 38 36 origlist = ["Foo", "Bar", "Baz"] 39 37 json = v.from_python(origlist) 40 print json41 38 assert json == '["Foo", "Bar", "Baz"]' 42 l = v.to_python(json) 43 print l 44 assert origlist == l 39 jsonlist = v.to_python(json) 40 assert origlist == jsonlist 45 41 46 42 def test_unicodestring_validator(): … … 56 52 try: 57 53 # feed cp1251 encoded data when utf8 is expected 58 print `v.to_python('\xf0\xf3\xeb\xe8\xf2')`54 print repr(v.to_python('\xf0\xf3\xeb\xe8\xf2')) 59 55 assert 0, 'malformed data not detected' 60 56 except validators.Invalid: … … 65 61 # test for ticket #955 66 62 assert validators.Number.to_python(45) == 45 63 64 def test_empty_field_storage(): 65 from cgi import FieldStorage 66 empty_field_storage = FieldStorage() 67 try: 68 v = validators.FieldStorageUploadConverter( 69 not_empty=True).to_python(empty_field_storage) 70 except validators.Invalid: 71 v = 'invalid' 72 assert v == 'invalid', 'mandatory filename not ensured' 73 # test for ticket #1705 74 try: 75 v = validators.FieldStorageUploadConverter( 76 not_empty=False).to_python(empty_field_storage) 77 except validators.Invalid: 78 v = 'invalid' 79 assert v is None, 'optional filename not validated' branches/1.1/turbogears/validators.py
r4156 r4183 22 22 from formencode import validators # to disambiguate the Number validator... 23 23 24 # this import is needed for our custom FieldStorageUploadConverter25 import cgi26 24 27 25 def _(s): return s # dummy … … 137 135 return value 138 136 139 # see #1464357 on FE bugtracker (http://tinyurl.com/lm9ae). 140 # Custom version of FieldStorage validator that does not break FE schema validator. 137 138 # Improved FieldStorageUploadConverter heeding not_empty=False 139 # (see TurboGears ticket #1705, FormEncode bug #1905250) 140 141 141 class FieldStorageUploadConverter(TgFancyValidator): 142 def __init__(self, *args, **kwargs): 143 super(TgFancyValidator, self).__init__(*args, **kwargs) 144 self.not_empty = kwargs.get('not_empty') 142 143 messages = { 144 'notEmpty': _("Filename must not be empty"), 145 } 145 146 146 147 def _to_python(self, value, state=None): 147 if isinstance(value, cgi.FieldStorage): 148 if value.filename: 149 return value 150 if self.not_empty: 151 raise Invalid('invalid', value, state) 148 try: 149 value = value.filename 150 except AttributeError: 151 value = None 152 if not value and self.not_empty: 153 raise Invalid(self.message('notEmpty', state), value, state) 152 154 return value 155 153 156 154 157 # For translated messages that are not wrapped in a Validator.messages