[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms/Views/Browser - Converter.py:1.3 FormView.py:1.9 IForm.py:1.6 Widget.py:1.6

Stephan Richter srichter@cbu.edu
Wed, 24 Jul 2002 06:53:49 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Forms/Views/Browser
In directory cvs.zope.org:/tmp/cvs-serv1557/lib/python/Zope/App/Forms/Views/Browser

Modified Files:
	Converter.py FormView.py IForm.py Widget.py 
Log Message:
Made some bug fixes and some changes. "items" for the ItemWidgets no expect
a list of (value, description) instead of (description, value) as it was in 
Formulator. This way we can make better use of dict.items().


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/Converter.py 1.2 => 1.3 ===
 from Schema.IField import *
 
 
-class RequestToFieldConverter(Converter):
-    """Base class that defines how to convert from the Request to a Field.
-    Note that the value argument for the convert method is a string
-    containing the name of the variable in the Request."""
-    __convert_from__ = IRequest
-    __convert_to__ = IField
-    
-    field_prefix = 'field_'        
+class NoneToEmptyListConverter(Converter):
+    """Converts None object to an empty list."""
 
     def convert(self, value):
-        'See Zope.App.Forms.IConverter.IConverter'
-        request = self.context
-        raw_value = request.form.get(self.field_prefix+value)
-        return raw_value
+        if value is None:
+            return []
+        else:
+            return value
 
 
-class RequestToStrConverter(Converter):
-    """A specific class converting the in the request contained variable to
-    a string."""
-    __convert_from__ = IRequest
-    __convert_to__ = IStr
-
-
-class RequestToIntConverter(ContainerConverter):
-    """Convert from Request to an Int."""
-    converters = [RequestToStrConverter, StrToIntrConverter]
-
-
-class RequestToFloatConverter(ContainerConverter):
-    """Convert from Request to an Float."""
-    converters = [RequestToStrConverter, StrToFloatConverter]


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/FormView.py 1.8 => 1.9 ===
         return result
 
 
-    def getWidgetForFieldId(self, id):
+    def getField(self, id):
         'See Zope.App.Forms.Views.Browser.IForm.IReadForm'
         # XXX This needs to be optimized!
         field = None
         for f in self.getFields():
             if f.id == id:
                 field = f
+                break
         if field is None:
             raise KeyError, 'Field id "%s" does not exist.' %id
+        return field
+
+
+    def getWidgetForFieldId(self, id):
+        'See Zope.App.Forms.Views.Browser.IForm.IReadForm'
+        field = self.getField(id)
         return self.getWidgetForField(field)
 
 
@@ -84,10 +91,12 @@
         return getView(field, 'widget', self.request)
 
 
-    def renderField(self, field):
+    def renderField(self, field, useRequest=0):
         'See Zope.App.Forms.Views.Browser.IForm.IReadForm'
         widget = self.getWidgetForField(field)
-        value = self.request.form.get('field_' + field.id)
+        value = None
+        if useRequest:
+            value = self.request.form.get('field_' + field.id)
         if value is None:
             value = getattr(self.context, field.id)
         return widget.render(value)
@@ -155,6 +164,7 @@
         try:
             self.saveValuesInContext()
         except (ValidationErrorsAll, ConversionErrorsAll), e:
+            print e[0]
             return self.form(self, errors=e)
         else:
             # XXX This should do a redirect by looking up the object in


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/IForm.py 1.5 => 1.6 ===
     def getFields():
         """Get all the fields that need input from the content object."""
 
+    def getField(id):
+        """Get a field by id from the content object schemas."""
+
     def getWidgetForFieldId(id):
         """Lookup the widget of the field having id."""
 


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/Widget.py 1.5 => 1.6 ===
 ListTypes = (ListType, TupleType)
 from Schema import Converter
 from Zope.ComponentArchitecture import getAdapter
+from Zope.Proxy.ProxyIntrospection import removeAllProxies
 from Zope.Publisher.Browser.BrowserView import BrowserView
 from Zope.App.Forms.Views.Browser.IBrowserWidget import IBrowserWidget
+from Zope.App.Forms.Converter import \
+     NoneToEmptyListConverter, ValueToSingleItemListConverter
 from Zope.App.Forms.Widget import Widget
 
 
@@ -201,10 +204,10 @@
         rendered_items = []
         for item in items:
             try:
-                item_text, item_value = item
+                item_value, item_text = item
             except ValueError:
-                item_text = item
                 item_value = item
+                item_text = item
 
             if item_value == value:
                 rendered_item = self.renderSelectedItem(item_text,
@@ -282,21 +285,27 @@
 class MultiItemsWidget(ItemsWidget):
     """A widget with a number of items that has multiple selectable items."""
     default = []
+    converter = Converter.CombinedConverter(
+        (NoneToEmptyListConverter(), ValueToSingleItemListConverter()))
         
     def renderItems(self, value):
         # need to deal with single item selects
+        value = removeAllProxies(value)
+
         if not isinstance(value, ListTypes):
             value = [value]
         name = self.context.id
-        items = self.context.get('items')
+        items = self.context.items
+        if callable(items):
+            items = items()
         cssClass = self.getValue('cssClass')
         rendered_items = []
         for item in items:
             try:
-                item_text, item_value = item
+                item_value, item_text = item
             except ValueError:
-                item_text = item
                 item_value = item
+                item_text = item
 
             if item_value in value:
                 rendered_item = self.renderSelectedItem(item_text,