Changeset 4183

Show
Ignore:
Timestamp:
03/01/08 05:51:02 (5 months ago)
Author:
chrisz
Message:

Improved fix for ticket #1705.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.0/turbogears/tests/test_validators.py

    r3366 r4183  
    2727    assert date == dt.to_python(date), "Accepts datetime OK" 
    2828    assert date == dt.to_python(dt.from_python(date)), "Good datetime passes validation" 
    29  
    3029    try: 
    3130        dt.to_python("foo") 
     
    3837    origlist = ["Foo", "Bar", "Baz"] 
    3938    json = v.from_python(origlist) 
    40     print json 
    4139    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 
    4542 
    4643def test_unicodestring_validator(): 
     
    5653    try: 
    5754        # 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')) 
    5956        assert 0, 'malformed data not detected' 
    6057    except validators.Invalid: 
     
    6562    # test for ticket #955 
    6663    assert validators.Number.to_python(45) == 45 
     64 
     65def 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  
    2222from formencode import validators # to disambiguate the Number validator... 
    2323 
    24 # this import is needed for our custom FieldStorageUploadConverter 
    25 import cgi 
    2624 
    2725def _(s): return s # dummy 
     
    137135            return value 
    138136 
    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 
    141141class 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        } 
    145146 
    146147    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) 
    152154        return value 
    153155 
  • branches/1.1/turbogears/tests/test_validators.py

    r3367 r4183  
    1414    assert b.to_python("") is False 
    1515    assert b.to_python(None) is False 
    16  
    1716    try: 
    1817        b.to_python("foobar") 
     
    2726    assert date == dt.to_python(date), "Accepts datetime OK" 
    2827    assert date == dt.to_python(dt.from_python(date)), "Good datetime passes validation" 
    29  
    3028    try: 
    3129        dt.to_python("foo") 
     
    3836    origlist = ["Foo", "Bar", "Baz"] 
    3937    json = v.from_python(origlist) 
    40     print json 
    4138    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 
    4541 
    4642def test_unicodestring_validator(): 
     
    5652    try: 
    5753        # 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')) 
    5955        assert 0, 'malformed data not detected' 
    6056    except validators.Invalid: 
     
    6561    # test for ticket #955 
    6662    assert validators.Number.to_python(45) == 45 
     63 
     64def 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  
    2222from formencode import validators # to disambiguate the Number validator... 
    2323 
    24 # this import is needed for our custom FieldStorageUploadConverter 
    25 import cgi 
    2624 
    2725def _(s): return s # dummy 
     
    137135            return value 
    138136 
    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 
    141141class 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        } 
    145146 
    146147    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) 
    152154        return value 
     155 
    153156 
    154157# For translated messages that are not wrapped in a Validator.messages