[Zope3-checkins] CVS: Zope3/src/zope/app/form/browser -
boolwidgets.py:1.2 itemswidgets.py:1.4 objectwidget.py:1.3
sequencewidget.py:1.3 textwidgets.py:1.3
Garrett Smith
garrett at mojave-corp.com
Tue May 11 07:17:13 EDT 2004
Update of /cvs-repository/Zope3/src/zope/app/form/browser
In directory cvs.zope.org:/tmp/cvs-serv20581/src/zope/app/form/browser
Modified Files:
boolwidgets.py itemswidgets.py objectwidget.py
sequencewidget.py textwidgets.py
Log Message:
Updates of widget implementations per browser widget framework refactor.
=== Zope3/src/zope/app/form/browser/boolwidgets.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/form/browser/boolwidgets.py:1.1 Sat Apr 24 19:18:08 2004
+++ Zope3/src/zope/app/form/browser/boolwidgets.py Tue May 11 07:17:12 2004
@@ -18,13 +18,13 @@
from zope.interface import implements
from zope.schema.vocabulary import SimpleVocabulary
-from zope.app.form.browser.widget import BrowserWidget, renderElement
+from zope.app.form.browser.widget import SimpleInputWidget, renderElement
from zope.app.form.browser.itemswidgets import RadioWidget
from zope.app.form.browser.itemswidgets import SelectWidget, DropdownWidget
from zope.app.form.interfaces import IInputWidget
from zope.app.i18n import ZopeMessageIDFactory as _
-class CheckBoxWidget(BrowserWidget):
+class CheckBoxWidget(SimpleInputWidget):
"""A checkbox widget used to display Bool fields.
For more detailed documentation, including sample code, see
@@ -38,8 +38,8 @@
def __call__(self):
"""Render the widget to HTML."""
- data = self._showData()
- if data:
+ value = self._getFormValue()
+ if value:
kw = {'checked': None}
else:
kw = {}
@@ -59,14 +59,13 @@
**kw),
)
- def _convert(self, value):
+ def _toFieldValue(self, input):
"""Convert from HTML presentation to Python bool."""
- return value == 'on'
+ return input == 'on'
- def _unconvert(self, value):
+ def _toFormValue(self, value):
"""Convert from Python bool to HTML representation."""
return value and "on" or ""
- return value == 'on'
def hasInput(self):
"""Check whether the field is represented in the form."""
=== Zope3/src/zope/app/form/browser/itemswidgets.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/form/browser/itemswidgets.py:1.3 Thu May 6 12:13:41 2004
+++ Zope3/src/zope/app/form/browser/itemswidgets.py Tue May 11 07:17:12 2004
@@ -22,7 +22,7 @@
from zope.schema.interfaces import ConstraintNotSatisfied, ITitledTokenizedTerm
from zope.app import zapi
-from zope.app.form.browser.widget import BrowserWidget, renderElement
+from zope.app.form.browser.widget import SimpleInputWidget, renderElement
from zope.app.form.browser.interfaces import IVocabularyQueryView
from zope.app.form.interfaces import IInputWidget, IDisplayWidget
from zope.app.form.interfaces import WidgetInputError
@@ -32,24 +32,20 @@
# For choices, we want to make the widget a view of the field and vocabulary.
def ChoiceDisplayWidget(field, request):
- return zapi.getMultiView((field, field.vocabulary), request,
- IDisplayWidget)
+ return zapi.getMultiView((field, field.vocabulary), request, IDisplayWidget)
def ChoiceInputWidget(field, request):
- return zapi.getMultiView((field, field.vocabulary), request,
- IInputWidget)
+ return zapi.getMultiView((field, field.vocabulary), request, IInputWidget)
# for collections, we want to make the widget a view of the field and the
# value_type. If the value_type is None we may fall over. We may
# not be able to do any better than that.
def CollectionDisplayWidget(field, request):
- return zapi.getMultiView((field, field.value_type), request,
- IDisplayWidget)
+ return zapi.getMultiView((field, field.value_type), request, IDisplayWidget)
def CollectionInputWidget(field, request):
- return zapi.getMultiView((field, field.value_type), request,
- IInputWidget)
+ return zapi.getMultiView((field, field.value_type), request, IInputWidget)
# for collections of choices, we want to make the widget a view of the field,
# the value type, and the vocabulary.
@@ -75,7 +71,7 @@
return msgid
-class ItemsWidgetBase(TranslationHook, BrowserWidget):
+class ItemsWidgetBase(TranslationHook, SimpleInputWidget):
"""Convenience base class for widgets displaying items/choices."""
extra = ""
@@ -97,7 +93,7 @@
def __call__(self):
"""Render the widget to HTML."""
raise NotImplementedError(
- "__call__() must be implemented by a subclass; use _showData()")
+ "__call__() must be implemented by a subclass; use _getFormValue()")
def textForValue(self, term):
"""Extract a string from the term.
@@ -140,7 +136,7 @@
# Now let's see whether we have a valid input value
try:
- value = self._convert(input)
+ value = self._toFieldValue(input)
except ValidationError, error:
self._error = WidgetInputError(
self.context.__name__,
@@ -165,14 +161,14 @@
"""Store the ready-for-HTML value."""
self._data = value
- def _convert(self, input):
- """See BrowserWidget"""
+ def _toFieldValue(self, input):
+ """See SimpleInputWidget"""
raise NotImplementedError(
- "_convert(input) must be implemented by a subclass\n"
+ "_toFieldValue(input) must be implemented by a subclass\n"
"It may be inherited from the mix-in classes SingleDataHelper\n"
"or MultiDataHelper")
- def _showData(self):
+ def _getFormValue(self):
if self._data is self._data_marker:
# The data has not been retrieved from the form, so let's do that
if self.hasInput():
@@ -186,11 +182,6 @@
value = self._data
return value
- def _unconvert(self, value):
- """Disregard this method as suggested by BrowserWidget."""
- raise NotImplementedError(
- "vocabulary-based widgets don't use the _unconvert() method")
-
class SingleDataHelper(object):
"""Mix-in helper class for getting the term from the HTML form.
@@ -198,8 +189,7 @@
This is used when we expect a single input, i.e. the Choice field.
"""
- def _convert(self, input):
- """See BrowserWidget"""
+ def _toFieldValue(self, input):
if input:
return self.convertTokensToValues([input])[0]
else:
@@ -213,8 +203,8 @@
Choice field as value_type.
"""
- def _convert(self, input):
- """See BrowserWidget"""
+ def _toFieldValue(self, input):
+ """See SimpleInputWidget"""
if input is self._data_marker:
return []
if not isinstance(input, list):
@@ -239,7 +229,7 @@
def __call__(self):
"""See IBrowserWidget."""
- value = self._showData()
+ value = self._getFormValue()
if value is None:
return self.translate(self._messageNoValue)
else:
@@ -258,7 +248,7 @@
def __call__(self):
"""See IBrowserWidget."""
- value = self._showData()
+ value = self._getFormValue()
if value:
rendered_items = self.renderItems(value)
return renderElement(self.tag,
@@ -346,7 +336,7 @@
def __call__(self):
"""See IBrowserWidget."""
- value = self._showData()
+ value = self._getFormValue()
contents = []
have_results = False
@@ -572,7 +562,7 @@
def hidden(self):
items = []
- for item in self._showData():
+ for item in self._getFormValue():
items.append(
renderElement(self.tag,
type='hidden',
=== Zope3/src/zope/app/form/browser/objectwidget.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/form/browser/objectwidget.py:1.2 Fri May 7 15:41:32 2004
+++ Zope3/src/zope/app/form/browser/objectwidget.py Tue May 11 07:17:12 2004
@@ -19,6 +19,7 @@
from zope.schema import getFieldNamesInOrder
from zope.app.form.interfaces import IInputWidget
+from zope.app.form import InputWidget
from zope.app.form.browser.widget import BrowserWidget
from zope.app.form.utility import setUpEditWidgets, applyWidgetsChanges
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
@@ -36,7 +37,7 @@
return self.template()
-class ObjectWidget(BrowserWidget):
+class ObjectWidget(BrowserWidget, InputWidget):
"""A widget over an Interface that contains Fields.
"factory" - factory used to create content that this widget (field)
=== Zope3/src/zope/app/form/browser/sequencewidget.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/form/browser/sequencewidget.py:1.2 Thu May 6 12:13:41 2004
+++ Zope3/src/zope/app/form/browser/sequencewidget.py Tue May 11 07:17:12 2004
@@ -21,10 +21,11 @@
from zope.app import zapi
from zope.app.form.interfaces import IInputWidget
+from zope.app.form import InputWidget
from zope.app.form.browser.widget import BrowserWidget
from zope.app.i18n import ZopeMessageIDFactory as _
-class SequenceWidget(BrowserWidget):
+class SequenceWidget(BrowserWidget, InputWidget):
"""A widget baseclass for a sequence of fields.
subwidget - Optional CustomWidget used to generate widgets for the
=== Zope3/src/zope/app/form/browser/textwidgets.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/form/browser/textwidgets.py:1.2 Wed Mar 17 19:49:07 2004
+++ Zope3/src/zope/app/form/browser/textwidgets.py Tue May 11 07:17:12 2004
@@ -18,11 +18,13 @@
from zope.interface import implements
from zope.app.form.interfaces import IInputWidget, ConversionError
-from zope.app.form.browser.widget import BrowserWidget, renderElement
+from zope.app.form.browser.interfaces import ITextBrowserWidget
+from zope.app.form.browser.widget import SimpleInputWidget, renderElement
from zope.app.datetimeutils import parseDatetimetz
from zope.app.datetimeutils import DateTimeError
-class TextWidget(BrowserWidget):
+
+class TextWidget(SimpleInputWidget):
"""Text widget.
Single-line text (unicode) input
@@ -90,17 +92,15 @@
value="<h1>&copy;</h1>"
/>
"""
-
- implements(IInputWidget)
+
+ implements(ITextBrowserWidget)
default = ''
displayWidth = 20
displayMaxWidth = ""
extra = ''
- # XXX Alex Limi doesn't like this!
- # style = "width:100%"
style = ''
- __values = None
+ convert_missing_value = True
def __init__(self, *args):
super(TextWidget, self).__init__(*args)
@@ -112,7 +112,7 @@
type=self.type,
name=self.name,
id=self.name,
- value=self._showData(),
+ value=self._getFormValue(),
cssClass=self.cssClass,
style=self.style,
size=self.displayWidth,
@@ -123,31 +123,31 @@
type=self.type,
name=self.name,
id=self.name,
- value=self._showData(),
+ value=self._getFormValue(),
cssClass=self.cssClass,
style=self.style,
size=self.displayWidth,
extra=self.extra)
- def _convert(self, value):
- value = super(TextWidget, self)._convert(value)
- if value:
- value = decode_html(value)
- return value
+ def _toFieldValue(self, input):
+ if self.convert_missing_value and input == self._missing:
+ value = self.context.missing_value
+ else:
+ value = input
+ return decode_html(value)
-class Bytes(BrowserWidget):
+class Bytes(SimpleInputWidget):
- def _convert(self, value):
- value = super(Bytes, self)._convert(value)
+ def _toFieldValue(self, input):
+ value = super(Bytes, self)._toFieldValue(input)
if type(value) is unicode:
try:
value = value.encode('ascii')
except UnicodeError, v:
raise ConversionError("Invalid textual data", v)
-
return value
-
+
class BytesWidget(Bytes, TextWidget):
"""Bytes widget.
@@ -163,11 +163,11 @@
True
>>> widget.getInputValue()
'Bob'
- """
+ """
class ASCII(Bytes):
"""ASCII"""
-
+
class ASCIIWidget(BytesWidget):
"""ASCII widget.
@@ -175,7 +175,7 @@
Single-line data (string) input
"""
-class TextAreaWidget(BrowserWidget):
+class TextAreaWidget(SimpleInputWidget):
"""TextArea widget.
Multi-line text (unicode) input.
@@ -239,26 +239,23 @@
name="field.foo"
rows="15"
><h1>&copy;</h1></textarea>
-
"""
- implements(IInputWidget)
-
default = ""
width = 60
height = 15
extra = ""
style = ''
- def _convert(self, value):
- value = super(TextAreaWidget, self)._convert(value)
+ def _toFieldValue(self, value):
+ value = super(TextAreaWidget, self)._toFieldValue(value)
if value:
- value = value.replace("\r\n", "\n")
value = decode_html(value)
+ value = value.replace("\r\n", "\n")
return value
- def _unconvert(self, value):
- value = super(TextAreaWidget, self)._unconvert(value)
+ def _toFormValue(self, value):
+ value = super(TextAreaWidget, self)._toFormValue(value)
if value:
value = value.replace("\n", "\r\n")
value = encode_html(value)
@@ -272,7 +269,7 @@
rows=self.height,
cols=self.width,
style=self.style,
- contents=self._showData(),
+ contents=self._getFormValue(),
extra=self.extra)
class BytesAreaWidget(Bytes, TextAreaWidget):
@@ -289,11 +286,11 @@
True
>>> widget.getInputValue()
'Hello\\nworld!'
- """
+ """
class PasswordWidget(TextWidget):
"""Password Widget"""
-
+
type = 'password'
def __call__(self):
@@ -326,7 +323,7 @@
class FileWidget(TextWidget):
"""File Widget"""
-
+
type = 'file'
def __call__(self):
@@ -369,16 +366,16 @@
return False
- def _convert(self, value):
+ def _toFieldValue(self, input):
try:
- seek = value.seek
- read = value.read
+ seek = input.seek
+ read = input.read
except AttributeError, e:
- raise ConversionError('Value is not a file object', e)
+ raise ConversionError('Form input is not a file object', e)
else:
seek(0)
data = read()
- if data or getattr(value, 'filename', ''):
+ if data or getattr(input, 'filename', ''):
return data
else:
return self.context.missing_value
@@ -386,44 +383,44 @@
class IntWidget(TextWidget):
displayWidth = 10
- def _convert(self, value):
- if value == self._missing:
+ def _toFieldValue(self, input):
+ if input == self._missing:
return self.context.missing_value
else:
try:
- return int(value)
+ return int(input)
except ValueError, v:
raise ConversionError("Invalid integer data", v)
-
+
class FloatWidget(TextWidget):
implements(IInputWidget)
displayWidth = 10
- def _convert(self, value):
- if value == self._missing:
+ def _toFieldValue(self, input):
+ if input == self._missing:
return self.context.missing_value
else:
try:
- return float(value)
+ return float(input)
except ValueError, v:
raise ConversionError("Invalid floating point data", v)
-
+
class DatetimeWidget(TextWidget):
"""Datetime entry widget."""
displayWidth = 20
- def _convert(self, value):
- if value == self._missing:
+ def _toFieldValue(self, input):
+ if input == self._missing:
return self.context.missing_value
else:
try:
- return parseDatetimetz(value)
+ return parseDatetimetz(input)
except (DateTimeError, ValueError, IndexError), v:
raise ConversionError("Invalid datetime data", v)
-
+
class DateWidget(TextWidget):
"""Date entry widget.
@@ -431,26 +428,29 @@
displayWidth = 20
- def _convert(self, value):
- if value == self._missing:
+ def _toFieldValue(self, input):
+ if input == self._missing:
return self.context.missing_value
else:
try:
- return parseDatetimetz(value).date()
+ return parseDatetimetz(input).date()
except (DateTimeError, ValueError, IndexError), v:
raise ConversionError("Invalid datetime data", v)
def encode_html(text):
- text = text.replace('&', '&')
- text = text.replace('<', '<')
- text = text.replace('>', '>')
- text = text.replace('"', '"')
+ if text:
+ text = text.replace('&', '&')
+ text = text.replace('<', '<')
+ text = text.replace('>', '>')
+ text = text.replace('"', '"')
return text
+
def decode_html(text):
- text = text.replace('&', '&')
- text = text.replace('<', '<')
- text = text.replace('>', '>')
- text = text.replace('"', '"')
+ if text:
+ text = text.replace('&', '&')
+ text = text.replace('<', '<')
+ text = text.replace('>', '>')
+ text = text.replace('"', '"')
return text
More information about the Zope3-Checkins
mailing list