[Zope3-checkins] CVS: Zope3/src/zope/app/form - utility.py:1.9
R. David Murray
bitz@bitdance.com
Mon, 27 Jan 2003 23:48:40 -0500
Update of /cvs-repository/Zope3/src/zope/app/form
In directory cvs.zope.org:/tmp/cvs-serv23066/zope/app/form
Modified Files:
utility.py
Log Message:
Factored out this common pattern of getting the Fields from the schema
or using all of the Fields from the schema by default into an
internal function _fieldlist. Added some more tests to make sure
haveWidgetsData and getWidgetsData do the right thing on when 'names'
are passed in. Also fixed the docstring on haveWidgetsData.
=== Zope3/src/zope/app/form/utility.py 1.8 => 1.9 ===
--- Zope3/src/zope/app/form/utility.py:1.8 Mon Jan 27 23:06:53 2003
+++ Zope3/src/zope/app/form/utility.py Mon Jan 27 23:48:08 2003
@@ -42,6 +42,10 @@
from zope.app.interfaces.form import InputErrors
from zope.component.interfaces import IViewFactory
+def _fieldlist(names, schema):
+ if not names: fields = getFieldsInOrder(schema)
+ else: fields = [ (name, schema[name]) for name in names ]
+ return fields
def setUpWidget(view, name, field, value=None, prefix=None,
force=0, vname=None):
@@ -95,9 +99,7 @@
"""Set up widgets for the fields defined by a schema
"""
- if not names: fields = getFieldsInOrder(schema)
- else: fields = [ (name, schema[name]) for name in names ]
- for (name, field) in fields:
+ for (name, field) in _fieldlist(names, schema):
setUpWidget(view, name, field, initial.get(name),
prefix=prefix, force=force)
@@ -113,10 +115,7 @@
if content is None:
content = view.context
- if not names: fields = getFieldsInOrder(schema)
- else: fields = [ (name, schema[name]) for name in names ]
-
- for name, field in fields:
+ for name, field in _fieldlist(names, schema):
if field.readonly:
vname = 'display'
else:
@@ -133,20 +132,15 @@
prefix = prefix, force = force, vname = vname)
def haveWidgetsData(view, schema, names=None):
- """Collect the user-entered data defined by a schema
-
- Data is collected from view widgets. For every field in the
- schema, we look for a view of the same name and get it's data.
+ """Check if we have any user-entered data defined by a schema
- The data are returned in a mapping from field name to value.
+ Returns true if any schema field related widget has data
+ that was entered by the user.
"""
-
- for name in (names or schema):
- field = schema[name]
- if IField.isImplementedBy(field):
- # OK, we really got a field
- if getattr(view, name).haveData():
- return True
+
+ for name, field in _fieldlist(names, schema):
+ if getattr(view, name).haveData():
+ return True
return False
@@ -167,19 +161,16 @@
result = {}
errors = []
- for name in (names or schema):
- field = schema[name]
- if IField.isImplementedBy(field):
- # OK, we really got a field
- widget = getattr(view, name)
- if widget.haveData():
- try:
- result[name] = widget.getData()
- except InputErrors, v:
- errors.append(v)
- elif required and field.required:
- raise MissingInputError(
- widget.name, widget.title, name)
+ for name, field in _fieldlist(names, schema):
+ widget = getattr(view, name)
+ if widget.haveData():
+ try:
+ result[name] = widget.getData()
+ except InputErrors, v:
+ errors.append(v)
+ elif required and field.required:
+ raise MissingInputError(
+ widget.name, widget.title, name)
if errors:
raise WidgetsError(*errors)