Skip to content

Commit

Permalink
Merge pull request #8 from jriboux/master
Browse files Browse the repository at this point in the history
Fix unicode decode error in weird cases in checkbox.CheckboxWidget.update and radio.RadioWidget.update
  • Loading branch information
strichter committed Jul 5, 2013
2 parents 2713e3b + 5c0acb9 commit 40bdcb1
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
3 changes: 1 addition & 2 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ CHANGES
3.0.2 (unreleased)
------------------

- Nothing changed yet.

- Fix unicode decode error in weird cases in checkbox.CheckboxWidget.update and radio.RadioWidget.update (eg: when term.value is an Plone Archetype ATFile)

3.0.1 (2013-06-25)
------------------
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 @@ -48,10 +48,11 @@ def update(self):
for count, term in enumerate(self.terms):
checked = self.isChecked(term)
id = '%s-%i' % (self.id, count)
label = util.toUnicode(term.value)
if zope.schema.interfaces.ITitledTokenizedTerm.providedBy(term):
label = translate(term.title, context=self.request,
default=term.title)
else:
label = util.toUnicode(term.value)
self.items.append(
{'id':id, 'name':self.name + ':list', 'value':term.token,
'label':label, 'checked':checked})
Expand Down
52 changes: 52 additions & 0 deletions src/z3c/form/browser/checkbox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,55 @@ Check HIDDEN_MODE:
class="single-checkbox-widget" value="selected" />
</span>


Term with non ascii __str__
---------------------------

Check if a term which __str__ returns non ascii string does not crash the update method

>>> from zope.interface.verify import verifyClass
>>> from z3c.form import interfaces
>>> from z3c.form.browser import checkbox
>>> from z3c.form.testing import TestRequest

>>> request = TestRequest()

>>> widget = checkbox.CheckBoxWidget(request)
>>> widget.id = 'widget-id'
>>> widget.name = 'widget.name'

>>> import zope.schema.interfaces
>>> from zope.schema.vocabulary import SimpleVocabulary,SimpleTerm
>>> import z3c.form.term
>>> class ObjWithNonAscii__str__:
... def __str__(self):
... return 'héhé!'
>>> class MyTerms(z3c.form.term.ChoiceTermsVocabulary):
... def __init__(self, context, request, form, field, widget):
... self.terms = SimpleVocabulary([
... SimpleTerm(ObjWithNonAscii__str__(), 'one', 'One'),
... SimpleTerm(ObjWithNonAscii__str__(), 'two', 'Two'),
... ])
>>> zope.component.provideAdapter(MyTerms,
... adapts=(zope.interface.Interface,
... interfaces.IFormLayer, zope.interface.Interface,
... zope.interface.Interface, interfaces.ICheckBoxWidget))
>>> widget.update()
>>> print(widget.render())
<html>
<body>
<span class="option">
<input class="checkbox-widget" id="widget-id-0" name="widget.name:list" type="checkbox" value="one">
<label for="widget-id-0">
<span class="label">One</span>
</label>
</span>
<span class="option">
<input class="checkbox-widget" id="widget-id-1" name="widget.name:list" type="checkbox" value="two">
<label for="widget-id-1">
<span class="label">Two</span>
</label>
</span>
<input name="widget.name-empty-marker" type="hidden" value="1">
</body>
</html>
3 changes: 2 additions & 1 deletion src/z3c/form/browser/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ def update(self):
for count, term in enumerate(self.terms):
checked = self.isChecked(term)
id = '%s-%i' % (self.id, count)
label = util.toUnicode(term.value)
if zope.schema.interfaces.ITitledTokenizedTerm.providedBy(term):
label = translate(term.title, context=self.request,
default=term.title)
else:
label = util.toUnicode(term.value)
self.items.append(
{'id':id, 'name':self.name, 'value':term.token,
'label':label, 'checked':checked})
Expand Down
52 changes: 52 additions & 0 deletions src/z3c/form/browser/radio.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,55 @@ the value (which is used as a backup label) contains non-ASCII characters:

Note: The "\234" character is interpreted differently in Pytohn 2 and 3
here. (This is mostly due to changes int he SimpleVocabulary code.)

Term with non ascii __str__
---------------------------

Check if a term which __str__ returns non ascii string does not crash the update method

>>> request = TestRequest()
>>> widget = radio.RadioWidget(request)
>>> widget.id = 'widget-id'
>>> widget.name = 'widget.name'
>>> template = os.path.join(os.path.dirname(z3c.form.browser.__file__),
... 'radio_input.pt')
>>> factory = z3c.form.widget.WidgetTemplateFactory(template)
>>> zope.component.provideAdapter(factory,
... (zope.interface.Interface, IDefaultBrowserLayer, None, None, None),
... IPageTemplate, name='input')

>>> import zope.schema.interfaces
>>> from zope.schema.vocabulary import SimpleVocabulary,SimpleTerm
>>> import z3c.form.term
>>> class ObjWithNonAscii__str__:
... def __str__(self):
... return 'héhé!'
>>> class MyTerms(z3c.form.term.ChoiceTermsVocabulary):
... def __init__(self, context, request, form, field, widget):
... self.terms = SimpleVocabulary([
... SimpleTerm(ObjWithNonAscii__str__(), 'one', 'One'),
... SimpleTerm(ObjWithNonAscii__str__(), 'two', 'Two'),
... ])
>>> zope.component.provideAdapter(MyTerms,
... adapts=(zope.interface.Interface,
... interfaces.IFormLayer, zope.interface.Interface,
... zope.interface.Interface, interfaces.IRadioWidget))
>>> widget.update()
>>> print(widget.render())
<html>
<body>
<span class="option">
<label for="widget-id-0">
<input class="radio-widget" id="widget-id-0" name="widget.name" type="radio" value="one">
<span class="label">One</span>
</label>
</span>
<span class="option">
<label for="widget-id-1">
<input class="radio-widget" id="widget-id-1" name="widget.name" type="radio" value="two">
<span class="label">Two</span>
</label>
</span>
<input name="widget.name-empty-marker" type="hidden" value="1">
</body>
</html>

0 comments on commit 40bdcb1

Please sign in to comment.