[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Forms/Views/Browser - FormView.py:1.4 IBrowserWidget.py:1.2 Widget.py:1.3

Stephan Richter srichter@cbu.edu
Tue, 16 Jul 2002 10:03:03 -0400


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

Modified Files:
	FormView.py IBrowserWidget.py Widget.py 
Log Message:
Okay, I finished the basic Widget stuff and wrote tests for it, which seemed
like it took forever. There are still not all test cases covered, but we 
will have to do this over time. I also expect that some of the code will 
change anyways.

I also disabled briefly the Form tests, since I broke them, but I am working 
on it now anyways, so they will be fixed soon.

What am I going to work on next?

- Make FormViews work nicely.
- Make Widget instances View factories (or something similar)
- Write configuration files for the stuff
- Write a Form layouter.

Note that I will try not to touch the converter, since I think other people
are working on it.



=== Zope3/lib/python/Zope/App/Forms/Views/Browser/FormView.py 1.3 => 1.4 ===
 from Schema.IField import IField
 from Zope.ComponentArchitecture import getView
 import Schema
+from IForm import IForm
 
 class FormView(BrowserView):
-    def getWidgetsForSchema(self, schema, view_name):
-        """Given a schema and a desired field name, get a list of
-        widgets for it.
-        """
-        result = []
+
+    __implements__ = IForm, BrowserView.__implements__
+
+    schema = None
+    custom_widgets = None
+
+    def getWidgetForFieldId(self, id):
+        field = self.schema[id]
+        return self.getWidgetForField(field)
+
+
+    def getWidgetForField(self, field):
+        if self.custom_widgets is not None and \
+           field.getValue('id') in custom_widgets.keys():
+            return custom_widgets[field.getValue('id')](field)
+
+        return getView(field, 'widget', self.request)
+
+    def getFieldData(self):
+        result = {}
+        request = self.request
+
         for name in schema.names(1):
-            attr = schema.getDescriptionFor(name)
-            if IField.isImplementedBy(attr):
-                widget = getView(attr, view_name, self.request)
-                result.append(widget)
-        return result
+            field = schema.getDescriptionFor(name)
+
+            if IField.isImplementedBy(field):
+                widget = self.getWidgetForField(field)
+                result[field.getValue(id)] = widget.convert(request)
 
-    def getFields(self):
-        """XXX just a test method.
-        """
-        result = self.getWidgetsForSchema(ITestSchema, 'normal')
-        print result
         return result
-    
+
+
+    def saveValuesInContext(self, mapping):
+        """Store all the new data inside the context object."""
+        for item in mapping.items():
+            if getattr(self.context, item[0]) != item[1]:
+                setattr(self.context, attr, mapping[attr])
+
+
+    def action(self):
+        """Execute the form. By default it tries to save the values back
+           into the content object."""
+        data = self.getFieldData()
+        try:
+            Schema.validateMappingAll(self.schema, self.getFieldData())
+        except ValidationErrorsAll, errors:
+            # display the form again
+            pass
+        else:
+            self.saveValuesInContext(data)


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/IBrowserWidget.py 1.1 => 1.2 ===
     """
 
 
-    def render(field, key, value):
+    def render(value):
         """Renders this widget as HTML using property values in field."""
 
         


=== Zope3/lib/python/Zope/App/Forms/Views/Browser/Widget.py 1.2 => 1.3 === (465/565 lines abridged)
 """
 $Id$
 """
-from types import ListTypes
-
+from types import ListType, TupleType
+ListTypes = (ListType, TupleType)
 from Zope.ComponentArchitecture import getAdapter
+from Zope.Publisher.Browser.BrowserView import BrowserView
 from Zope.App.Forms.Views.Browser.IBrowserWidget import IBrowserWidget
-from Zope.App.Forms.IPropertyFieldAdapter import IPropertyFieldAdapter
 from Zope.App.Forms.Widget import Widget
 
 
 class BrowserWidget(Widget, BrowserView):
     """A field widget that knows how to display itself as HTML."""
+
     __implements__ = IBrowserWidget
+    converter = None
+
     propertyNames = Widget.propertyNames + \
                     ['tag', 'type', 'cssClass', 'hidden', 'extra']
     
@@ -34,33 +37,21 @@
     hidden = 0
     extra = ''
 
-    def _getValueToInsert(self):
-        """Get a value to be inserted as the value of the input"""
-        request = self.request
-        field = self.context
-        if request and (('field_'+field.id) in request):
-            return request['field_'+field.id]
-        else:
-            return getAdapter(field, IPropertyFieldAdapter).\
-                   getPropertyInContext()
-        
-            
-    def render(self, field, key, 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.id,
-                             value = self._getValueToInsert(),
+                             value = value,
                              cssClass = self.getValue('cssClass'),
                              extra = self.getValue('extra'))
 
-

[-=- -=- -=- 465 lines omitted -=- -=- -=-]

-
-
-class TextWidget(BrowserWidget):
-    """Text widget."""
-    propertyNames = BrowserWidget.propertyNames + \
-                     ['displayWidth', 'displayMaxWidth', 'extra', 'default']
-    default = ''
-    displayWidth = 20
-    displayMaxWidth = ''
-    extra = ''
-
-    def render(self):
-        'See Zope.App.Forms.Views.Browser.IBrowserWidget.IBrowserWidget'
-        displayMaxWidth = self.getValue('displayMaxWidth') or 0
-        if displayMaxWidth > 0:
-            return renderElement(self.getValue('tag'),
-                                 type = self.getValue('type'),
-                                 name = self.context.id,
-                                 value = self._getValueToInsert(REQUEST),
-                                 cssClass = self.getValue('cssClass'),
-                                 size = self.getValue('displayWidth'),
-                                 maxlength = displayMaxWidth,
-                                 extra = self.getValue('extra'))
-        else:
-            return renderElement(self.getValue('tag'),
-                                 type = self.getValue('type'),
-                                 name = self.context.id,
-                                 value = self._getValueToInsert(REQUEST),
-                                 cssClass = self.getValue('cssClass'),
-                                 size = self.getValue('displayWidth'),
-                                 extra = self.getValue('extra'))
-
-
 # XXX Note, some HTML quoting is needed in renderTag and renderElement.
 
 def renderTag(tag, **kw):
-    """Render the tag. Well, not all of it, as we may want to / it.
-    """
+    """Render the tag. Well, not all of it, as we may want to / it."""
     attr_list = []
 
-    kw['name'] = 'field_' + kw['name']
+    if kw.has_key('name'):
+        kw['name'] = 'field_' + kw['name']
 
-    # special case handling for css_class
+    # special case handling for cssClass
     if 'cssClass' in kw:
         if kw['cssClass'] != "":
             attr_list.append('class="%s"' % kw['cssClass'])