[Zope3-checkins] CVS: Zope3/src/zope/app/form - utility.py:1.23 widget.py:1.8

Garrett Smith garrett at mojave-corp.com
Wed Aug 13 18:29:10 EDT 2003


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

Modified Files:
	utility.py widget.py 
Log Message:
Made the following changes to the widget machinery:

- Renamed IWidget getData to getInputValue

getInputValue no longer accepts an 'optional' flag. If value is missing or is invalid, getInputValue will raise an error. Calls made to this method should be in a try...except block to handle such conditions.

- Renamed IWidget haveData to hasInput

- Added method hasValidInput to IWidget and widget implementations

- Renamed IWidget setData to setRenderedValue

- Added functional tests for some of the core widgets - additional ftests are needed

- Deleted the class PossibleEmptyMeansMissing - it's no longer needed

- Added deprecation tests for changes to widgets

- Some widgets were refactored to use the new framework correctly

These changes were based on the proposal:

 http://dev.zope.org/Zope3/ComponentArchitecture/WidgetsFormsSchemas

Not all the changes in the proposal are included in this commit. Specifically, getRawData/setRawData and the refactoring of the widget error classes are the two major changes not included in this commit.

=== Zope3/src/zope/app/form/utility.py 1.22 => 1.23 ===
--- Zope3/src/zope/app/form/utility.py:1.22	Sun Jul 13 02:47:21 2003
+++ Zope3/src/zope/app/form/utility.py	Wed Aug 13 17:28:34 2003
@@ -161,18 +161,8 @@
     if prefix:
         widget.setPrefix(prefix)
 
-    # XXX
-    # Only set data if the widget doesn't have any itself already from the
-    # request. This is a problem for something like a checkbox, where it
-    # always claims to have data, becuase when there is no name in the request
-    # for it, its value is False.
-    # This is only a problem when force is False.
-    #
-    # It took me a while to work out what 'force' means in all these methods.
-    # Perhaps it should be renamed 'preserveExistingData', and have the
-    # opposite meaning.
-    if value is not None and (force or not widget.haveData()):
-        widget.setData(value)
+    if force or not widget.hasInput():
+        widget.setRenderedValue(value)
 
 
 def setUpWidgets(view, schema, prefix=None, force=False,
@@ -241,39 +231,35 @@
         setUpWidget(view, name, field, value,
                     prefix=prefix, force=force, vname=vname, context=context)
 
-def haveWidgetsData(view, schema, names=None):
-    """Check if we have any user-entered data defined by a schema
+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 data
-    that was entered by the user.
+    Returns True if any schema field related widget has input provided by 
+    the user.
     """
     for name, field in _fieldlist(names, schema):
-        if  getattr(view, name+'_widget').haveData():
+        if  getattr(view, name+'_widget').hasInput():
             return True
-
     return False
 
 def applyWidgetsChanges(view, content, schema, strict=True,
         names=None, set_missing=True, do_not_raise=False,
         exclude_readonly=False):
-    """Apply changes in widgets to the object."""
+    """Apply changes in widgets to the object.
+    
+    XXX this needs to be thoroughly documented.
+    """
     errors = []
-
     changed = False
     for name, field in _fieldlist(names, schema):
         widget = getattr(view, name+'_widget')
         if exclude_readonly and field.readonly:
             continue
-        if widget.haveData():
+        if widget.hasInput():
             try:
                 changed = widget.applyChanges(content) or changed
             except InputErrors, v:
                 errors.append(v)
-        elif strict and field.required:
-            err = MissingInputError(name, widget.title, 'the field is required')
-            errors.append(err)
-        elif set_missing:
-            field.set(content, field.missing_value)
 
     if errors and not do_not_raise:
         raise WidgetsError(*errors)
@@ -321,9 +307,9 @@
         widget = getattr(view, name+'_widget')
         if exclude_readonly and widget.context.readonly:
             continue
-        if widget.haveData():
+        if widget.hasInput():
             try:
-                result[name] = widget.getData()
+                result[name] = widget.getInputValue()
             except InputErrors, v:
                 errors.append(v)
         elif strict and field.required:


=== Zope3/src/zope/app/form/widget.py 1.7 => 1.8 ===
--- Zope3/src/zope/app/form/widget.py:1.7	Mon Jul 14 11:06:10 2003
+++ Zope3/src/zope/app/form/widget.py	Wed Aug 13 17:28:34 2003
@@ -14,6 +14,8 @@
 """
 $Id$
 """
+import traceback
+from warnings import warn
 from zope.app.interfaces.form import IWidget
 from zope.component.interfaces import IViewFactory
 from zope.interface import implements
@@ -26,7 +28,8 @@
     implements(IWidget)
 
     _prefix = 'field.'
-    _data = None
+    _data_marker = object()
+    _data = _data_marker
 
     def __init__(self, context, request):
         self.context = context
@@ -48,13 +51,26 @@
         self.name = prefix + self.context.__name__
 
     def setData(self, value):
+        if traceback.extract_stack()[-2][2] != 'setRenderedValue':
+            warn("setData is deprecated - use setRenderedValue",
+                DeprecationWarning, 2)
+        
+        # XXX - move this implementation to setRenderedValue when 
+        # deprecation is removed
+
         self._data = value
 
-    def haveData(self):
-        raise TypeError("haveData has not been implemented")
+    def setRenderedValue(self, value):
+        self.setData(value)
+
+    def hasInput(self):
+        raise TypeError("hasInput has not been implemented")
 
-    def getData(self):
-        raise TypeError("getData has not been implemented")
+    def hasValidInput(self):
+        raise TypeError("hasValidInput has not been implemented")
+    
+    def getInputValue(self):
+        raise TypeError("getInputValue has not been implemented")
 
     def validate(self):
         raise TypeError("validate has not been implemented")




More information about the Zope3-Checkins mailing list