[Checkins] SVN: z3c.form/trunk/ - Fixed a potential problem where a non-ascii vocabulary/source term value

Stephan Richter srichter at gmail.com
Tue Jan 31 04:01:46 UTC 2012


Log message for revision 124259:
  - Fixed a potential problem where a non-ascii vocabulary/source term value
    could cause the checkbox and readio widget to crash.
  
  - Fixed a problem with the ``datetime.timedelta`` converter, which failed to
    convert back to the field value, when the day part was missing.
  
  

Changed:
  U   z3c.form/trunk/CHANGES.txt
  U   z3c.form/trunk/src/z3c/form/browser/checkbox.py
  U   z3c.form/trunk/src/z3c/form/browser/checkbox.txt
  U   z3c.form/trunk/src/z3c/form/browser/radio.py
  U   z3c.form/trunk/src/z3c/form/browser/radio.txt
  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	2012-01-30 18:05:17 UTC (rev 124258)
+++ z3c.form/trunk/CHANGES.txt	2012-01-31 04:01:45 UTC (rev 124259)
@@ -2,12 +2,16 @@
 CHANGES
 =======
 
-2.6.1 (unreleased)
+2.6.1 (2012-01-30)
 ------------------
 
-- Nothing changed yet.
+- Fixed a potential problem where a non-ascii vocabulary/source term value
+  could cause the checkbox and readio widget to crash.
 
+- Fixed a problem with the ``datetime.timedelta`` converter, which failed to
+  convert back to the field value, when the day part was missing.
 
+
 2.6.0 (2012-01-30)
 ------------------
 

Modified: z3c.form/trunk/src/z3c/form/browser/checkbox.py
===================================================================
--- z3c.form/trunk/src/z3c/form/browser/checkbox.py	2012-01-30 18:05:17 UTC (rev 124258)
+++ z3c.form/trunk/src/z3c/form/browser/checkbox.py	2012-01-31 04:01:45 UTC (rev 124259)
@@ -47,7 +47,8 @@
         for count, term in enumerate(self.terms):
             checked = self.isChecked(term)
             id = '%s-%i' % (self.id, count)
-            label = unicode(term.value)
+            label = unicode(term.value) if not isinstance(term.value, str) \
+                else unicode(term.value, 'utf-8', errors='ignore')
             if zope.schema.interfaces.ITitledTokenizedTerm.providedBy(term):
                 label = translate(term.title, context=self.request,
                                   default=term.title)

Modified: z3c.form/trunk/src/z3c/form/browser/checkbox.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/browser/checkbox.txt	2012-01-30 18:05:17 UTC (rev 124258)
+++ z3c.form/trunk/src/z3c/form/browser/checkbox.txt	2012-01-31 04:01:45 UTC (rev 124259)
@@ -130,7 +130,24 @@
            class="checkbox-widget" value="false" />
   </span>
 
+Make sure that we produce a proper label when we have no title for a term and
+the value (which is used as a backup label) contains non-ASCII characters:
 
+  >>> terms = SimpleVocabulary.fromValues(['yes\012', 'no\243'])
+  >>> widget.terms = terms
+  >>> widget.update()
+  >>> print widget.items
+  [{'label': u'yes\n',
+    'checked': False,
+    'id': 'widget-id-0',
+    'value': 'yes\n',
+    'name': 'widget.name:list'},
+    {'label': u'no',
+     'checked': False,
+      'id': 'widget-id-1',
+      'value': 'no\xa3',
+      'name': 'widget.name:list'}]
+
 Single Checkbox Widget
 ----------------------
 

Modified: z3c.form/trunk/src/z3c/form/browser/radio.py
===================================================================
--- z3c.form/trunk/src/z3c/form/browser/radio.py	2012-01-30 18:05:17 UTC (rev 124258)
+++ z3c.form/trunk/src/z3c/form/browser/radio.py	2012-01-31 04:01:45 UTC (rev 124259)
@@ -46,7 +46,8 @@
         for count, term in enumerate(self.terms):
             checked = self.isChecked(term)
             id = '%s-%i' % (self.id, count)
-            label = unicode(term.value)
+            label = unicode(term.value) if not isinstance(term.value, str) \
+                else unicode(term.value, 'utf-8', errors='ignore')
             if zope.schema.interfaces.ITitledTokenizedTerm.providedBy(term):
                 label = translate(term.title, context=self.request,
                                   default=term.title)

Modified: z3c.form/trunk/src/z3c/form/browser/radio.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/browser/radio.txt	2012-01-30 18:05:17 UTC (rev 124258)
+++ z3c.form/trunk/src/z3c/form/browser/radio.txt	2012-01-31 04:01:45 UTC (rev 124259)
@@ -121,3 +121,21 @@
   <input id="widget-id-0" name="widget.name" value="true"
          class="hidden-widget" type="hidden" />
 
+Make sure that we produce a proper label when we have no title for a term and
+the value (which is used as a backup label) contains non-ASCII characters:
+
+  >>> from zope.schema.vocabulary import SimpleVocabulary
+  >>> terms = SimpleVocabulary.fromValues(['yes\012', 'no\243'])
+  >>> widget.terms = terms
+  >>> widget.update()
+  >>> print widget.items
+  [{'label': u'yes\n',
+    'checked': False,
+    'id': 'widget-id-0',
+    'value': 'yes\n',
+    'name': 'widget.name'},
+    {'label': u'no',
+     'checked': False,
+     'id': 'widget-id-1',
+     'value': 'no\xa3',
+     'name': 'widget.name'}]

Modified: z3c.form/trunk/src/z3c/form/converter.py
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.py	2012-01-30 18:05:17 UTC (rev 124258)
+++ z3c.form/trunk/src/z3c/form/converter.py	2012-01-31 04:01:45 UTC (rev 124259)
@@ -198,8 +198,13 @@
         """See interfaces.IDataConverter"""
         if value == u'':
             return self.field.missing_value
-        daysString, crap, timeString = value.split(' ')
-        days = int(daysString)
+        try:
+            daysString, crap, timeString = value.split(' ')
+        except ValueError:
+            timeString = value
+            days = 0
+        else:
+            days = int(daysString)
         seconds = [int(part)*60**(2-n)
                    for n, part in enumerate(timeString.split(':'))]
         return datetime.timedelta(days, sum(seconds))

Modified: z3c.form/trunk/src/z3c/form/converter.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/converter.txt	2012-01-30 18:05:17 UTC (rev 124258)
+++ z3c.form/trunk/src/z3c/form/converter.txt	2012-01-31 04:01:45 UTC (rev 124259)
@@ -377,6 +377,17 @@
   >>> tddc.toFieldValue(u'1 day, 1:01:01')
   datetime.timedelta(1, 3661)
 
+If no day is available, the following short form is used:
+
+  >>> noDay = datetime.timedelta(0, 3600+60+1)
+  >>> tddc.toWidgetValue(noDay)
+  u'1:01:01'
+
+And now back to the field value:
+
+  >>> tddc.toFieldValue(u'1:01:01')
+  datetime.timedelta(0, 3661)
+
 By default the converter converts missing input to missin_input value:
 
   >>> tddc.toFieldValue(u'') is None



More information about the checkins mailing list