[Zope3-checkins] CVS: Zope3/src/zope/app/form/browser - meta.zcml:1.2 metaconfigure.py:1.3 metadirectives.py:1.5

Stephan Richter srichter at cosmos.phy.tufts.edu
Sun Apr 25 12:19:15 EDT 2004


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

Modified Files:
	meta.zcml metaconfigure.py metadirectives.py 
Log Message:


Implemented browser:widget subdirective.




=== Zope3/src/zope/app/form/browser/meta.zcml 1.1 => 1.2 ===
--- Zope3/src/zope/app/form/browser/meta.zcml:1.1	Sat Mar 13 20:11:34 2004
+++ Zope3/src/zope/app/form/browser/meta.zcml	Sun Apr 25 12:19:14 2004
@@ -18,6 +18,7 @@
 
     </meta:complexDirective>
 
+
     <meta:complexDirective
         name="editwizard"
         schema=".metadirectives.IEditWizardDirective"
@@ -31,23 +32,48 @@
 
     </meta:complexDirective>
 
+
     <meta:complexDirective
         name="editform"
         schema=".metadirectives.IEditFormDirective"
         handler=".metaconfigure.EditFormDirective"
-        />
+        >
+
+      <meta:subdirective
+          name="widget"
+          schema=".metadirectives.IWidgetSubdirective"
+          />
+
+    </meta:complexDirective>
+
 
     <meta:complexDirective
         name="subeditform"
         schema=".metadirectives.ISubeditFormDirective"
         handler=".metaconfigure.SubeditFormDirective"
-        />
+        >
+
+      <meta:subdirective
+          name="widget"
+          schema=".metadirectives.IWidgetSubdirective"
+          />
+
+    </meta:complexDirective>
+
 
     <meta:complexDirective
         name="addform"
         schema=".metadirectives.IAddFormDirective"
         handler=".metaconfigure.AddFormDirective"
-        />
+        >
+
+      <meta:subdirective
+          name="widget"
+          schema=".metadirectives.IWidgetSubdirective"
+          />
+
+    </meta:complexDirective>
+
 
     <meta:complexDirective
         name="schemadisplay"


=== Zope3/src/zope/app/form/browser/metaconfigure.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/form/browser/metaconfigure.py:1.2	Tue Mar 23 17:08:10 2004
+++ Zope3/src/zope/app/form/browser/metaconfigure.py	Sun Apr 25 12:19:14 2004
@@ -11,14 +11,13 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""
+"""Configuration handlers for forms and widgets
+
 $Id$
 """
-
-__metaclass__ = type
-
 import os
 
+from zope.interface import implementedBy
 from zope.configuration.exceptions import ConfigurationError
 
 from zope.schema import getFieldNamesInOrder
@@ -27,13 +26,14 @@
 from zope.app.publisher.browser.globalbrowsermenuservice import \
      menuItemDirective
 
+from zope.app.form import CustomWidgetFactory
 from add import AddView, AddViewFactory
 from editview import EditView, EditViewFactory
 from addwizard import AddWizardView, AddWizardViewFactory
 from editwizard import EditWizardView, EditWizardViewFactory
 from schemadisplay import DisplayView, DisplayViewFactory
 
-class BaseFormDirective:
+class BaseFormDirective(object):
 
     # to be overriden by the subclasses
     view = None
@@ -58,6 +58,27 @@
             if not (value is None and hasattr(self, key)):
                 setattr(self, key, value)
         self._normalize()
+        self._widgets = {}
+
+    def widget(self, _context, field, class_, **kw):
+        attrs = kw
+        ifaces = implementedBy(class_)
+        # 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()' 
+        for name, value in kw.items():
+            for iface in ifaces:
+                if name in iface:
+                    attrs[name] = iface[name].fromUnicode(value)
+                    break
+
+        self._widgets[field+'_widget'] = CustomWidgetFactory(class_, **attrs) 
+
+    def _processWidgets(self):
+        if self._widgets:
+            customWidgetsObject = type('CustomWidgetsMixin', (object,),
+                                       self._widgets) 
+            self.bases = self.bases + (customWidgetsObject,)
 
     def _normalize(self):
         if self.for_ is None:
@@ -94,7 +115,8 @@
         return ('view', self.for_, self.name, IBrowserRequest,
                 self.layer)
 
-class Pane:
+
+class Pane(object):
     ''' Holder for information about a Pane of a wizard '''
     # TODO: Add more funky stuff to each pane, such as a validator
     def __init__(self, field_names, label):
@@ -204,6 +226,7 @@
             self.set_after_add = leftover
 
     def __call__(self):
+        self._processWidgets()
         self._handle_menu()
         self._handle_arguments()
 
@@ -229,6 +252,7 @@
                 '@@' + self.name, self.title, permission=self.permission)
 
     def __call__(self):
+        self._processWidgets()
         self._handle_menu()
         self._context.action(
             discriminator=self._discriminator(),
@@ -247,6 +271,7 @@
     fulledit_label = None
 
     def __call__(self):
+        self._processWidgets()
         self._context.action(
             discriminator = self._discriminator(),
             callable = EditViewFactory,


=== Zope3/src/zope/app/form/browser/metadirectives.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/form/browser/metadirectives.py:1.4	Wed Apr 14 10:31:18 2004
+++ Zope3/src/zope/app/form/browser/metadirectives.py	Sun Apr 25 12:19:14 2004
@@ -319,3 +319,28 @@
         description=u"This attribute defaults to 'Edit'.",
         required=False
         )
+
+
+class IWidgetSubdirective(Interface):
+    """Register custom widgets for a form.
+
+    This directive allows you to quickly generate custom widget directives for
+    a form.
+    """
+
+    field = TextLine(
+        title=u"Field Name",
+        description=u"""
+        The name of the field/attribute/property for which this widget will be
+        used.""",
+        required=False
+        )
+
+    class_ = GlobalObject(
+        title=u"Widget Class",
+        description=u"""The class that will create the widget.""",
+        required=False
+        )
+
+# Arbitrary keys and values are allowed to be passed to the CustomWidget.
+IWidgetSubdirective.setTaggedValue('keyword_arguments', True)




More information about the Zope3-Checkins mailing list