[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms - Converter.py:1.4
Stephan Richter
srichter@cbu.edu
Wed, 24 Jul 2002 06:54:19 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/Forms
In directory cvs.zope.org:/tmp/cvs-serv1557/lib/python/Zope/App/Forms
Modified Files:
Converter.py
Log Message:
Made some bug fixes and some changes. "items" for the ItemWidgets no expect
a list of (value, description) instead of (description, value) as it was in
Formulator. This way we can make better use of dict.items().
=== Zope3/lib/python/Zope/App/Forms/Converter.py 1.3 => 1.4 ===
"""
$Id$
"""
-from Zope.App.Forms.IConverter import IConverter
+from types import TupleType, ListType
+ListTypes = (TupleType, ListType)
+from Schema.IConverter import IConverter
+from Schema.Converter import NullConverter
from Schema.IField import *
-class Converter:
- """ """
- __implements__ = IConverter
- def __init__(self, context):
- self.context = context
+class RawToHomogeneousListConverter(NullConverter):
+ """Converts a list of raw values to a list of values with a specific
+ type."""
- def convert(self, value):
- 'See Zope.App.Forms.IConverter.IConverter'
- return value
+ def __init__(self, type):
+ self.type = type
+ def convert(self, value):
+ result = []
+ for elem in value:
+ result.append(self.type(elem))
+ return result
+
-class FieldToFieldConverter(Converter):
+class FieldToFieldConverter(NullConverter):
""" """
__convert_from__ = IField
__convert_to__ = IField
@@ -42,28 +48,24 @@
return value
-class RequestConverter(Converter):
- """ """
- __convert_from__ = IRequest
- __convert_to__ = IStr
-
- field_prefix = 'field_'
+class NoneToEmptyListConverter(NullConverter):
+ """Converts None object to an empty list."""
def convert(self, value):
- 'See Zope.App.Forms.IConverter.IConverter'
- request = self.context
- raw_value = request.form.get(self.field_prefix+value)
- return raw_value
+ if value is None:
+ return []
+ else:
+ return value
-class ContainerConverter(Converter):
- """ """
- converters = []
+class ValueToSingleItemListConverter(NullConverter):
+ """Converts a single value to a list with the value being the only
+ element."""
def convert(self, value):
- 'See Zope.App.Forms.IConverter.IConverter'
- for converter in converters:
- value = converter(self.context).convert(value)
- return value
+ if not isinstance(value, ListTypes):
+ return [value]
+ else:
+ return value
+
-