Skip to content

Commit

Permalink
Merge pull request #39 from zopefoundation/adamg-fixes
Browse files Browse the repository at this point in the history
Smaller fixes
  • Loading branch information
agroszer committed Feb 25, 2016
2 parents 4231cca + 688d12f commit 8965b40
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
7 changes: 6 additions & 1 deletion CHANGES.txt
Expand Up @@ -5,8 +5,13 @@ CHANGES
3.2.10 (unreleased)
-------------------

- Nothing changed yet.
- RadioWidget items are better determined when they are needed [agroszer]

- CheckBoxWidget items are better determined when they are needed [agroszer]

- Bugfix: The ``ChoiceTerms`` adapter blindly assumed that the passed in field
is unbound, which is not necessarily the case in interesting ObjectWidget
scenarios. Not it checks for a non-None field context first. [srichter]

3.2.9 (2016-02-01)
------------------
Expand Down
27 changes: 15 additions & 12 deletions src/z3c/form/browser/checkbox.py
Expand Up @@ -40,14 +40,10 @@ class CheckBoxWidget(widget.HTMLInputWidget, SequenceWidget):
def isChecked(self, term):
return term.token in self.value

def update(self):
"""See z3c.form.interfaces.IWidget."""
super(CheckBoxWidget, self).update()
widget.addFieldClass(self)
# XXX: this is to early for setup items. See select widget how this
# sould be done. Setup the items here doens't allow to override the
# widget.value in updateWidgets, ri
self.items = []
def items(self):
if self.terms is None:
return ()
items = []
for count, term in enumerate(self.terms):
checked = self.isChecked(term)
id = '%s-%i' % (self.id, count)
Expand All @@ -56,16 +52,23 @@ def update(self):
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})
items.append(
{'id': id, 'name': self.name + ':list', 'value': term.token,
'label': label, 'checked': checked})
return items

def update(self):
"""See z3c.form.interfaces.IWidget."""
super(CheckBoxWidget, self).update()
widget.addFieldClass(self)

def json_data(self):
data = super(CheckBoxWidget, self).json_data()
data['options'] = self.items
data['options'] = list(self.items())
data['type'] = 'check'
return data


@zope.component.adapter(zope.schema.interfaces.IField, interfaces.IFormLayer)
@zope.interface.implementer(interfaces.IFieldWidget)
def CheckBoxFieldWidget(field, request):
Expand Down
2 changes: 1 addition & 1 deletion src/z3c/form/browser/checkbox.txt
Expand Up @@ -206,7 +206,7 @@ the value (which is used as a backup label) contains non-ASCII characters:
>>> terms = SimpleVocabulary.fromValues([b'yes\012', b'no\243'])
>>> widget.terms = terms
>>> widget.update()
>>> pprint(widget.items)
>>> pprint(list(widget.items()))
[{'checked': False,
'id': 'widget-id-0',
'label': 'yes\n',
Expand Down
25 changes: 12 additions & 13 deletions src/z3c/form/browser/radio.py
Expand Up @@ -36,7 +36,6 @@ class RadioWidget(widget.HTMLInputWidget, SequenceWidget):

klass = u'radio-widget'
css = u'radio'
items = ()

def isChecked(self, term):
return term.token in self.value
Expand All @@ -60,14 +59,9 @@ def renderForValue(self, value):
IPageTemplate, name=self.mode + '_single')
return template(self, item)

def update(self):
"""See z3c.form.interfaces.IWidget."""
super(RadioWidget, self).update()
# XXX: this is to early for setup items. See select widget how this
# sould be done. Setup the items here doens't allow to override the
# widget.value in updateWidgets, ri
widget.addFieldClass(self)
self.items = []
def items(self):
if self.terms is None:
return
for count, term in enumerate(self.terms):
checked = self.isChecked(term)
id = '%s-%i' % (self.id, count)
Expand All @@ -76,16 +70,21 @@ def update(self):
default=term.title)
else:
label = util.toUnicode(term.value)
self.items.append(
{'id':id, 'name':self.name, 'value':term.token,
'label':label, 'checked':checked})
yield {'id': id, 'name': self.name, 'value': term.token,
'label': label, 'checked': checked}

def update(self):
"""See z3c.form.interfaces.IWidget."""
super(RadioWidget, self).update()
widget.addFieldClass(self)

def json_data(self):
data = super(RadioWidget, self).json_data()
data['options'] = self.items
data['options'] = list(self.items())
data['type'] = 'radio'
return data


@zope.component.adapter(zope.schema.interfaces.IField, interfaces.IFormLayer)
@zope.interface.implementer(interfaces.IFieldWidget)
def RadioFieldWidget(field, request):
Expand Down
2 changes: 1 addition & 1 deletion src/z3c/form/browser/radio.txt
Expand Up @@ -240,7 +240,7 @@ the value (which is used as a backup label) contains non-ASCII characters:
>>> terms = SimpleVocabulary.fromValues([b'yes\012', b'no\243'])
>>> widget.terms = terms
>>> widget.update()
>>> pprint(widget.items)
>>> pprint(list(widget.items()))
[{'checked': False,
'id': 'widget-id-0',
'label': 'yes\n',
Expand Down
3 changes: 2 additions & 1 deletion src/z3c/form/term.py
Expand Up @@ -101,7 +101,8 @@ def __contains__(self, value):
zope.schema.interfaces.IChoice,
interfaces.IWidget)
def ChoiceTerms(context, request, form, field, widget):
field = field.bind(context)
if field.context is None:
field = field.bind(context)
terms = field.vocabulary
return zope.component.queryMultiAdapter(
(context, request, form, field, terms, widget),
Expand Down

0 comments on commit 8965b40

Please sign in to comment.