Skip to content

Commit

Permalink
- Fixed a potential problem where a non-ascii vocabulary/source term …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
strichter committed Jan 31, 2012
1 parent c882f8b commit 6360365
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
8 changes: 6 additions & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
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)
Expand Down
3 changes: 2 additions & 1 deletion src/z3c/form/browser/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def update(self):
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)
Expand Down
17 changes: 17 additions & 0 deletions src/z3c/form/browser/checkbox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ Check HIDDEN_MODE:
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
----------------------
Expand Down
3 changes: 2 additions & 1 deletion src/z3c/form/browser/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def update(self):
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)
Expand Down
18 changes: 18 additions & 0 deletions src/z3c/form/browser/radio.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,21 @@ Check HIDDEN_MODE:
<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'}]
9 changes: 7 additions & 2 deletions src/z3c/form/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,13 @@ def toFieldValue(self, value):
"""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))
Expand Down
11 changes: 11 additions & 0 deletions src/z3c/form/converter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,17 @@ datetime value:
>>> 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
Expand Down

0 comments on commit 6360365

Please sign in to comment.