[Checkins] SVN: z3c.form/trunk/ - Bug: When dealing with `Bytes` fields, we should do a null conversion when

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Sep 29 17:32:00 EDT 2008


Log message for revision 91631:
  - Bug: When dealing with `Bytes` fields, we should do a null conversion when
    going to its widget value.
  

Changed:
  U   z3c.form/trunk/CHANGES.txt
  _U  z3c.form/trunk/benchmark/
  U   z3c.form/trunk/src/z3c/form/converter.py
  U   z3c.form/trunk/src/z3c/form/converter.txt

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2008-09-29 21:22:18 UTC (rev 91630)
+++ z3c.form/trunk/CHANGES.txt	2008-09-29 21:31:59 UTC (rev 91631)
@@ -45,6 +45,9 @@
   user interfaces.  The widget can be used for sequence fields (e.g. `IList`)
   that specify a simple value type field (e.g. `ITextLine` or `IInt`).
 
+- Bug: When dealing with `Bytes` fields, we should do a null conversion when
+  going to its widget value.
+
 - Bug: Use ``nocall:`` modifier in `orderedselect_input.pt` to avoid calling
   list entry if it is callable.
 


Property changes on: z3c.form/trunk/benchmark
___________________________________________________________________
Name: svn:ignore
   + benchmark.egg-info


Modified: z3c.form/trunk/src/z3c/form/converter.py
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.py	2008-09-29 21:22:18 UTC (rev 91630)
+++ z3c.form/trunk/src/z3c/form/converter.py	2008-09-29 21:31:59 UTC (rev 91631)
@@ -212,6 +212,10 @@
     zope.component.adapts(
         zope.schema.interfaces.IBytes, interfaces.IFileWidget)
 
+    def toWidgetValue(self, value):
+        """See interfaces.IDataConverter"""
+        return value
+
     def toFieldValue(self, value):
         """See interfaces.IDataConverter"""
         if value is None or value == '':

Modified: z3c.form/trunk/src/z3c/form/converter.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.txt	2008-09-29 21:22:18 UTC (rev 91630)
+++ z3c.form/trunk/src/z3c/form/converter.txt	2008-09-29 21:31:59 UTC (rev 91631)
@@ -384,8 +384,8 @@
 File Upload Data Converter
 --------------------------
 
-Since the ``Bytes`` field can contain a FileUpload object, we have to make
-sure we can convert FileUpload objects to bytes too.
+Since the ``Bytes`` field can contain a ``FileUpload`` object, we have to make
+sure we can convert ``FileUpload`` objects to bytes too.
 
   >>> import z3c.form.browser.file
   >>> fileWidget = z3c.form.browser.file.FileWidget(TestRequest())
@@ -395,14 +395,15 @@
   >>> fudc
   <FileUploadDataConverter converts from Bytes to FileWidget>
 
-Bytes are converted to unicode:
+The file upload widget usually provides a file object. But sometimes is also
+provides a string:
 
   >>> simple = 'foobar'
   >>> fudc.toFieldValue(simple)
   'foobar'
 
-The converter can also convert FileUpload objects. Setup a fields storage
-stub...
+The converter can also convert ``FileUpload`` objects. So we need to setup a
+fields storage stub ...
 
   >>> class FieldStorageStub:
   ...     def __init__(self, file):
@@ -410,7 +411,7 @@
   ...         self.headers = {}
   ...         self.filename = 'foo.bar'
 
-build a FileUpload...
+and a ``FileUpload`` component:
 
   >>> import cStringIO
   >>> from zope.publisher.browser import FileUpload
@@ -418,14 +419,14 @@
   >>> aFieldStorage = FieldStorageStub(myfile)
   >>> myUpload = FileUpload(aFieldStorage)
 
-and try to convert:
+Let's try to convert the input now:
 
   >>> fudc.toFieldValue(myUpload)
   'File upload contents.'
 
-By default the converter converts missing input to missin_input value:
+By default the converter converts missing input to ``missing_input`` value:
 
-  >>> fudc.toFieldValue(u'') is None
+  >>> fudc.toFieldValue('') is None
   True
 
 If we get a emtpy filename for a fileupload, we also get the missing_value,
@@ -446,7 +447,7 @@
   True
 
 There is also a ValueError if we don't get a seekable file from the
-FieldStorage during the upload:
+``FieldStorage`` during the upload:
 
   >>> myfile = ''
   >>> aFieldStorage = FieldStorageStub(myfile)
@@ -458,6 +459,12 @@
   ...
   ValueError: (u'Bytes data are not a file object', ...AttributeError...)
 
+When converting to the widget value, not conversion should be done, since
+bytes are not convertable in that sense.
+
+  >>> fudc.toWidgetValue('bytes')
+  'bytes'
+
 When the file upload widget is not used and a text-based widget is desired,
 then the regular field data converter will be chosen. Using a text widget,
 however, must be setup manually in the form with code like this::



More information about the Checkins mailing list