[Checkins] SVN: zc.form/trunk/src/zc/form/ Added use_default_for_not_selected to Union field to use default value even if sub field is not selected.

Michael Howitz mh+zope at gocept.com
Thu May 3 04:51:04 EDT 2007


Log message for revision 75044:
  Added use_default_for_not_selected to Union field to use default value even if sub field is not selected.
  

Changed:
  A   zc.form/trunk/src/zc/form/CHANGES.txt
  U   zc.form/trunk/src/zc/form/browser/tests.py
  U   zc.form/trunk/src/zc/form/browser/unionwidget.py
  U   zc.form/trunk/src/zc/form/field.py
  U   zc.form/trunk/src/zc/form/interfaces.py

-=-
Added: zc.form/trunk/src/zc/form/CHANGES.txt
===================================================================
--- zc.form/trunk/src/zc/form/CHANGES.txt	2007-05-03 08:50:24 UTC (rev 75043)
+++ zc.form/trunk/src/zc/form/CHANGES.txt	2007-05-03 08:51:03 UTC (rev 75044)
@@ -0,0 +1,9 @@
+======================================================================
+CHANGES in zc.form
+======================================================================
+
+2007-05-03
+----------
+
+- Added use_default_for_not_selected to Union field to use default
+  value even if sub field is not selected.


Property changes on: zc.form/trunk/src/zc/form/CHANGES.txt
___________________________________________________________________
Name: svn:keywords
   + Id Rev Date
Name: svn:eol-style
   + native

Modified: zc.form/trunk/src/zc/form/browser/tests.py
===================================================================
--- zc.form/trunk/src/zc/form/browser/tests.py	2007-05-03 08:50:24 UTC (rev 75043)
+++ zc.form/trunk/src/zc/form/browser/tests.py	2007-05-03 08:51:03 UTC (rev 75044)
@@ -36,6 +36,7 @@
 import zope.app.security
 import zc.form.browser
 from zc.form.field import Union
+import zc.form.field 
 from zc.form.browser.unionwidget import UnionWidget
 from zope.testing.doctestunit import pprint
 
@@ -104,6 +105,40 @@
         self.failUnless(re.search(
             '''checked\s*=\s*['"]checked['"]''', output))
 
+    def test_use_default_for_not_selected(self):
+        # test use_default_for_not_selected = True
+        request = TestRequest()
+        # the default selection shoud be the the option field which has the
+        # value of None
+        field = Union(
+            (zc.form.field.TextLine(
+                    title=u"New Password", missing_value=u'',
+                    default_getter=lambda x: u'secret password'),
+             zc.form.field.Option(
+                    title=u"No Change", value_getter=lambda x: None)),
+            title=u"Change Password",
+            missing_value=u'',
+            use_default_for_not_selected=True,
+            __name__='identifier')
+        widget = UnionWidget(field, request)
+        widget.setPrefix('field')
+        output = widget()
+        # remove double whitespaces
+        normalized_output = " ".join(output.split())
+        
+        # the value of the textline field should be the default_getter's
+        # result
+        value_attr_of_textline = re.search(
+            '<input.*id="field.identifier.unioned_00".*(value=".*").*></div>',
+            normalized_output).groups()[0]
+        self.failUnless('secret password' in value_attr_of_textline)
+        
+        # the radio button of the option field should be selected
+        radio_option_field = re.search(
+            '<input.*id="field.identifier-01"(.*)/> </td>',
+            normalized_output).groups()[0]
+        self.failUnless('checked="checked"' in radio_option_field)
+
     def test_evaluate(self):
         request = TestRequest()
         request.form.update({

Modified: zc.form/trunk/src/zc/form/browser/unionwidget.py
===================================================================
--- zc.form/trunk/src/zc/form/browser/unionwidget.py	2007-05-03 08:50:24 UTC (rev 75043)
+++ zc.form/trunk/src/zc/form/browser/unionwidget.py	2007-05-03 08:51:03 UTC (rev 75044)
@@ -109,7 +109,10 @@
                 if selected:
                     widget.setRenderedValue(value)
                 elif self._renderedValueSet():
-                    widget.setRenderedValue(inner.missing_value)
+                    if field.use_default_for_not_selected:
+                        widget.setRenderedValue(inner.default)
+                    else:
+                        widget.setRenderedValue(inner.missing_value)
             widget.setPrefix(self.name)
             choices.append(
                 {'selected': selected, 'identifier': identifier,

Modified: zc.form/trunk/src/zc/form/field.py
===================================================================
--- zc.form/trunk/src/zc/form/field.py	2007-05-03 08:50:24 UTC (rev 75043)
+++ zc.form/trunk/src/zc/form/field.py	2007-05-03 08:51:03 UTC (rev 75044)
@@ -232,8 +232,9 @@
     interface.implements(interfaces.IUnionField)
     
     fields = ()
+    use_default_for_not_selected = False
     
-    def __init__(self, fields, **kw):
+    def __init__(self, fields, use_default_for_not_selected=False, **kw):
         if len(fields) < 2:
             raise ValueError(_("union must combine two or more fields"))
         for ix, field in enumerate(fields):
@@ -241,6 +242,7 @@
                 raise DoesNotImplement(IField)
             field.__name__ = "unioned_%02d" % ix
         self.fields = tuple(fields)
+        self.use_default_for_not_selected = use_default_for_not_selected
         super(Union, self).__init__(**kw)
 
     def bind(self, object):

Modified: zc.form/trunk/src/zc/form/interfaces.py
===================================================================
--- zc.form/trunk/src/zc/form/interfaces.py	2007-05-03 08:50:24 UTC (rev 75043)
+++ zc.form/trunk/src/zc/form/interfaces.py	2007-05-03 08:51:03 UTC (rev 75044)
@@ -40,7 +40,11 @@
         otherwise returns whatever value has been set as the default.""")
 
 class IOptionField(IExtendedField):
+    """Field with excatly one predefined value
 
+    Caution: The value will not get displayed by the widget of this field.
+    """
+
     value = interface.Attribute(
         """the value for this field; one and only one of value and
         value_getter must be non-None""")
@@ -65,6 +69,17 @@
         title=_("Composite Fields"),
         description=_("""\
             The possible schema fields that may describe the data"""))
+
+    use_default_for_not_selected = schema.Bool(
+        description=_(
+            """When displaying the Union field in the browser the fields
+            which are not selected will have no value (i. e. the field's
+            missing_value.
+
+            With this attribute set the default value of the field will
+            be displayed instead.
+
+            Default: False"""))
     
     def validField(value):
         u"returns first valid field for the given value, or None"



More information about the Checkins mailing list