Changeset 6278 for main/waeup.sirp/trunk


Ignore:
Timestamp:
4 Jun 2011, 12:23:23 (13 years ago)
Author:
uli
Message:

Add extended bools widget. This one accepts also '1', 'true', 'yes' as True values, which is handy when importing CSV
files. We have to register this new widget overriding the default. Maybe a bit overkill?

Location:
main/waeup.sirp/trunk/src/waeup/sirp
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/configure.zcml

    r6138 r6278  
    55  <includeDependencies package="." />
    66  <grok:grok package="." />
     7  <includeOverrides package="waeup.sirp.utils" file="overrides.zcml" />
    78</configure>
  • main/waeup.sirp/trunk/src/waeup/sirp/ftesting.zcml

    r5499 r6278  
    66
    77  <include package="grok" />
    8   <include package="waeup.sirp" />
     8  <includeOverrides package="waeup.sirp" />
    99
    1010  <!-- Typical functional testing security setup -->
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/converters.py

    r6276 r6278  
    44from zope.component import createObject
    55from zope.formlib import form
     6from zope.formlib.boolwidgets import CheckBoxWidget
    67from zope.formlib.form import (
    78    _widgetKey, WidgetInputError, ValidationError, InputErrors, expandPrefix)
    8 from zope.formlib.interfaces import IInputWidget
     9from zope.formlib.interfaces import IInputWidget, ISimpleInputWidget
    910from zope.interface import Interface
    1011from zope.publisher.browser import TestRequest
    1112from waeup.sirp.interfaces import IObjectConverter
     13
     14class 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
    1254
    1355def getWidgetsData(widgets, form_prefix, data):
     
    4890
    4991    return errors
     92
    5093
    5194class DefaultObjectConverter(grok.Adapter):
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_converters.py

    r6273 r6278  
    171171        assert data2['vip'] is False
    172172
     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
    173215    def test_int(self):
    174216        contact = Contact()
Note: See TracChangeset for help on using the changeset viewer.