Changeset 5365


Ignore:
Timestamp:
4 Aug 2010, 18:34:50 (14 years ago)
Author:
uli
Message:
  • Clean up imports.
  • Add sorting. So we can be sure, the dict contents is always shown in the same order (sorted by key).
  • Remove unused code.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.widgets.core/trunk/src/waeup/widgets/core/dictwidget.py

    r5292 r5365  
    2020## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    2121##
    22 import grok
    2322from zope.browserpage import ViewPageTemplateFile
    2423from zope.component import getMultiAdapter
    2524from zope.formlib.i18n import _
    26 from zope.formlib.interfaces import (
    27     ISimpleInputWidget, IInputWidget, IDisplayWidget)
     25from zope.formlib.interfaces import IInputWidget, IDisplayWidget
    2826from zope.formlib.sequencewidget import SequenceWidget
    2927from zope.formlib.widget import DisplayWidget, renderElement
     
    3230from zope.schema.interfaces import IDict, IField
    3331
     32def 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
    3441class DictWidget(SequenceWidget):
     42    """An IInputWidget that renders dictionaries.
     43
     44    Items are displayed in key order.
     45    """
    3546    _type = dict
    3647    template = ViewPageTemplateFile('dictwidget.pt')
     
    7788        return self._widgets[i]
    7889           
    79         if i not in self._widgets:
    80             field = self.context.value_type
    81             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 widget
    89             self._widgets[i] = widget
    90         return self._widgets[i]
    91 
    9290    def widgets(self):
    9391        """Return a list of widgets to display"""
    9492        sequence = self._getRenderedValue()
    9593        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()):
    9798            widget = self._getWidget(i)
    9899            widget[0].setRenderedValue(value[0])
     
    206207
    207208class DictDisplayWidget(DisplayWidget):
    208 
     209    """A display widget for dictionaries.
     210
     211    Items are displayed in key order.
     212    """
     213   
    209214    _missingValueMessage = _("sequence-value-not-provided",
    210215                             u"(no value available)")
     
    237242
    238243        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)):
    240246            key_widget, value_widget = self._getWidget(i)
    241247            key_widget.setRenderedValue(item[0])
     
    270276        value_widget.setPrefix('%s.value.%d.' % (self.name, i))
    271277        return (key_widget, value_widget)
    272 
Note: See TracChangeset for help on using the changeset viewer.