[Checkins] SVN: z3c.form/trunk/src/z3c/form/converter. Fix a bug with TextLinesConverter when schema fields defines their types as tuple.

Dan Korostelev nadako at gmail.com
Mon Feb 9 03:12:19 EST 2009


Log message for revision 96294:
  Fix a bug with TextLinesConverter when schema fields defines their types as tuple.

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

-=-
Modified: z3c.form/trunk/src/z3c/form/converter.py
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.py	2009-02-08 23:44:06 UTC (rev 96293)
+++ z3c.form/trunk/src/z3c/form/converter.py	2009-02-09 08:12:18 UTC (rev 96294)
@@ -318,17 +318,20 @@
         # if the value is the missing value, then an empty list is produced.
         if value is self.field.missing_value:
             return u''
-        return "\n".join(value)
+        return u'\n'.join(unicode(v) for v in value)
 
     def toFieldValue(self, value):
         """See interfaces.IDataConverter"""
         widget = self.widget
         collectionType = self.field._type
+        if isinstance(collectionType, tuple):
+            collectionType = collectionType[-1]
         if not len(value):
             return self.field.missing_value
         valueType = self.field.value_type._type
-        values = [valueType(v) for v in value.split()]
-        return collectionType(values)
+        if isinstance(valueType, tuple):
+            valueType = valueType[0]
+        return collectionType(valueType(v) for v in value.split())
 
 
 class MultiConverter(BaseDataConverter):

Modified: z3c.form/trunk/src/z3c/form/converter.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.txt	2009-02-08 23:44:06 UTC (rev 96293)
+++ z3c.form/trunk/src/z3c/form/converter.txt	2009-02-09 08:12:18 UTC (rev 96294)
@@ -867,3 +867,36 @@
 
   >>> tlc.toFieldValue('') is None
   True
+
+It also should work for schema fields that define their type as tuple,
+for instance zope.schema.Int declares its type as (int, long). 
+
+  >>> ids = zope.schema.List(
+  ...     value_type=zope.schema.Int(),
+  ...     )
+
+Let's illustrate the problem:
+
+  >>> zope.schema.Int._type
+  (<type 'int'>, <type 'long'>)
+
+The converter will use the first one.
+
+  >>> tlWidget.field = ids
+  >>> tlc = converter.TextLinesConverter(ids, tlWidget)
+
+Of course, it still can convert to the widget value:
+
+  >>> tlc.toWidgetValue([1,2,3])
+  u'1\n2\n3'
+
+And back:
+
+  >>> tlc.toFieldValue(u'1\n2\n3\n')
+  [1, 2, 3]
+
+An empty string will also cause the missing value to be returned:
+
+  >>> tlc.toFieldValue('') is None
+  True
+ 
\ No newline at end of file



More information about the Checkins mailing list