Changeset 5365 for main/waeup.widgets.core/trunk/src/waeup
- Timestamp:
- 4 Aug 2010, 18:34:50 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.widgets.core/trunk/src/waeup/widgets/core/dictwidget.py
r5292 r5365 20 20 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 ## 22 import grok23 22 from zope.browserpage import ViewPageTemplateFile 24 23 from zope.component import getMultiAdapter 25 24 from zope.formlib.i18n import _ 26 from zope.formlib.interfaces import ( 27 ISimpleInputWidget, IInputWidget, IDisplayWidget) 25 from zope.formlib.interfaces import IInputWidget, IDisplayWidget 28 26 from zope.formlib.sequencewidget import SequenceWidget 29 27 from zope.formlib.widget import DisplayWidget, renderElement … … 32 30 from zope.schema.interfaces import IDict, IField 33 31 32 def sort_dict_items(x, y): 33 """Sort items of a dict by key. 34 35 As we don't know what type the key is, `cmp` should serve as 36 solution for most basic types. 37 """ 38 return cmp(x[0], y[0]) 39 40 34 41 class DictWidget(SequenceWidget): 42 """An IInputWidget that renders dictionaries. 43 44 Items are displayed in key order. 45 """ 35 46 _type = dict 36 47 template = ViewPageTemplateFile('dictwidget.pt') … … 77 88 return self._widgets[i] 78 89 79 if i not in self._widgets:80 field = self.context.value_type81 if self.subwidget is not None:82 widget = self.subwidget(field, self.request)83 else:84 widget = getMultiAdapter(85 (field, self.request), IInputWidget)86 widget.setPrefix('%s.%d.' % (self.name, i))87 if not self.preserve_widgets:88 return widget89 self._widgets[i] = widget90 return self._widgets[i]91 92 90 def widgets(self): 93 91 """Return a list of widgets to display""" 94 92 sequence = self._getRenderedValue() 95 93 result = [] 96 for i, value in enumerate(sequence.items()): 94 # for i, item in enumerate(data.items()): 95 for i, value in enumerate(sorted( 96 sequence.items(), cmp=sort_dict_items)): 97 # for i, value in enumerate(sequence.items()): 97 98 widget = self._getWidget(i) 98 99 widget[0].setRenderedValue(value[0]) … … 206 207 207 208 class DictDisplayWidget(DisplayWidget): 208 209 """A display widget for dictionaries. 210 211 Items are displayed in key order. 212 """ 213 209 214 _missingValueMessage = _("sequence-value-not-provided", 210 215 u"(no value available)") … … 237 242 238 243 parts = [] 239 for i, item in enumerate(data.items()): 244 # for i, item in enumerate(data.items()): 245 for i, item in enumerate(sorted(data.items(), cmp=sort_dict_items)): 240 246 key_widget, value_widget = self._getWidget(i) 241 247 key_widget.setRenderedValue(item[0]) … … 270 276 value_widget.setPrefix('%s.value.%d.' % (self.name, i)) 271 277 return (key_widget, value_widget) 272
Note: See TracChangeset for help on using the changeset viewer.