[Checkins] SVN: zope.app.form/branches/faassen-zaf/src/zope/app/form/ Move some more code over to zope.formlib.

Martijn Faassen faassen at startifact.com
Wed Dec 30 15:10:52 EST 2009


Log message for revision 107382:
  Move some more code over to zope.formlib.
  

Changed:
  U   zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/exception.py
  U   zope.app.form/branches/faassen-zaf/src/zope/app/form/tests/test_utility.py
  U   zope.app.form/branches/faassen-zaf/src/zope/app/form/utility.py

-=-
Modified: zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/exception.py
===================================================================
--- zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/exception.py	2009-12-30 20:10:41 UTC (rev 107381)
+++ zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/exception.py	2009-12-30 20:10:52 UTC (rev 107382)
@@ -15,47 +15,5 @@
 
 $Id$
 """
-__docformat__ = 'restructuredtext'
-
-from cgi import escape
-
-from zope.interface import implements
-from zope.i18n import translate
-
-from zope.formlib.interfaces import IWidgetInputError
-from zope.app.form.browser.interfaces import IWidgetInputErrorView
-
-class WidgetInputErrorView(object):
-    """Display an input error as a snippet of text."""
-    implements(IWidgetInputErrorView)
-
-    __used_for__ = IWidgetInputError
-
-    def __init__(self, context, request):
-        self.context, self.request = context, request
-
-    def snippet(self):
-        """Convert a widget input error to an html snippet
-
-        >>> from zope.formlib.interfaces import WidgetInputError
-        >>> class TooSmallError(object):
-        ...     def doc(self):
-        ...         return "Foo input < 1"
-        >>> err = WidgetInputError("foo", "Foo", TooSmallError())
-        >>> view = WidgetInputErrorView(err, None)
-        >>> view.snippet()
-        u'<span class="error">Foo input &lt; 1</span>'
-
-        The only method that IWidgetInputError promises to implement is
-        `doc()`. Therefore, other implementations of the interface should also
-        work.
-
-        >>> from zope.formlib.interfaces import ConversionError
-        >>> err = ConversionError('Could not convert to float.')
-        >>> view = WidgetInputErrorView(err, None)
-        >>> view.snippet()
-        u'<span class="error">Could not convert to float.</span>'
-        """
-        message = self.context.doc()
-        translated = translate(message, context=self.request, default=message)
-        return u'<span class="error">%s</span>' % escape(translated)
+# implementation moved to zope.formlib.exception
+from zope.formlib.exception import WidgetInputErrorView

Modified: zope.app.form/branches/faassen-zaf/src/zope/app/form/tests/test_utility.py
===================================================================
--- zope.app.form/branches/faassen-zaf/src/zope/app/form/tests/test_utility.py	2009-12-30 20:10:41 UTC (rev 107381)
+++ zope.app.form/branches/faassen-zaf/src/zope/app/form/tests/test_utility.py	2009-12-30 20:10:52 UTC (rev 107382)
@@ -524,9 +524,9 @@
             ...     print "ignoreStickyValues: %s" % ignoreStickyValues
             ...     print "context: %s" % context
             ...     print '---'
-            >>> import zope.app.form.utility
-            >>> setUpWidgetsSave = zope.app.form.utility.setUpWidget
-            >>> zope.app.form.utility.setUpWidget = setUpWidget
+            >>> import zope.formlib.utility
+            >>> setUpWidgetsSave = zope.formlib.utility.setUpWidget
+            >>> zope.formlib.utility.setUpWidget = setUpWidget
             
         When we call setUpWidgets, we should see that setUpWidget is called 
         for each field in the specified schema:
@@ -553,7 +553,7 @@
             ignoreStickyValues: True
             context: Alt Context
             ---
-            >>> zope.app.form.utility.setUpWidget = setUpWidgetsSave
+            >>> zope.formlib.utility.setUpWidget = setUpWidgetsSave
      
         >>> tearDown()
         """

Modified: zope.app.form/branches/faassen-zaf/src/zope/app/form/utility.py
===================================================================
--- zope.app.form/branches/faassen-zaf/src/zope/app/form/utility.py	2009-12-30 20:10:41 UTC (rev 107381)
+++ zope.app.form/branches/faassen-zaf/src/zope/app/form/utility.py	2009-12-30 20:10:52 UTC (rev 107382)
@@ -46,112 +46,15 @@
 from zope.formlib.interfaces import InputErrors
 from zope.formlib.interfaces import IInputWidget, IDisplayWidget
 from zope.formlib.interfaces import IWidgetFactory
-
-# A marker that indicates 'no value' for any of the utility functions that
-# accept a 'value' argument.
-no_value = object()
-
-def _fieldlist(names, schema):
-    if not names:
-        fields = getFieldsInOrder(schema)
-    else:
-        fields = [ (name, schema[name]) for name in names ]
-    return fields
-
-
-def _createWidget(context, field, viewType, request):
-    """Creates a widget given a `context`, `field`, and `viewType`."""
-    field = field.bind(context)
-    return component.getMultiAdapter((field, request), viewType)
-
-def _widgetHasStickyValue(widget):
-    """Returns ``True`` if the widget has a sticky value.
-
-    A sticky value is input from the user that should not be overridden
-    by an object's current field value. E.g. a user may enter an invalid
-    postal code, submit the form, and receive a validation error - the postal
-    code should be treated as 'sticky' until the user successfully updates
-    the object.
-    """
-    return IInputWidget.providedBy(widget) and widget.hasInput()
-
-def setUpWidget(view, name, field, viewType, value=no_value, prefix=None,
-                ignoreStickyValues=False, context=None):
-    """Sets up a single view widget.
-
-    The widget will be an attribute of the `view`. If there is already
-    an attribute of the given name, it must be a widget and it will be
-    initialized with the given `value` if not ``no_value``.
-
-    If there isn't already a `view` attribute of the given name, then a
-    widget will be created and assigned to the attribute.
-    """
-    if context is None:
-        context = view.context
-    widgetName = name + '_widget'
-
-    # check if widget already exists
-    widget = getattr(view, widgetName, None)
-    if widget is None:
-        # does not exist - create it
-        widget = _createWidget(context, field, viewType, view.request)
-        setattr(view, widgetName, widget)
-    elif IWidgetFactory.providedBy(widget):
-        # exists, but is actually a factory - use it to create the widget
-        widget = widget(field.bind(context), view.request)
-        setattr(view, widgetName, widget)
-
-    # widget must implement IWidget
-    if not IWidget.providedBy(widget):
-        raise TypeError(
-            "Unable to configure a widget for %s - attribute %s does not "
-            "implement IWidget" % (name, widgetName))
-
-    if prefix:
-        widget.setPrefix(prefix)
-
-    if value is not no_value and (
-        ignoreStickyValues or not _widgetHasStickyValue(widget)):
-        widget.setRenderedValue(value)
-
-
-def setUpWidgets(view, schema, viewType, prefix=None, ignoreStickyValues=False,
-                 initial={}, names=None, context=None):
-    """Sets up widgets for the fields defined by a `schema`.
-
-    Appropriate for collecting input without a current object implementing
-    the schema (such as an add form).
-
-    `view` is the view that will be configured with widgets.
-
-    `viewType` is the type of widgets to create (e.g. IInputWidget or
-    IDisplayWidget).
-
-    `schema` is an interface containing the fields that widgets will be
-    created for.
-
-    `prefix` is a string that is prepended to the widget names in the generated
-    HTML. This can be used to differentiate widgets for different schemas.
-
-    `ignoreStickyValues` is a flag that, when ``True``, will cause widget
-    sticky values to be replaced with the context field value or a value
-    specified in initial.
-
-    `initial` is a mapping of field names to initial values.
-
-    `names` is an optional iterable that provides an ordered list of field
-    names to use. If names is ``None``, the list of fields will be defined by
-    the schema.
-
-    `context` provides an alternative context for acquisition.
-    """
-    for (name, field) in _fieldlist(names, schema):
-        setUpWidget(view, name, field, viewType,
-                    value=initial.get(name, no_value),
-                    prefix=prefix,
-                    ignoreStickyValues=ignoreStickyValues,
-                    context=context)
-
+from zope.formlib.utility import (
+    setUpWidget,
+    setUpWidgets,
+    applyWidgetsChanges,
+    _fieldlist,
+    no_value,
+    _widgetHasStickyValue,
+    applyWidgetsChanges)
+    
 def setUpEditWidgets(view, schema, source=None, prefix=None,
                      ignoreStickyValues=False, names=None, context=None,
                      degradeInput=False, degradeDisplay=False):
@@ -278,36 +181,6 @@
             return True
     return False
 
-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 target is None:
-        target = view.context
-
-    for name, field in _fieldlist(names, schema):
-        widget = getattr(view, name + '_widget')
-        if IInputWidget.providedBy(widget) and widget.hasInput():
-            try:
-                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):
     """Returns user entered data for a set of `schema` fields.
 



More information about the checkins mailing list