Changeset 6278 for main/waeup.sirp/trunk/src/waeup/sirp/utils
- Timestamp:
- 4 Jun 2011, 12:23:23 (13 years ago)
- Location:
- main/waeup.sirp/trunk/src/waeup/sirp/utils
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/converters.py
r6276 r6278 4 4 from zope.component import createObject 5 5 from zope.formlib import form 6 from zope.formlib.boolwidgets import CheckBoxWidget 6 7 from zope.formlib.form import ( 7 8 _widgetKey, WidgetInputError, ValidationError, InputErrors, expandPrefix) 8 from zope.formlib.interfaces import IInputWidget 9 from zope.formlib.interfaces import IInputWidget, ISimpleInputWidget 9 10 from zope.interface import Interface 10 11 from zope.publisher.browser import TestRequest 11 12 from waeup.sirp.interfaces import IObjectConverter 13 14 class ExtendedCheckBoxWidget(CheckBoxWidget): 15 """A checkbox widget that supports more input values as True/False 16 markers. 17 18 The default bool widget expects the string 'on' as only valid 19 ``True`` value in HTML forms for bool fields. 20 21 This widget also accepts '1', 'true' and 'yes' for that. Also all 22 uppercase/lowecase combinations of these strings are accepted. 23 24 The widget still renders ``True`` to ``'on'`` when a form is 25 generated. 26 """ 27 true_markers = ['1', 'true', 'on', 'yes'] 28 29 def _toFieldValue(self, input): 30 """Convert from HTML presentation to Python bool.""" 31 if not isinstance(input, basestring): 32 return False 33 return input.lower() in self.true_markers 34 35 def _getFormInput(self): 36 """Returns the form input used by `_toFieldValue`. 37 38 Return values: 39 40 ``'on'`` checkbox is checked 41 ``''`` checkbox is not checked 42 ``None`` form input was not provided 43 44 """ 45 value = self.request.get(self.name) 46 if isinstance(value, basestring): 47 value = value.lower() 48 if value in self.true_markers: 49 return 'on' 50 elif self.name + '.used' in self.request: 51 return '' 52 else: 53 return None 12 54 13 55 def getWidgetsData(widgets, form_prefix, data): … … 48 90 49 91 return errors 92 50 93 51 94 class DefaultObjectConverter(grok.Adapter): -
main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_converters.py
r6273 r6278 171 171 assert data2['vip'] is False 172 172 173 def test_bool_nonstandard_values1(self): 174 # We accept 'true', 'True', 'tRuE', 'faLSE' and similar. 175 contact1 = Contact() 176 contact2 = Contact() 177 input_data1 = dict(vip='True') 178 input_data2 = dict(vip='false') 179 converter = IObjectConverter(IContact) # a converter to IContact 180 err1, inv_err1, data1 = converter.fromStringDict( 181 input_data1, contact1) 182 err2, inv_err2, data2 = converter.fromStringDict( 183 input_data2, contact2) 184 assert data1['vip'] is True 185 assert data2['vip'] is False 186 187 def test_bool_nonstandard_values2(self): 188 # We accept '1' and '0' as bool values. 189 contact1 = Contact() 190 contact2 = Contact() 191 input_data1 = dict(vip='1') 192 input_data2 = dict(vip='0') 193 converter = IObjectConverter(IContact) # a converter to IContact 194 err1, inv_err1, data1 = converter.fromStringDict( 195 input_data1, contact1) 196 err2, inv_err2, data2 = converter.fromStringDict( 197 input_data2, contact2) 198 assert data1['vip'] is True 199 assert data2['vip'] is False 200 201 def test_bool_nonstandard_values3(self): 202 # We accept 'yEs', 'no' and similar as bool values. 203 contact1 = Contact() 204 contact2 = Contact() 205 input_data1 = dict(vip='Yes') 206 input_data2 = dict(vip='no') 207 converter = IObjectConverter(IContact) # a converter to IContact 208 err1, inv_err1, data1 = converter.fromStringDict( 209 input_data1, contact1) 210 err2, inv_err2, data2 = converter.fromStringDict( 211 input_data2, contact2) 212 assert data1['vip'] is True 213 assert data2['vip'] is False 214 173 215 def test_int(self): 174 216 contact = Contact()
Note: See TracChangeset for help on using the changeset viewer.