[Zope3-checkins] SVN: Zope3/trunk/ Allow the <widget> subdirective without a "class" attribute.

Fred L. Drake, Jr. fdrake at gmail.com
Mon Nov 1 13:09:57 EST 2004


Log message for revision 28307:
  Allow the <widget> subdirective without a "class" attribute.
  Resolves collector issue 308:
  http://collector.zope.org/Zope3-dev/308
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/form/browser/metaconfigure.py
  U   Zope3/trunk/src/zope/app/form/browser/metadirectives.py
  A   Zope3/trunk/src/zope/app/form/browser/tests/test_widgetdirective.py
  A   Zope3/trunk/src/zope/app/form/browser/tests/widgetDirectives.zcml

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2004-10-31 18:53:38 UTC (rev 28306)
+++ Zope3/trunk/doc/CHANGES.txt	2004-11-01 18:09:56 UTC (rev 28307)
@@ -10,6 +10,11 @@
 
     New features
 
+      - Allow the <widget> subdirective to be used without specifying
+        a "class" attribute.  This allows overriding some attributes
+        for the default widget implementation without having to
+        specify the widget class itself.
+
       - Added a "debug" command to zopectl.  This loads the Zope
         configuration and provides an interactive Python session in
         which a `zope.app.debug.debug.Debugger` instance has been

Modified: Zope3/trunk/src/zope/app/form/browser/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/metaconfigure.py	2004-10-31 18:53:38 UTC (rev 28306)
+++ Zope3/trunk/src/zope/app/form/browser/metaconfigure.py	2004-11-01 18:09:56 UTC (rev 28307)
@@ -19,6 +19,8 @@
 
 import os
 
+import zope.component
+
 from zope.interface import implementedBy
 from zope.configuration.exceptions import ConfigurationError
 
@@ -28,6 +30,7 @@
 from zope.app.publisher.browser.menu import menuItemDirective
 
 from zope.app.form import CustomWidgetFactory
+from zope.app.form.interfaces import IInputWidget, IDisplayWidget
 from add import AddView, AddViewFactory
 from editview import EditView, EditViewFactory
 from addwizard import AddWizardView, AddWizardViewFactory
@@ -61,8 +64,9 @@
         self._normalize()
         self._widgets = {}
 
-    def widget(self, _context, field, class_, **kw):
+    def widget(self, _context, field, **kw):
         attrs = kw
+        class_ = attrs.pop("class_", None)
         # Try to do better than accepting the string value by looking through
         # the interfaces and trying to find the field, so that we can use
         # 'fromUnicode()' 
@@ -73,6 +77,12 @@
                     if name in iface:
                         attrs[name] = iface[name].fromUnicode(value)
                         break
+        if class_ is None:
+            # The _default_widget_factory is required to allow the
+            # <widget> directive to be given without a "class"
+            # attribute.  This can be used to override some of the
+            # presentational attributes of the widget implementation.
+            class_ = self._default_widget_factory
         self._widgets[field+'_widget'] = CustomWidgetFactory(class_, **attrs) 
 
     def _processWidgets(self):
@@ -163,6 +173,11 @@
     set_before_add = None
     set_after_add = None
 
+    def _default_widget_factory(self, field, request):
+        # `field` is a bound field
+        return zope.component.getMultiAdapter(
+            (field, request), IInputWidget)
+
     def _handle_menu(self):
         if self.menu or self.title:
             if (not self.menu) or (not self.title):
@@ -241,9 +256,21 @@
             kw={'menu': self.menu},
             )
 
-class EditFormDirective(BaseFormDirective):
+class EditFormDirectiveBase(BaseFormDirective):
 
     view = EditView
+
+    def _default_widget_factory(self, field, request):
+        # `field` is a bound field
+        if field.readonly:
+            iface = IDisplayWidget
+        else:
+            iface = IInputWidget
+        return zope.component.getMultiAdapter(
+            (field, request), iface)
+
+class EditFormDirective(EditFormDirectiveBase):
+
     default_template = 'edit.pt'
     title = 'Edit'
 
@@ -263,9 +290,8 @@
             kw={'menu': self.menu},
         )
 
-class SubeditFormDirective(BaseFormDirective):
+class SubeditFormDirective(EditFormDirectiveBase):
 
-    view = EditView
     default_template = 'subedit.pt'
 
     # default subedit form directive

Modified: Zope3/trunk/src/zope/app/form/browser/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/metadirectives.py	2004-10-31 18:53:38 UTC (rev 28306)
+++ Zope3/trunk/src/zope/app/form/browser/metadirectives.py	2004-11-01 18:09:56 UTC (rev 28307)
@@ -352,7 +352,7 @@
     class_ = GlobalObject(
         title=u"Widget Class",
         description=u"""The class that will create the widget.""",
-        required=True,
+        required=False,
         )
 
 # Arbitrary keys and values are allowed to be passed to the CustomWidget.

Added: Zope3/trunk/src/zope/app/form/browser/tests/test_widgetdirective.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_widgetdirective.py	2004-10-31 18:53:38 UTC (rev 28306)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_widgetdirective.py	2004-11-01 18:09:56 UTC (rev 28307)
@@ -0,0 +1,93 @@
+##############################################################################
+#
+# Copyright (c) 2004 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.
+#
+##############################################################################
+"""Tests for the <widget> subdirective for the generated form pages.
+
+$Id$
+"""
+import unittest
+
+import zope.interface
+import zope.configuration.xmlconfig
+import zope.publisher.browser
+import zope.schema
+
+import zope.app.container.interfaces
+import zope.app.form.browser.interfaces
+import zope.app.form.interfaces
+import zope.app.tests.placelesssetup
+
+from zope.app import zapi
+
+__docformat__ = "reStructuredText"
+
+
+class IContent(zope.interface.Interface):
+
+    field = zope.schema.TextLine(
+        title=u"Field",
+        description=u"Sample input field",
+        required=False,
+        )
+
+
+class Content(object):
+
+    zope.interface.implements(IContent)
+
+    __parent__ = None
+    __name__ = "sample-content"
+
+    field = None
+
+
+class Adding(object):
+
+    zope.interface.implements(
+        zope.app.container.interfaces.IAdding)
+
+    def add(self, content):
+        self.content = content
+
+
+class WidgetDirectiveTestCase(zope.app.tests.placelesssetup.PlacelessSetup,
+                              unittest.TestCase):
+
+    def setUp(self):
+        super(WidgetDirectiveTestCase, self).setUp()
+        zope.configuration.xmlconfig.file("widgetDirectives.zcml",
+                                          zope.app.form.browser.tests)
+
+    def get_widget(self, name, context):
+        request = zope.publisher.browser.TestRequest()
+        view = zapi.getView(context, name, request)
+        return view.field_widget
+
+    def test_addform_widget_without_class(self):
+        w = self.get_widget("add.html", Adding())
+        self.assert_(zope.app.form.interfaces.IInputWidget.providedBy(w))
+        self.assertEqual(w.extraAttr, "42")
+
+    def test_editform_widget_without_class(self):
+        w = self.get_widget("edit.html", Content())
+        self.assert_(zope.app.form.interfaces.IInputWidget.providedBy(w))
+        self.assertEqual(w.extraAttr, "84")
+
+    def test_subeditform_widget_without_class(self):
+        w = self.get_widget("subedit.html", Content())
+        self.assert_(zope.app.form.interfaces.IInputWidget.providedBy(w))
+        self.assertEqual(w.extraAttr, "168")
+
+
+def test_suite():
+    return unittest.makeSuite(WidgetDirectiveTestCase)


Property changes on: Zope3/trunk/src/zope/app/form/browser/tests/test_widgetdirective.py
___________________________________________________________________
Name: svn:mime-type
   + text/x-python
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/form/browser/tests/widgetDirectives.zcml
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/widgetDirectives.zcml	2004-10-31 18:53:38 UTC (rev 28306)
+++ Zope3/trunk/src/zope/app/form/browser/tests/widgetDirectives.zcml	2004-11-01 18:09:56 UTC (rev 28307)
@@ -0,0 +1,51 @@
+<configure xmlns="http://namespaces.zope.org/browser"
+           xmlns:zope="http://namespaces.zope.org/zope"
+           i18n_domain="zope">
+
+  <include package="zope.app.component" file="meta.zcml" />
+  <include package="zope.app.publisher" file="meta.zcml" />
+  <include package="zope.app.security" file="meta.zcml" />
+  <include package="zope.app.form.browser" file="meta.zcml" />
+
+  <zope:view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.schema.interfaces.ITextLine"
+      provides="zope.app.form.interfaces.IInputWidget"
+      factory="zope.app.form.browser.TextWidget"
+      permission="zope.Public"
+      />
+
+  <addform
+      schema=".test_widgetdirective.IContent"
+      label="Add Content"
+      name="add.html"
+      permission="zope.Public"
+      >
+    <widget field="field"
+            extraAttr="42"
+            />
+  </addform>
+
+  <editform
+      schema=".test_widgetdirective.IContent"
+      label="Edit Content"
+      name="edit.html"
+      permission="zope.Public"
+      >
+    <widget field="field"
+            extraAttr="84"
+            />
+  </editform>
+
+  <editform
+      schema=".test_widgetdirective.IContent"
+      label="Edit Content"
+      name="subedit.html"
+      permission="zope.Public"
+      >
+    <widget field="field"
+            extraAttr="168"
+            />
+  </editform>
+
+</configure>


Property changes on: Zope3/trunk/src/zope/app/form/browser/tests/widgetDirectives.zcml
___________________________________________________________________
Name: svn:mime-type
   + text/xml
Name: svn:eol-style
   + native



More information about the Zope3-Checkins mailing list