[Zope3-checkins] CVS: Zope3/src/zope/app/form - utility.py:1.25.14.2

Garrett Smith garrett at mojave-corp.com
Mon Mar 1 18:33:43 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/form
In directory cvs.zope.org:/tmp/cvs-serv27606/src/zope/app/form

Modified Files:
      Tag: garrett-widgets2-branch
	utility.py 
Log Message:
Changes related to widget machinery:

- Added source argument to setUpEditWidgets and setUpDisplayWidgets.
- Renamed IEditWidget to IInputWidget.
- Added a widgetsData attribute to zope.app.interfaces.formWidgetsError.



=== Zope3/src/zope/app/form/utility.py 1.25.14.1 => 1.25.14.2 ===
--- Zope3/src/zope/app/form/utility.py:1.25.14.1	Thu Feb 26 00:10:55 2004
+++ Zope3/src/zope/app/form/utility.py	Mon Mar  1 18:33:12 2004
@@ -41,7 +41,7 @@
 from zope.app.interfaces.form import IWidget
 from zope.app.interfaces.form import WidgetsError, MissingInputError
 from zope.app.interfaces.form import InputErrors
-from zope.app.interfaces.form import IEditWidget, IDisplayWidget
+from zope.app.interfaces.form import IInputWidget, IDisplayWidget
 from zope.component.interfaces import IViewFactory
 
 # A marker that indicates 'no value' for any of the utility functions that
@@ -74,7 +74,7 @@
     code should be treated as 'sticky' until the user successfully updates
     the object.
     """
-    return IEditWidget.isImplementedBy(widget) and widget.hasInput()
+    return IInputWidget.isImplementedBy(widget) and widget.hasInput()
     
 def setUpWidget(view, name, field, viewType, value=no_value, prefix=None,
                 ignoreStickyValues=False, context=None):
@@ -138,8 +138,7 @@
     names to use. If names is None, the list of fields will be defined by
     the schema.
     
-    context provides an alternative context that will be used instead of the
-    view context.
+    context provides an alternative context for acquisition.
     """
     for (name, field) in _fieldlist(names, schema):
         setUpWidget(view, name, field, viewType, 
@@ -148,76 +147,95 @@
                     ignoreStickyValues=ignoreStickyValues, 
                     context=context)
 
-def setUpEditWidgets(view, schema, prefix=None, ignoreStickyValues=False,
-                     names=None, context=None):
-    """Sets up widgets for an edit form.
+def setUpEditWidgets(view, schema, source=None, prefix=None,
+                     ignoreStickyValues=False, names=None, context=None):
+    """Sets up widgets to collect input on a view.
     
-    See setUpWidgets for details on this method's arguments.
+    See setUpWidgets for details on view, schema, prefix, ignoreStickyValues,
+    names, and context.
+    
+    source, if specified, is an object from which initial widget values are
+    read. If source is not specified, the view context is used as the source.
     """
-    _setUpFormWidgets(view, schema, prefix, ignoreStickyValues, names, 
-                      context, IDisplayWidget, IEditWidget)
+    _setUpFormWidgets(view, schema, source, prefix, ignoreStickyValues,
+                      names, context, IDisplayWidget, IInputWidget)
 
-def setUpDisplayWidgets(view, schema, prefix=None, ignoreStickyValues=False,
-                        names=None, context=None):
-    """Sets up widgets for a display (read-only) form.
+def setUpDisplayWidgets(view, schema, source=None, prefix=None, 
+                        ignoreStickyValues=False, names=None, context=None):
+    """Sets up widgets to display field values on a view.
+    
+    See setUpWidgets for details on view, schema, prefix, ignoreStickyValues,
+    names, and context.
     
-    See setUpWidgets for details on this method's arguments.
+    source, if specified, is an object from which initial widget values are
+    read. If source is not specified, the view context is used as the source.
     """
-    _setUpFormWidgets(view, schema, prefix, ignoreStickyValues, names,
-                      context, IDisplayWidget, IDisplayWidget)
+    _setUpFormWidgets(view, schema, source, prefix, ignoreStickyValues,
+                      names, context, IDisplayWidget, IDisplayWidget)
 
-def _setUpFormWidgets(view, schema, prefix, ignoreStickyValues, names,
-                      context, displayType, editType):
+def _setUpFormWidgets(view, schema, source, prefix, ignoreStickyValues,
+                      names, context, displayType, inputType):
     """A helper function used by setUpDisplayWidget and setUpEditWidget."""
     if context is None:
         context = view.context
+    if source is None:
+        source = view.context
     for name, field in _fieldlist(names, schema):
         if field.readonly:
             viewType = displayType
         else:
-            viewType = editType
+            viewType = inputType
         try:
-            value = field.get(context)
+            value = field.get(source)
         except AttributeError, v:
             value = no_value
         setUpWidget(view, name, field, viewType, value, prefix,
                     ignoreStickyValues, context)
 
 def viewHasInput(view, schema, names=None):
-    """Check if we have any user-entered data defined by a schema.
-
-    Returns True if any schema field related widget has input provided by 
-    the user.
+    """Returns True if the any of the view's widgets contain user input.
+    
+    schema specifies the set of fields that correspond to the view widgets.
+    names can be specified to provide a subset of these fields.
     """
     for name, field in _fieldlist(names, schema):
         if  getattr(view, name + '_widget').hasInput():
             return True
     return False
 
-def applyWidgetsChanges(view, schema, names=None, context=None):
-    """Apply changes in widgets to the object."""
+def applyWidgetsChanges(view, schema, target=None, names=None):
+    """Updates an object with values from a view's widgets.
+    
+    view contained the widgets that perform the update. By default, the widgets
+    will update the view's context. target can be specified as an alternative
+    object to update.
+    
+    schema contrains the values provided by the widgets.
+    
+    names can be specified to update a subset of the schema constrained values.    
+    """
     
     errors = []
     changed = False
-    if context is None:
-        context = view.context
-    
+    if target is None:
+        target = view.context
+
     for name, field in _fieldlist(names, schema):
         widget = getattr(view, name + '_widget')
-        if IEditWidget.isImplementedBy(widget) and widget.hasInput():
+        if IInputWidget.isImplementedBy(widget) and widget.hasInput():
             try:
-                changed = widget.applyChanges(context) or changed
+                changed = widget.applyChanges(target) or changed
             except InputErrors, v:
                 errors.append(v)
     if errors:
         raise WidgetsError(*errors)
-        
+
     return changed
 
 def getWidgetsData(view, schema, names=None):
-    """Returnd user entered data for a set of schema fields.
+    """Returns user entered data for a set of schema fields.
     
-    The return value is a map of field names to user entered data.
+    The return value is a map of field names to data values.
     
     view is the view containing the widgets. schema is the schema that
     defines the widget fields. An optional names argument can be provided
@@ -248,7 +266,7 @@
     
     for name, field in _fieldlist(names, schema):
         widget = getattr(view, name + '_widget')
-        if IEditWidget.isImplementedBy(widget):
+        if IInputWidget.isImplementedBy(widget):
             if widget.hasInput():
                 try:
                     result[name] = widget.getInputValue()
@@ -259,7 +277,7 @@
                     name, widget.title, 'the field is required'))
             
     if errors:
-        raise WidgetsError(*errors)
+        raise WidgetsError(errors, widgetsData=result)
         
     return result
 




More information about the Zope3-Checkins mailing list