[Zope3-checkins]
SVN: Zope3/branches/philikon-widget-subdirective/src/zope/app/form/__init__.py
- Introduce CustomVocabularyFactory that takes care of
creating vocabulary
Philipp von Weitershausen
philikon at philikon.de
Sun Jun 12 10:19:29 EDT 2005
Log message for revision 30764:
- Introduce CustomVocabularyFactory that takes care of creating vocabulary
widgets (widgets for Choice)
- Change the __call__ signature of CustomSequenceWidgetFactory to be in line
with CustomWidgetFactory. All widget factories comply with the IViewFactory
interface, which means they are called with (context, request) as args.
Changed:
U Zope3/branches/philikon-widget-subdirective/src/zope/app/form/__init__.py
-=-
Modified: Zope3/branches/philikon-widget-subdirective/src/zope/app/form/__init__.py
===================================================================
--- Zope3/branches/philikon-widget-subdirective/src/zope/app/form/__init__.py 2005-06-12 14:17:04 UTC (rev 30763)
+++ Zope3/branches/philikon-widget-subdirective/src/zope/app/form/__init__.py 2005-06-12 14:19:29 UTC (rev 30764)
@@ -17,10 +17,11 @@
"""
__docformat__ = 'restructuredtext'
-from zope.app.form.interfaces import IWidget, InputErrors
from zope.component.interfaces import IViewFactory
from zope.interface import implements
from zope.i18n import translate
+from zope.schema.interfaces import IChoice, ICollection
+from zope.app.form.interfaces import IWidget, InputErrors
class Widget(object):
"""Mixin class providing functionality common across widget types."""
@@ -92,25 +93,29 @@
self.args = args
self.kw = kw
- def __call__(self, context, request):
- args = (context, request) + self.args
+ def _create(self, args):
instance = self._widget_factory(*args)
for name, value in self.kw.items():
setattr(instance, name, value)
return instance
-class CustomSequenceWidgetFactory(object):
- """Custom Widget Factory."""
- implements(IViewFactory)
+ def __call__(self, context, request):
+ return self._create((context, request) + self.args)
- def __init__(self, widget_factory, *args, **kw):
- self._widget_factory = widget_factory
- self.args = args
- self.kw = kw
+class CustomSequenceWidgetFactory(CustomWidgetFactory):
+ """Custom sequence widget factory."""
- def __call__(self, context, field, request):
- args = (context, field, request) + 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):
+ if not ICollection.providedBy(context):
+ raise TypeError, "Provided field does not provide ICollection."
+ args = (context, context.value_type, request) + self.args
+ return self._create(args)
+
+class CustomVocabularyWidgetFactory(CustomWidgetFactory):
+ """Custom vocabulary widget factory."""
+
+ def __call__(self, context, request):
+ if not IChoice.providedBy(context):
+ raise TypeError, "Provided field does not provide ICollection."
+ args = (context, context.vocabulary, request) + self.args
+ return self._create(args)
More information about the Zope3-Checkins
mailing list