[Zope3-checkins] SVN: zope.formlib/branches/faassen-zaf/src/zope/formlib/ No more imports from zope.app.form.

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


Log message for revision 107381:
  No more imports from zope.app.form.
  

Changed:
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/boolwidgets.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/bugs.txt
  A   zope.formlib/branches/faassen-zaf/src/zope/formlib/exception.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/i18n.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/objectwidget.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/tests.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/textwidgets.py
  A   zope.formlib/branches/faassen-zaf/src/zope/formlib/utility.py

-=-
Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/boolwidgets.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/boolwidgets.py	2009-12-30 19:49:25 UTC (rev 107380)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/boolwidgets.py	2009-12-30 20:10:41 UTC (rev 107381)
@@ -23,8 +23,8 @@
 from zope.formlib.widget import SimpleInputWidget, renderElement
 from zope.formlib.widget import DisplayWidget
 from zope.formlib.i18n import _
-from zope.app.form.browser.itemswidgets import RadioWidget
-from zope.app.form.browser.itemswidgets import SelectWidget, DropdownWidget
+from zope.formlib.itemswidgets import RadioWidget
+from zope.formlib.itemswidgets import SelectWidget, DropdownWidget
 from zope.formlib.interfaces import IInputWidget
 
 

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/bugs.txt
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/bugs.txt	2009-12-30 19:49:25 UTC (rev 107380)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/bugs.txt	2009-12-30 20:10:41 UTC (rev 107381)
@@ -34,7 +34,7 @@
 >>> form.widgets['title']
 <zope.formlib.widget.DisplayWidget object at 0x...>
 >>> form.widgets['name']
-<zope.app.form.browser.textwidgets.TextWidget object at 0x...>
+<zope.formlib.textwidgets.TextWidget object at 0x...>
 
 
 Make sure we have the same behaviour for non-edit forms:
@@ -49,7 +49,7 @@
 >>> form.widgets['title']
 <zope.formlib.widget.DisplayWidget object at 0x...>
 >>> form.widgets['name']
-<zope.app.form.browser.textwidgets.TextWidget object at 0x...>
+<zope.formlib.textwidgets.TextWidget object at 0x...>
 
 
 

Copied: zope.formlib/branches/faassen-zaf/src/zope/formlib/exception.py (from rev 107371, zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/exception.py)
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/exception.py	                        (rev 0)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/exception.py	2009-12-30 20:10:41 UTC (rev 107381)
@@ -0,0 +1,60 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Form-related exception views
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+from cgi import escape
+
+from zope.interface import implements
+from zope.i18n import translate
+
+from zope.formlib.interfaces import IWidgetInputError, 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)

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/i18n.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/i18n.py	2009-12-30 19:49:25 UTC (rev 107380)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/i18n.py	2009-12-30 20:10:41 UTC (rev 107381)
@@ -1,5 +1,5 @@
 """\
-I18N support for zope.app.form.browser.
+I18N support for zope.formlib
 
 """
 __docformat__ = "reStructuredText"

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/objectwidget.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/objectwidget.py	2009-12-30 19:49:25 UTC (rev 107380)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/objectwidget.py	2009-12-30 20:10:41 UTC (rev 107381)
@@ -24,9 +24,9 @@
 from zope.formlib.interfaces import IInputWidget
 from zope.formlib.widget import InputWidget
 from zope.formlib.widget import BrowserWidget
-from zope.app.form.utility import setUpWidgets, applyWidgetsChanges
+from zope.formlib.utility import setUpWidgets, applyWidgetsChanges
 from zope.browserpage import ViewPageTemplateFile
-from zope.app.form.browser.interfaces import IWidgetInputErrorView
+from zope.formlib.interfaces import IWidgetInputErrorView
 
 
 class ObjectWidgetView:

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests.py	2009-12-30 19:49:25 UTC (rev 107380)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests.py	2009-12-30 20:10:41 UTC (rev 107381)
@@ -32,11 +32,11 @@
 import zope.testing.renormalizing
 import zope.traversing.adapters
 
-from zope.app.form.browser import (
+from zope.formlib.widgets import (
     TextWidget, FloatWidget, UnicodeDisplayWidget, IntWidget,
     DatetimeDisplayWidget, DatetimeWidget)
 
-from zope.app.form.browser import exception
+from zope.formlib import exception
 from zope.formlib.interfaces import IWidgetInputErrorView
 
 import zope.formlib

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/textwidgets.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/textwidgets.py	2009-12-30 19:49:25 UTC (rev 107380)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/textwidgets.py	2009-12-30 20:10:41 UTC (rev 107381)
@@ -26,7 +26,7 @@
 
 from zope.formlib.interfaces import IInputWidget, ConversionError
 from zope.formlib.i18n import _
-from zope.app.form.browser.interfaces import ITextBrowserWidget
+from zope.formlib.interfaces import ITextBrowserWidget
 from zope.formlib.widget import SimpleInputWidget, renderElement
 from zope.formlib.widget import DisplayWidget
 

Copied: zope.formlib/branches/faassen-zaf/src/zope/formlib/utility.py (from rev 107371, zope.app.form/branches/faassen-zaf/src/zope/app/form/utility.py)
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/utility.py	                        (rev 0)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/utility.py	2009-12-30 20:10:41 UTC (rev 107381)
@@ -0,0 +1,172 @@
+##############################################################################
+#
+# Copyright (c) 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Form utility functions
+
+This is an implementation only used by zope.formlib.objectwidget, not
+by the rest of the widgets in zope.formlib. We would like to keep it
+this way.
+
+At some point we would like to rewrite zope.formlib.objectwidget so it
+uses the infrastructure provided by zope.formlib itself.
+"""
+__docformat__ = 'restructuredtext'
+
+from zope import component
+from zope import security
+from zope.security.proxy import Proxy
+from zope.proxy import isProxy
+from zope.interface.interfaces import IMethod
+from zope.security.interfaces import ForbiddenAttribute, Unauthorized
+from zope.schema import getFieldsInOrder
+from zope.formlib.interfaces import IWidget
+from zope.formlib.interfaces import WidgetsError, MissingInputError
+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)
+
+
+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



More information about the Zope3-Checkins mailing list