[Zope3-checkins] CVS: Zope3/src/zope/app/form/browser - interfaces.py:1.5 widget.py:1.10

Garrett Smith garrett at mojave-corp.com
Tue May 11 07:16:28 EDT 2004


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

Modified Files:
	interfaces.py widget.py 
Log Message:
Refactor of browser widget framework per:

  http://dev.zope.org/Zope3/CleanupOfSchemaAndWidgets 


=== Zope3/src/zope/app/form/browser/interfaces.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/form/browser/interfaces.py:1.4	Fri May  7 15:39:44 2004
+++ Zope3/src/zope/app/form/browser/interfaces.py	Tue May 11 07:16:28 2004
@@ -15,7 +15,84 @@
 $Id$
 """
 from zope.interface import Interface
-from zope.app.form.interfaces import IWidget
+from zope.schema import TextLine, Bool
+from zope.app.form.interfaces import IWidget, IInputWidget
+
+
+class IBrowserWidget(IWidget):
+    """A widget for use in a web browser UI."""
+
+    def __call__():
+        """Render the widget."""
+
+    def hidden():
+        """Render the widget as a hidden field."""
+
+    def error():
+        """Render the validation error for the widget, or return
+        an empty string if no error"""
+        
+        
+class ISimpleInputWidget(IBrowserWidget, IInputWidget):
+    """A widget that uses a single HTML element to collect user input."""
+    
+    tag = TextLine(
+        title=u'Tag',
+        description=u'The widget HTML element.')
+        
+    type = TextLine(
+        title=u'Type',
+        description=u'The element type attribute',
+        required=False)
+        
+    cssClass = TextLine(
+        title=u'CSS Class',
+        description=u'The element class attribute.',
+        required=False)
+        
+    extra = TextLine(
+        title=u'Extra',
+        description=u'The element extra attribute.',
+        required=False)
+        
+        
+class ITextBrowserWidget(ISimpleInputWidget):
+    
+    convert_missing_value = Bool(
+        title=u'Translate Input Value',
+        description=
+            u'If True, an empty string is converted to field.missing_value.',
+        default=True)
+    
+
+class IFormCollaborationView(Interface):
+    """Views that collaborate to create a single form.
+
+    When a form is applied, the changes in the form need to
+    be applied to individual views, which update objects as
+    necessary.
+    """
+
+    def __call__():
+        """Render the view as a part of a larger form.
+
+        Form input elements should be included, prefixed with the
+        prefix given to setPrefix.
+
+        'form' and 'submit' elements should not be included. They
+        will be provided for the larger form.
+        """
+
+    def setPrefix(prefix):
+        """Set the prefix used for names of input elements
+
+        Element names should begin with the given prefix,
+        followed by a dot.
+        """
+
+    def update():
+        """Update the form with data from the request."""
+
 
 class IAddFormCustomization(Interface):
     """API for add form customization.
@@ -74,50 +151,6 @@
         The default implementation returns self.context.nextURL(),
         i.e. it delegates to the IAdding view.
         """
-
-class IBrowserWidget(IWidget):
-    """A widget for use in a web browser UI."""
-
-    def __call__():
-        """Render the widget."""
-
-    def hidden():
-        """Render the widget as a hidden field."""
-
-    def error():
-        """Render the validation error for the widget, or return
-        an empty string if no error"""
-
-
-class IFormCollaborationView(Interface):
-    """Views that collaborate to create a single form.
-
-    When a form is applied, the changes in the form need to
-    be applied to individual views, which update objects as
-    necessary.
-    """
-
-    def __call__():
-        """Render the view as a part of a larger form.
-
-        Form input elements should be included, prefixed with the
-        prefix given to setPrefix.
-
-        'form' and 'submit' elements should not be included. They
-        will be provided for the larger form.
-        """
-
-    def setPrefix(prefix):
-        """Set the prefix used for names of input elements
-
-        Element names should begin with the given prefix,
-        followed by a dot.
-        """
-
-    def update():
-        """Update the form with data from the request."""
-
-
 class IVocabularyQueryView(Interface):
     """View support for IVocabularyQuery objects.
 


=== Zope3/src/zope/app/form/browser/widget.py 1.9 => 1.10 ===
--- Zope3/src/zope/app/form/browser/widget.py:1.9	Fri May  7 15:46:13 2004
+++ Zope3/src/zope/app/form/browser/widget.py	Tue May 11 07:16:28 2004
@@ -17,7 +17,6 @@
 """
 import re, cgi
 import traceback
-from warnings import warn
 from xml.sax.saxutils import quoteattr
 
 from zope.interface import implements
@@ -26,149 +25,217 @@
 
 from zope.app import zapi
 from zope.app.tests import ztapi
-from zope.app.form import Widget
+from zope.app.form import Widget, InputWidget
 from zope.app.form.interfaces import WidgetInputError, MissingInputError
 from zope.app.form.browser.interfaces import IBrowserWidget
+from zope.app.form.browser.interfaces import ISimpleInputWidget
 from zope.app.form.browser.interfaces import IWidgetInputErrorView
 
 class BrowserWidget(Widget, BrowserView):
-    """A field widget that knows how to display itself as HTML.
+    """Base class for browser widgets.
 
-    When we generate labels, titles, descriptions, and errors, the
-    labels, titles, and descriptions are translated and the
-    errors are rendered with the view machinery, so we need to set up
-    a lot of machinery to support translation and views:
-
-	  >>> setUp() # now we have to set up an error view...
-	  >>> from zope.app.form.interfaces import IWidgetInputError
-	  >>> from zope.app.publisher.browser import BrowserView
-	  >>> from cgi import escape
-	  >>> class SnippetErrorView(BrowserView):
-	  ...     implements(IWidgetInputErrorView)
-	  ...     def snippet(self):
-	  ...         return escape(self.context.errors[0])
-	  ...
-	  >>> ztapi.browserViewProviding(IWidgetInputError, SnippetErrorView,
-	  ...                            IWidgetInputErrorView)
-	  >>> from zope.publisher.browser import TestRequest
-	
-	  >>> from zope.schema import Field
-	  >>> import re
-	  >>> isFriendly=re.compile(".*hello.*").match
-	  >>> field = Field(__name__='foo', title=u'Foo', constraint=isFriendly)
-	  >>> request = TestRequest(form={
-	  ...     'field.foo': u'hello\\r\\nworld',
-	  ...     'baz.foo': u'bye world'})
-	  >>> widget = BrowserWidget(field, request)
+    >>> setUp()
+
+    The class provides some basic functionality common to all browser widgets.
+
+    Browser widgets have a 'required' attribute, which indicates whether or
+    not the underlying field requires input. By default, the widget's required
+    attribute is equal to the field's required attribute:
+
+        >>> from zope.schema import Field
+        >>> from zope.publisher.browser import TestRequest
+        >>> field = Field(required=True)
+        >>> widget = BrowserWidget(field, TestRequest())
+        >>> widget.required
+        True
+        >>> field.required = False
+        >>> widget = BrowserWidget(field, TestRequest())
+        >>> widget.required
+        False
+
+    However, the two 'required' values are independent of one another:
+
+        >>> field.required = True
+        >>> widget.required
+        False
+
+    Browser widgets have an error state, which can be rendered in a form using
+    the error() method. The error method delegates the error rendering to a
+    view that is registered as providing IWidgetInputErrorView. To illustrate,
+    we can create and register a simple error display view:
+
+        >>> from zope.app.form.interfaces import IWidgetInputError
+        >>> class SnippetErrorView:
+        ...     implements(IWidgetInputErrorView)
+        ...     def __init__(self, context, request):
+        ...         self.context = context
+        ...     def snippet(self):
+        ...         return "The error: " + str(self.context.errors)
+        >>> ztapi.browserViewProviding(IWidgetInputError, SnippetErrorView,
+        ...                            IWidgetInputErrorView)
+
+    Whever an error occurs, widgets should set _error:
+
+        >>> widget._error = WidgetInputError('foo', 'Foo', ('Err1', 'Err2'))
+
+    so that it can be displayed using the error() method:
+
+        >>> widget.error()
+        "The error: ('Err1', 'Err2')"
+
+    >>> tearDown()
+
+    """
+
+    implements(IBrowserWidget)
+
+    _error = None
+
+    def __init__(self, context, request):
+        super(BrowserWidget, self).__init__(context, request)
+        self.required = context.required
+
+    def error(self):
+        if self._error:
+            return zapi.getViewProviding(self._error, IWidgetInputErrorView,
+                                         self.request).snippet()
+        return ""
+
+    def hidden(self):
+        return ""
+
+
+class SimpleInputWidget(BrowserWidget, InputWidget):
+    """A widget that uses a single HTML form element to capture user input.
+
+    >>> setUp()
+
+    Simple input widgets read input from a browser form. To illustrate, we
+    will use a test request with two form values:
+
+        >>> from zope.publisher.browser import TestRequest
+        >>> request = TestRequest(form={
+        ...     'field.foo': u'hello\\r\\nworld',
+        ...     'baz.foo': u'bye world'})
+
+    Like all widgets, simple input widgets are a view to a field context:
+
+        >>> from zope.schema import Field
+        >>> field = Field(__name__='foo', title=u'Foo')
+        >>> widget = SimpleInputWidget(field, request)
 
     Widgets are named using their field's name:
 
-      >>> widget.name
-      'field.foo'
+        >>> widget.name
+        'field.foo'
 
     The default implementation for the widget label is to use the field title:
 
-      >>> widget.label
-      u'Foo'
+        >>> widget.label
+        u'Foo'
 
     According the request, the widget has input because 'field.foo' is
     present:
 
-      >>> widget.hasInput()
-      True
-      >>> widget.getInputValue()
-      u'hello\\r\\nworld'
+        >>> widget.hasInput()
+        True
+        >>> widget.getInputValue()
+        u'hello\\r\\nworld'
 
     Widgets maintain an error state, which is used to communicate invalid
     input or other errors:
 
-      >>> widget._error is None
-      True
-      >>> widget.error()
-      ''
+        >>> widget._error is None
+        True
+        >>> widget.error()
+        ''
 
     setRenderedValue is used to specify the value displayed by the widget to
     the user. This value, however, is not the same as the input value, which
     is read from the request:
 
-      >>> widget.setRenderedValue('Hey\\nfolks')
-      >>> widget.getInputValue()
-      u'hello\\r\\nworld'
-      >>> widget._error is None
-      True
-      >>> widget.error()
-      ''
+        >>> widget.setRenderedValue('Hey\\nfolks')
+        >>> widget.getInputValue()
+        u'hello\\r\\nworld'
+        >>> widget._error is None
+        True
+        >>> widget.error()
+        ''
 
     You can modify the prefix used to create the widget name as follows:
 
-      >>> widget.setPrefix('baz')
-      >>> widget.name
-      'baz.foo'
-
-    The modification of the widget's name changes the input the widget reads
-    from the request. Instead of reading input from 'field.foo', the widget
-    now reads input from 'baz.foo'. In this case, the input from 'baz.foo'
-    (see request above) violates the isFriendly constraint on field 'foo':
-
-      >>> widget.error()
-      ''
-      >>> try:
-      ...     widget.getInputValue()
-      ... except WidgetInputError:
-      ...     print widget._error.errors
-      bye world
-      >>> widget.error()
-      u'bye world'
-      >>> widget._error = None # clean up for next round of tests
-
-    Changing the widget prefix/name, again, changes the input the widget uses
-    from the request:
-
-      >>> widget.setPrefix('test')
-      >>> widget.name
-      'test.foo'
-      >>> widget._error is None
-      True
-      >>> widget.error()
-      ''
-      >>> widget.hasInput()
-      False
-      >>> widget.getInputValue()
-      Traceback (most recent call last):
-      ...
-      MissingInputError: ('test.foo', u'Foo', None)
-
-    Whether or not the widget requires input depends on its field's required
-    attribute:
-
-      >>> field.required
-      True
-      >>> widget.required
-      True
-      >>> field.required = False
-      >>> widget.request.form['test.foo'] = u''
-      >>> widget.required
-      False
-      >>> widget.getInputValue() == field.missing_value
-      True
-      >>> widget._error is None
-      True
-      >>> widget.error()
-      ''
+        >>> widget.setPrefix('baz')
+        >>> widget.name
+        'baz.foo'
+
+    getInputValue always returns a value that can legally be assigned to
+    the widget field. To illustrate widget validation, we can add a constraint
+    to its field:
+
+        >>> import re
+        >>> field.constraint = re.compile(".*hello.*").match
+
+    Because we modified the widget's name, the widget will now read different
+    different form input:
+
+        >>> request.form[widget.name]
+        u'bye world'
+
+    This input violates the new field constraint and therefore causes an
+    error when getInputValue is called:
+
+        >>> widget.getInputValue()
+        Traceback (most recent call last):
+        WidgetInputError: ('foo', u'Foo', bye world)
+
+    Simple input widgets require that input be available in the form request.
+    If input is not present, a MissingInputError is raised:
+
+        >>> del request.form[widget.name]
+        >>> widget.getInputValue()
+        Traceback (most recent call last):
+        MissingInputError: ('baz.foo', u'Foo', None)
+
+    A MissingInputError indicates that input is missing from the form
+    altogether. It does not indicate that the user failed to provide a value
+    for a required field. The MissingInputError above was caused by the fact
+    that the form does have any input for the widget:
+
+        >>> request.form[widget.name]
+        Traceback (most recent call last):
+        KeyError: 'baz.foo'
+
+    If a user fails to provide input for a field, the form will contain the
+    input provided by the user, namely an empty string:
+
+        >>> request.form[widget.name] = ''
+
+    In such a case, if the field is required, a WidgetInputError will be
+    raised on a call to getInputValue:
+
+        >>> field.required = True
+        >>> widget.getInputValue()
+        Traceback (most recent call last):
+        WidgetInputError: ('foo', u'Foo', )
+
+    However, if the field is not required, the empty string will be converted
+    by the widget into the field's 'missing_value' and read as a legal field
+    value:
+
+        >>> field.required = False
+        >>> widget.getInputValue() is field.missing_value
+        True
 
     >>> tearDown()
     """
 
-    implements(IBrowserWidget)
+    implements(ISimpleInputWidget)
 
     tag = u'input'
     type = u'text'
     cssClass = u''
     extra = u''
     _missing = u''
-    _error = None
-
-    required = property(lambda self: self.context.required)
 
     def hasInput(self):
         """See IWidget.hasInput.
@@ -183,16 +250,7 @@
         """
         return self.name in self.request.form
 
-    def hasValidInput(self):
-        """See IWidget."""
-        try:
-            self.getInputValue()
-            return True
-        except WidgetInputError:
-            return False
-
     def getInputValue(self):
-        """See IWidget."""
         self._error = None
         field = self.context
 
@@ -202,7 +260,7 @@
             raise MissingInputError(self.name, self.label, None)
 
         # convert input to suitable value - may raise conversion error
-        value = self._convert(input)
+        value = self._toFieldValue(input)
 
         # allow missing values only for non-required fields
         if value == field.missing_value and not field.required:
@@ -217,9 +275,6 @@
             raise self._error
         return value
 
-    def validate(self):
-        self.getInputValue()
-
     def applyChanges(self, content):
         field = self.context
         value = self.getInputValue()
@@ -229,7 +284,7 @@
         else:
             return False
 
-    def _convert(self, input):
+    def _toFieldValue(self, input):
         """Converts input to a value appropriate for the field type.
 
         Widgets for non-string fields should override this method to
@@ -246,7 +301,7 @@
         else:
             return input
 
-    def _unconvert(self, value):
+    def _toFormValue(self, value):
         """Converts a field value to a string used as an HTML form value.
 
         This method is used in the default rendering of widgets that can
@@ -259,9 +314,8 @@
         else:
             return value
 
-    def _showData(self):
-        """Returns a value suitable for use as an HTML form value."""
-
+    def _getFormValue(self):
+        """Returns a value suitable for use in an HTML form."""
         if not self._renderedValueSet():
             if self.hasInput():
                 try:
@@ -272,11 +326,10 @@
                 value = self._getDefault()
         else:
             value = self._data
-
-        return self._unconvert(value)
+        return self._toFormValue(value)
 
     def _getDefault(self):
-        """Return the default value for this widget."""
+        """Returns the default value for this widget."""
         return self.context.default
 
     def __call__(self):
@@ -284,7 +337,7 @@
                              type=self.type,
                              name=self.name,
                              id=self.name,
-                             value=self._showData(),
+                             value=self._getFormValue(),
                              cssClass=self.cssClass,
                              extra=self.extra)
 
@@ -293,34 +346,18 @@
                              type='hidden',
                              name=self.name,
                              id=self.name,
-                             value=self._showData(),
+                             value=self._getFormValue(),
                              cssClass=self.cssClass,
                              extra=self.extra)
 
-    def render(self, value):
-        warn("The widget render method is deprecated",
-            DeprecationWarning, 2)
-
-        self.setRenderedValue(value)
-        return self()
-
-    def renderHidden(self, value):
-        warn("The widget render method is deprecated",
-            DeprecationWarning, 2)
-        self.setRenderedValue(value)
-        return self.hidden()
-
-    def error(self):
-        if self._error:
-            return zapi.getViewProviding(self._error, IWidgetInputErrorView,
-                                         self.request).snippet()
-        return ""
-
-
 class DisplayWidget(BrowserWidget):
 
     def __call__(self):
-        return self._showData()
+        if self._renderedValueSet():
+            return self._data
+        else:
+            return self.context.default
+
 
 # XXX Note, some HTML quoting is needed in renderTag and renderElement.
 def renderTag(tag, **kw):




More information about the Zope3-Checkins mailing list