[Zope3-checkins] SVN: zope.formlib/branches/faassen-zaf/src/zope/formlib/ Move over code in zope.app.form and zope.app.form.browser.interfaces

Martijn Faassen faassen at startifact.com
Wed Dec 30 14:21:00 EST 2009


Log message for revision 107375:
  Move over code in zope.app.form and zope.app.form.browser.interfaces
  to zope.formlib.widget and zope.formlib.interfaces.
  

Changed:
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/errors.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/errors.txt
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/form.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/interfaces.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/tests.py
  U   zope.formlib/branches/faassen-zaf/src/zope/formlib/widget.py

-=-
Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/errors.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/errors.py	2009-12-30 18:57:26 UTC (rev 107374)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/errors.py	2009-12-30 19:20:59 UTC (rev 107375)
@@ -24,7 +24,7 @@
 from zope.i18n import Message
 from zope.i18n import translate
 
-from zope.app.form.browser.interfaces import IWidgetInputErrorView
+from zope.formlib.interfaces import IWidgetInputErrorView
 from zope.publisher.interfaces.browser import IBrowserRequest
 
 

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/errors.txt
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/errors.txt	2009-12-30 18:57:26 UTC (rev 107374)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/errors.txt	2009-12-30 19:20:59 UTC (rev 107375)
@@ -14,7 +14,7 @@
     >>> from zope.publisher.browser import TestRequest
     >>> from zope.interface.exceptions import Invalid
     >>> from zope.component import getMultiAdapter
-    >>> from zope.app.form.browser.interfaces import IWidgetInputErrorView
+    >>> from zope.formlib.interfaces import IWidgetInputErrorView
     >>> error = Invalid("You are wrong!")
     >>> message = getMultiAdapter((error, TestRequest()),
     ...         IWidgetInputErrorView).snippet()

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/form.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/form.py	2009-12-30 18:57:26 UTC (rev 107374)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/form.py	2009-12-30 19:20:59 UTC (rev 107375)
@@ -38,7 +38,7 @@
 from zope.lifecycleevent import Attributes
 from zope.browserpage import namedtemplate
 
-import zope.app.form.browser.interfaces
+from zope.formlib.interfaces import IWidgetInputErrorView
 from zope.formlib.interfaces import IInputWidget, IDisplayWidget
 from zope.formlib.interfaces import WidgetsError, MissingInputError
 from zope.formlib.interfaces import InputErrors, WidgetInputError
@@ -795,7 +795,7 @@
             else:
                 view = component.getMultiAdapter(
                     (error, self.request),
-                    zope.app.form.browser.interfaces.IWidgetInputErrorView)
+                    IWidgetInputErrorView)
                 title = getattr(error, 'widget_title', None) # duck typing
                 if title:
                     if isinstance(title, zope.i18n.Message):

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/interfaces.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/interfaces.py	2009-12-30 18:57:26 UTC (rev 107374)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/interfaces.py	2009-12-30 19:20:59 UTC (rev 107375)
@@ -248,6 +248,52 @@
     """There was an error in managing the form
     """
 
+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 = schema.TextLine(
+        title=u'Tag',
+        description=u'The widget HTML element.')
+
+    type = schema.TextLine(
+        title=u'Type',
+        description=u'The element type attribute',
+        required=False)
+
+    cssClass = schema.TextLine(
+        title=u'CSS Class',
+        description=u'The element class attribute.',
+        required=False)
+
+    extra = schema.TextLine(
+        title=u'Extra',
+        description=u'The element extra attribute.',
+        required=False)
+
+
+class ITextBrowserWidget(ISimpleInputWidget):
+
+    convert_missing_value = schema.Bool(
+        title=u'Translate Input Value',
+        description=
+            u'If True, an empty string is converted to field.missing_value.',
+        default=True)
+
+
 def reConstraint(pat, explanation):
     pat = re.compile(pat)
 
@@ -256,6 +302,13 @@
             return True
         raise interface.Invalid(value, explanation)
 
+class IWidgetInputErrorView(Interface):
+    """Display an input error as a snippet of text."""
+
+    def snippet():
+        """Convert a widget input error to an html snippet."""
+
+
 class ISubPage(Interface):
     """A component that computes part of a page
     """

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests.py	2009-12-30 18:57:26 UTC (rev 107374)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests.py	2009-12-30 19:20:59 UTC (rev 107375)
@@ -32,11 +32,13 @@
 import zope.testing.renormalizing
 import zope.traversing.adapters
 
-import zope.app.form.browser
-import zope.app.form.browser.exception
-import zope.app.form.browser.interfaces
-import zope.formlib.interfaces
+from zope.app.form.browser import (
+    TextWidget, FloatWidget, UnicodeDisplayWidget, IntWidget,
+    DatetimeDisplayWidget, DatetimeWidget)
 
+from zope.app.form.browser import exception
+from zope.formlib.interfaces import IWidgetInputErrorView
+
 import zope.formlib
 import zope.formlib.form
 import zope.formlib.interfaces
@@ -89,74 +91,74 @@
     setUp(test)
     i18nSetUp(test)
     provideAdapter(
-        zope.app.form.browser.TextWidget,
+        TextWidget,
         [zope.schema.interfaces.ITextLine,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
         zope.formlib.interfaces.IInputWidget,
         )
     provideAdapter(
-        zope.app.form.browser.FloatWidget,
+        FloatWidget,
         [zope.schema.interfaces.IFloat,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
         zope.formlib.interfaces.IInputWidget,
         )
     provideAdapter(
-        zope.app.form.browser.UnicodeDisplayWidget,
+        UnicodeDisplayWidget,
         [zope.schema.interfaces.IInt,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
         zope.formlib.interfaces.IDisplayWidget,
         )
     provideAdapter(
-        zope.app.form.browser.IntWidget,
+        IntWidget,
         [zope.schema.interfaces.IInt,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
         zope.formlib.interfaces.IInputWidget,
         )
     provideAdapter(
-        zope.app.form.browser.UnicodeDisplayWidget,
+        UnicodeDisplayWidget,
         [zope.schema.interfaces.IFloat,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
         zope.formlib.interfaces.IDisplayWidget,
         )
     provideAdapter(
-        zope.app.form.browser.UnicodeDisplayWidget,
+        UnicodeDisplayWidget,
         [zope.schema.interfaces.ITextLine,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
         zope.formlib.interfaces.IDisplayWidget,
         )
     provideAdapter(
-        zope.app.form.browser.DatetimeDisplayWidget,
+        DatetimeDisplayWidget,
         [zope.schema.interfaces.IDatetime,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
         zope.formlib.interfaces.IDisplayWidget,
         )
     provideAdapter(
-        zope.app.form.browser.DatetimeWidget,
+        DatetimeWidget,
         [zope.schema.interfaces.IDatetime,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
         zope.formlib.interfaces.IInputWidget,
         )
     provideAdapter(
-        zope.app.form.browser.exception.WidgetInputErrorView,
+        exception.WidgetInputErrorView,
         [zope.formlib.interfaces.IWidgetInputError,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
-        zope.app.form.browser.interfaces.IWidgetInputErrorView,
+        IWidgetInputErrorView,
         )
     provideAdapter(
         zope.formlib.errors.InvalidErrorView,
         [zope.interface.Invalid,
          zope.publisher.interfaces.browser.IBrowserRequest,
          ],
-        zope.app.form.browser.interfaces.IWidgetInputErrorView,
+        IWidgetInputErrorView,
         )
     provideAdapter(TestTemplate, name='default')
     provideAdapter(requestToTZInfo)

Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/widget.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/widget.py	2009-12-30 18:57:26 UTC (rev 107374)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/widget.py	2009-12-30 19:20:59 UTC (rev 107375)
@@ -24,13 +24,16 @@
 from zope.schema.interfaces import ValidationError
 from zope.publisher.browser import BrowserView
 
-from zope.app.form import Widget, InputWidget
 from zope.formlib.interfaces import ConversionError
 from zope.formlib.interfaces import InputErrors
 from zope.formlib.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
+from zope.formlib.interfaces import IBrowserWidget
+from zope.formlib.interfaces import ISimpleInputWidget
+from zope.formlib.interfaces import IWidgetInputErrorView
+from zope.formlib.interfaces import IWidget, InputErrors, IWidgetFactory
+from zope.interface import implements
+from zope.i18n import translate
+from zope.schema.interfaces import IChoice, ICollection
 
 import warnings
 
@@ -43,6 +46,95 @@
             data, {'\n': '
', '\r': '
', '\t':'	'})
 
 
+class Widget(object):
+    """Mixin class providing functionality common across widget types."""
+
+    implements(IWidget)
+
+    _prefix = 'field.'
+    _data_marker = object()
+    _data = _data_marker
+
+    visible = True
+
+    def __init__(self, context, request):
+        self.context = context
+        self.request = request
+        self.name = self._prefix + context.__name__
+        self.label = self.context.title
+        self.hint = self.context.description
+
+    def _translate(self, text):
+        return translate(text, context=self.request, default=text)
+
+    def _renderedValueSet(self):
+        """Returns ``True`` if the the widget's rendered value has been set.
+
+        This is a convenience method that widgets can use to check whether
+        or not `setRenderedValue` was called.
+        """
+        return self._data is not self._data_marker
+
+    def setPrefix(self, prefix):
+        if prefix and not prefix.endswith("."):
+            prefix += '.'
+        self._prefix = prefix
+        self.name = prefix + self.context.__name__
+
+    def setRenderedValue(self, value):
+        self._data = value
+
+
+class InputWidget(Widget):
+    """Mixin class providing some default input widget methods."""
+
+    def hasValidInput(self):
+        try:
+            self.getInputValue()
+            return True
+        except InputErrors:
+            return False
+
+    def applyChanges(self, content):
+        field = self.context
+        value = self.getInputValue()
+        if field.query(content, self) != value:
+            field.set(content, value)
+            return True
+        else:
+            return False
+
+
+class CustomWidgetFactory(object):
+    """Custom Widget Factory."""
+    implements(IWidgetFactory)
+
+    def __init__(self, widget_factory, *args, **kw):
+        self._widget_factory = widget_factory
+        self.args = args
+        self.kw = kw
+
+    def _create(self, args):
+        instance = self._widget_factory(*args)
+        for name, value in self.kw.items():
+            setattr(instance, name, value)
+        return instance
+
+    def __call__(self, context, request):
+        # Sequence widget factory
+        if ICollection.providedBy(context):
+            args = (context, context.value_type, request) + self.args
+
+        # Vocabulary widget factory
+        elif IChoice.providedBy(context):
+            args = (context, context.vocabulary, request) + self.args
+
+        # Regular widget factory
+        else:
+            args = (context, request) + self.args
+
+        return self._create(args)
+
 class BrowserWidget(Widget, BrowserView):
     """Base class for browser widgets.
 



More information about the Zope3-Checkins mailing list