[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms/Views/Browser - FormView.py:1.12 Widget.py:1.9 configure.zcml:1.6

Jim Fulton jim@zope.com
Sat, 7 Sep 2002 12:19:19 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/Forms/Views/Browser
In directory cvs.zope.org:/tmp/cvs-serv19433/lib/python/Zope/App/Forms/Views/Browser

Modified Files:
	FormView.py Widget.py configure.zcml 
Log Message:
More cleanup/refactoring of Schemas and forms. There's more to come,
but I'm checkpointing here.

I:

- Added schema field properties. These are like standard Python
  properies except that they are derived from Schema fields.

- Decomposed Str fields into Bytes fields and Text fields.
  Bytes fields contain 8-bit data and are stored as python strings.
  Text fields contain written human discourse, and are stored as
  unicode. It is invalid to store Python strings in Text fields or
  unicode in Bytes fields.

- Moved converters from schemas to forms, where they are used.

- Widgets are now responsible for:

  - Getting raw data from the request

  - Converting raw data to application data

  - Validating converted data against schema fields

- Began defining an error framework for errors in forms.

- Simplified FormViews to reflect new widget responsibilities.

- Added Bytes, Int and Float widgets and changed some application and
  test code to use them.




=== Zope3/lib/python/Zope/App/Forms/Views/Browser/FormView.py 1.11 => 1.12 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/FormView.py:1.11	Thu Sep  5 14:55:01 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/FormView.py	Sat Sep  7 12:18:48 2002
@@ -16,7 +16,8 @@
 """
 from Zope.Schema.IField import IField
 from Zope.Schema.Exceptions import StopValidation, ValidationError, \
-     ConversionError, ValidationErrorsAll, ConversionErrorsAll
+     ValidationErrorsAll, ConversionErrorsAll
+from Zope.App.Forms.Exceptions import ConversionError
 from Zope.Schema import getFields, validateMappingAll
 #from Zope.Proxy.ContextWrapper import ContextWrapper
 from Zope.ComponentArchitecture import getView
@@ -73,49 +74,16 @@
             value = getattr(self.context, field.getName())
         return widget.render(value)
 
-    def getAllRawFieldData(self):
-        """Returns field data retrieved from request."""
-        request = self.request
-        data = {}
-        for field in self.getFields():
-            name = field.getName()
-            raw_data = request.form.get('field_' + name)
-            data[name] = raw_data
-        return data
-
-    def convertAllFieldData(self, raw_data):
-        """Convert the raw data into valid objects."""
-        result = {}
-        errors = []
-        for name, value in raw_data.iteritems():
-            widget = self.getWidgetForFieldName(name)
-            try:
-                result[name] = widget.convert(value)
-            except ConversionError, error:
-                errors.append((name, error))
-
-        if errors:
-            raise ConversionErrorsAll, errors
-
-        return result
-            
-    def validateAllFieldData(self, data):
-        """Validate all the data."""
-        validateMappingAll(self.schema, data)
+    def saveValuesInContext(self):
+        'See Zope.App.Forms.Views.Browser.IForm.IWriteForm'
 
-    def storeAllDataInContext(self, data):
-        """Store the data back into the context object."""
         context = removeAllProxies(self.context)
-        for name, value in data.iteritems():
+        for field in self.getFields():            
+            widget = self.getWidgetForField(field)
+            name = field.getName()
+            value = widget.getData()
             if value != getattr(context, name):
                 setattr(context, name, value)
-
-    def saveValuesInContext(self):
-        'See Zope.App.Forms.Views.Browser.IForm.IWriteForm'
-        data = self.getAllRawFieldData()
-        data = self.convertAllFieldData(data)
-        self.validateAllFieldData(data)
-        self.storeAllDataInContext(data)
 
     def action(self):
         'See Zope.App.Forms.Views.Browser.IForm.IWriteForm'


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/Widget.py 1.8 => 1.9 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/Widget.py:1.8	Thu Sep  5 14:55:01 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/Widget.py	Sat Sep  7 12:18:48 2002
@@ -16,7 +16,7 @@
 """
 from types import ListType, TupleType
 ListTypes = (ListType, TupleType)
-from Zope.Schema import Converter
+from Zope.App.Forms import Converter
 from Zope.ComponentArchitecture import getAdapter
 from Zope.Proxy.ProxyIntrospection import removeAllProxies
 from Zope.Publisher.Browser.BrowserView import BrowserView
@@ -24,6 +24,7 @@
 from Zope.App.Forms.Converter import \
      NoneToEmptyListConverter, ValueToSingleItemListConverter
 from Zope.App.Forms.Widget import Widget
+from Zope.App.Forms.Converter import StrToIntConverter, StrToFloatConverter
 
 
 class BrowserWidget(Widget, BrowserView):
@@ -42,14 +43,17 @@
     extra = ''
 
 
-    def convert(self, value):
+    def _getRawData(self):
+        return self.request.form["field_" + self.getName()] 
+
+    def _convert(self, value):
         return self.converter.convert(value)
 
     def render(self, value):
         'See Zope.App.Forms.Views.Browser.IBrowserWidget.IBrowserWidget'
         return renderElement(self.getValue('tag'),
                              type = self.getValue('type'),
-                             name = self.context.getName(),
+                             name = self.getName(),
                              value = value,
                              cssClass = self.getValue('cssClass'),
                              extra = self.getValue('extra'))
@@ -58,7 +62,7 @@
         'See Zope.App.Forms.Views.Browser.IBrowserWidget.IBrowserWidget'
         return renderElement(self.getValue('tag'),
                              type = 'hidden',
-                             name = self.context.getName(),
+                             name = self.getName(),
                              value = value,
                              cssClass = self.getValue('cssClass'),
                              extra = self.getValue('extra'))
@@ -121,6 +125,23 @@
                                  size = self.getValue('displayWidth'),
                                  extra = self.getValue('extra'))
 
+class BytesWidget(TextWidget):
+
+    def _convert(self, value):
+        if type(value) is unicode:
+            value = value.encode('ascii')
+
+        return value
+
+class IntWidget(TextWidget):
+    displayWidth = 10
+
+    converter = StrToIntConverter()
+
+class FloatWidget(TextWidget):
+    displayWidth = 10
+
+    converter = StrToFloatConverter()
 
 class TextAreaWidget(BrowserWidget):
     """Textarea widget."""


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/configure.zcml 1.5 => 1.6 ===
--- Zope3/lib/python/Zope/App/Forms/Views/Browser/configure.zcml:1.5	Thu Sep  5 14:55:01 2002
+++ Zope3/lib/python/Zope/App/Forms/Views/Browser/configure.zcml	Sat Sep  7 12:18:48 2002
@@ -7,21 +7,27 @@
   <!-- Form Widget View Directives -->
   <browser:view
       permission="Zope.View"
-      for="Zope.Schema.IField.IStr"
+      for="Zope.Schema.IField.IText"
       name="widget"
       factory=".Widget.TextWidget." />
 
   <browser:view
       permission="Zope.View"
+      for="Zope.Schema.IField.IBytes"
+      name="widget"
+      factory=".Widget.BytesWidget." />
+
+  <browser:view
+      permission="Zope.View"
       for="Zope.Schema.IField.IInt"
       name="widget"
-      factory=".Widget.TextWidget." />
+      factory=".Widget.IntWidget." />
 
   <browser:view
       permission="Zope.View"
       for="Zope.Schema.IField.IFloat"
       name="widget"
-      factory=".Widget.TextWidget." />
+      factory=".Widget.FloatWidget." />
 
   <browser:view
       permission="Zope.View"