From dd40f6f513b08854c64e8d59cf6440082f4b11d9 Mon Sep 17 00:00:00 2001 From: Adam Groszer Date: Thu, 25 Feb 2016 02:04:30 +0100 Subject: [PATCH] RadioWidget items are better determined when they are needed --- CHANGES.txt | 2 +- src/z3c/form/browser/radio.py | 25 ++++++++++++------------- src/z3c/form/browser/radio.txt | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 432aefaa..f9c5b791 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,7 +5,7 @@ CHANGES 3.2.10 (unreleased) ------------------- -- Nothing changed yet. +- RadioWidget items are better determined when they are needed [agroszer] 3.2.9 (2016-02-01) diff --git a/src/z3c/form/browser/radio.py b/src/z3c/form/browser/radio.py index 1a75cc90..6993d234 100644 --- a/src/z3c/form/browser/radio.py +++ b/src/z3c/form/browser/radio.py @@ -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 @@ -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) @@ -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): diff --git a/src/z3c/form/browser/radio.txt b/src/z3c/form/browser/radio.txt index 635f0034..cdee3192 100644 --- a/src/z3c/form/browser/radio.txt +++ b/src/z3c/form/browser/radio.txt @@ -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',