[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form - add.py:1.5 editview.py:1.5 widget.py:1.7
Jim Fulton
jim@zope.com
Thu, 9 Jan 2003 09:13:37 -0500
Update of /cvs-repository/Zope3/src/zope/app/browser/form
In directory cvs.zope.org:/tmp/cvs-serv14386/src/zope/app/browser/form
Modified Files:
add.py editview.py widget.py
Log Message:
- Got rid of (essentially) unused validateMapping, validateMappingAll,
ValidationErrorsAll and ConversionErrorsAll.
- Renamed zope.app.interfaces.forms to zope.app.interfaces.form
- Changed the way form input errors are handled. Now, *only*
validation errors and widget input errors are caught and displayed
on forms. These errors are about data entry problems. For these, it
never makes sense to show a traceback.
For Python programming errors, we want tracebacks, so it's important
to handle them differently, but letting the propigate and get logged
by the error reporting service.
This required updating the widget code to be more careful about
errors raised.
=== Zope3/src/zope/app/browser/form/add.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/browser/form/add.py:1.4 Mon Dec 30 09:02:53 2002
+++ Zope3/src/zope/app/browser/form/add.py Thu Jan 9 09:13:04 2003
@@ -16,9 +16,12 @@
"""
import sys
+
+from zope.schema.interfaces import ValidationError
+
from zope.app.event import publish
from zope.app.event.objectevent import ObjectCreatedEvent
-from zope.app.interfaces.forms import WidgetsError
+from zope.app.interfaces.form import WidgetsError
from zope.app.form.utility import setUpWidgets, getWidgetsData
from zope.app.form.utility import haveWidgetsData, fieldNames
from zope.configuration.action import Action
@@ -67,9 +70,7 @@
if name in data:
try:
setattr(content, name, data[name])
- except:
- # Yes, I want a bare except here to catch all errors and
- # include them in the error list
+ except ValidationError:
errors.append(sys.exc_info()[1])
if errors:
@@ -77,20 +78,13 @@
publish(content, ObjectCreatedEvent(content))
- try:
- content = self.context.add(content)
- except:
- errors.append(sys.exc_info()[1])
- raise WidgetsError(*errors)
-
+ content = self.context.add(content)
for name in self._set_after_add:
if name in data:
try:
setattr(content, name, data[name])
- except:
- # Yes, I want a bare except here to catch all errors and
- # include them in the error list
+ except ValidationError:
errors.append(sys.exc_info()[1])
if errors:
@@ -106,9 +100,6 @@
content = self.apply_update(data)
except WidgetsError, errors:
self.errors = errors
- return u"An error occured."
- except Exception, v:
- self.errors = (v, )
return u"An error occured."
self.request.response.redirect(self.context.nextURL())
=== Zope3/src/zope/app/browser/form/editview.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/browser/form/editview.py:1.4 Mon Dec 30 18:27:47 2002
+++ Zope3/src/zope/app/browser/form/editview.py Thu Jan 9 09:13:04 2003
@@ -16,10 +16,13 @@
"""
from datetime import datetime
+
+from zope.schema.interfaces import ValidationError
+
from zope.app.event import publish
from zope.app.event.objectevent import ObjectModifiedEvent
from zope.publisher.browser import BrowserView
-from zope.app.interfaces.forms import WidgetsError
+from zope.app.interfaces.form import WidgetsError
from zope.app.form.utility import setUpEditWidgets, getWidgetsData
from zope.app.form.utility import haveWidgetsData, fieldNames
from zope.configuration.action import Action
@@ -85,17 +88,15 @@
# it's the same.
change = True
- try:
- # Use self as a marker
- change = getattr(content, name, self) != newvalue
- except:
- pass
+
+ # Use self as a marker
+ change = getattr(content, name, self) != newvalue
if change:
setattr(content, name, data[name])
unchanged = False
- except Exception, v:
+ except ValidationError, v:
errors.append(v)
if errors:
@@ -115,9 +116,6 @@
unchanged = self.apply_update(data)
except WidgetsError, errors:
self.errors = errors
- return u"An error occured."
- except Exception, v:
- self.errors = (v, )
return u"An error occured."
else:
setUpEditWidgets(self, self.schema, force=1,
=== Zope3/src/zope/app/browser/form/widget.py 1.6 => 1.7 ===
--- Zope3/src/zope/app/browser/form/widget.py:1.6 Fri Jan 3 10:52:30 2003
+++ Zope3/src/zope/app/browser/form/widget.py Thu Jan 9 09:13:04 2003
@@ -24,8 +24,8 @@
from zope.publisher.browser import BrowserView
from zope.app.interfaces.browser.form import IBrowserWidget
from zope.app.form.widget import Widget
-from zope.app.interfaces.forms import ConversionError, WidgetInputError
-from zope.app.interfaces.forms import MissingInputError
+from zope.app.interfaces.form import ConversionError, WidgetInputError
+from zope.app.interfaces.form import MissingInputError
from zope.schema.interfaces import ValidationError
@@ -58,16 +58,7 @@
'the field is required')
return field.default
- try:
- value = self._convert(value)
- except ConversionError:
- # Already have right error type
- raise
- except:
- # Convert to conversion error
- exc = ConversionError(sys.exc_info()[1])
- raise ConversionError, exc, sys.exc_info()[2]
-
+ value = self._convert(value)
if value is not None or not optional:
try:
@@ -283,7 +274,10 @@
value = super(Bytes, self)._convert(value)
if type(value) is unicode:
- value = value.encode('ascii')
+ try:
+ value = value.encode('ascii')
+ except UnicodeError, v:
+ raise ConversionError("Invalid textual data", v)
return value
@@ -293,12 +287,23 @@
class IntWidget(TextWidget):
displayWidth = 10
- _convert = int
+ def _convert(self, value):
+ if value:
+ try:
+ return int(value)
+ except ValueError, v:
+ raise ConversionError("Invalid integer data", v)
+
class FloatWidget(TextWidget):
displayWidth = 10
- _convert = float
+ def _convert(self, value):
+ if value:
+ try:
+ return float(value)
+ except ValueError, v:
+ raise ConversionError("Invalid floating point data", v)
class TextAreaWidget(PossiblyEmptyMeansMissing, BrowserWidget):
"""Textarea widget."""
@@ -362,22 +367,32 @@
def haveData(self):
file = self.request.form.get(self.name)
if file is None:
- return 0
+ return False
+
if getattr(file, 'filename', ''):
- return 1
+ return True
- file.seek(0)
- if file.read(1):
- return 1
- return 0
+ try:
+ seek = file.seek
+ read = file.read
+ except AttributeError, e:
+ return False
+
+ seek(0)
+ if read(1):
+ return True
+
+ return False
def _convert(self, value):
try:
- value.seek(0)
- data = value.read()
- except Exception, e:
- raise ConversionError('Value is not a file object', e)
+ seek = value.seek
+ read = value.read
+ except AttributeError, e:
+ raise ConversionError('Value is not a file object', e)
else:
+ seek(0)
+ data = read()
if data or getattr(value, 'filename', ''):
return data
else: