1
##############################################################################
3
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
13
##############################################################################
14
"""Generic Widget base classes
16
$Id: __init__.py 71638 2006-12-20 23:34:35Z jacobholm $
18
__docformat__ = 'restructuredtext'
20
from zope.app.form.interfaces import IWidget, InputErrors
21
from zope.cachedescriptors.property import readproperty
22
from zope.component.interfaces import IViewFactory
23
from zope.deprecation import deprecated
24
from zope.interface import implements
25
from zope.i18n import translate
26
from zope.schema.interfaces import IChoice, ICollection
29
"""Mixin class providing functionality common across widget types."""
34
_data_marker = object()
39
def __init__(self, context, request):
40
self.context = context
41
self.request = request
42
self.name = self._prefix + context.__name__
48
This read-write attribute defaults to the title of the context.
50
return self.context.title
54
"""A hint regarding the use of the widget.
56
This read-write attribute defaults to the description of the context.
58
return self.context.description
60
def _translate(self, text):
61
return translate(text, context=self.request, default=text)
63
def _renderedValueSet(self):
64
"""Returns ``True`` if the the widget's rendered value has been set.
66
This is a convenience method that widgets can use to check whether
67
or not `setRenderedValue` was called.
69
return self._data is not self._data_marker
71
def setPrefix(self, prefix):
72
if prefix and not prefix.endswith("."):
75
self.name = prefix + self.context.__name__
77
def setRenderedValue(self, value):
81
class InputWidget(Widget):
82
"""Mixin class providing some default input widget methods."""
84
def hasValidInput(self):
91
def applyChanges(self, content):
93
value = self.getInputValue()
94
if field.query(content, self) != value:
95
field.set(content, value)
101
class CustomWidgetFactory(object):
102
"""Custom Widget Factory."""
103
implements(IViewFactory)
105
def __init__(self, widget_factory, *args, **kw):
106
self._widget_factory = widget_factory
110
def _create(self, args):
111
instance = self._widget_factory(*args)
112
for name, value in self.kw.items():
113
setattr(instance, name, value)
116
def __call__(self, context, request):
117
# Sequence widget factory
118
if ICollection.providedBy(context):
119
args = (context, context.value_type, request) + self.args
121
# Vocabulary widget factory
122
elif IChoice.providedBy(context):
123
args = (context, context.vocabulary, request) + self.args
125
# Regular widget factory
127
args = (context, request) + self.args
129
return self._create(args)