[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form - editview.py:1.12

Tres Seaver tseaver@zope.com
Sat, 1 Mar 2003 22:34:37 -0500


Update of /cvs-repository/Zope3/src/zope/app/browser/form
In directory cvs.zope.org:/tmp/cvs-serv32503/src/zope/app/browser/form

Modified Files:
	editview.py 
Log Message:


  Adapt context to the EditView's schema, if possible.  Since the view
  expects to be able to write to the fields defined by the schema, it
  is sensible to check that such fields are actually supported.  Note
  that this change has an additional benefit:  the views can be safely
  used for objects which do not implement the schema directly, but
  for which an adapter to the schema is available.  In particular, I
  envision using such an adapter to store the schema values in an
  annotation, rather than directly in the object's own attributes, which
  should make it safe to extend an arbitrary third-party content object
  via a combination of an annotaion, and adapter, and an edit view.
  
  XXX This is still problematic for at least the AddView derivative,
  whose context is expected to implement IAdding, rather than the schema.
  My current workaround is to use 'queryAdapter(context,self.schema)', and
  then replace context with the adapter if found.  This choice shouldn't
  break any existing, working code;  a configuration which manages to
  bind an EditView to an inappropriate object will happily scribble on
  its attributes, however.



=== Zope3/src/zope/app/browser/form/editview.py 1.11 => 1.12 ===
--- Zope3/src/zope/app/browser/form/editview.py:1.11	Fri Feb 21 09:53:35 2003
+++ Zope3/src/zope/app/browser/form/editview.py	Sat Mar  1 22:34:04 2003
@@ -25,6 +25,7 @@
 from zope.publisher.browser import BrowserView
 from zope.security.checker import defineChecker, NamesChecker
 from zope.component.view import provideView
+from zope.component import queryAdapter
 
 from zope.app.interfaces.form import WidgetsError
 from zope.app.form.utility import setUpEditWidgets, getWidgetsData
@@ -52,8 +53,23 @@
     fieldNames = property(lambda self: getFieldNamesInOrder(self.schema))
 
     def __init__(self, context, request):
+        # XXX   This feels like it should be 'getAdapter';  it won't really
+        #       be sensible to use an EditView against an object which
+        #       doesn't implement our schema.  The AddView subclass, however,
+        #       expects its context to be an IAdding, and doesn't use the
+        #       schema attributes to set values.
+        #
+        #       I originally had EditView declare an '_adaptContextToSchema'
+        #       method, which used 'getAdapter', and then overrode it in
+        #       AddView to just return the context.  That felt icky, too,
+        #       and was more complex, so I backed that out in favor of
+        #       just using 'queryAdapter'.
+        adapted = queryAdapter(context, self.schema)
+        if adapted is not None:
+            context = adapted
         super(EditView, self).__init__(context, request)
         self._setUpWidgets()
+
 
     def _setUpWidgets(self):
         setUpEditWidgets(self, self.schema, names=self.fieldNames)