[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/Forms/Browser - subedit.pt:1.2 EditView.py:1.4 meta.zcml:1.2

Jim Fulton jim@zope.com
Wed, 11 Dec 2002 08:55:59 -0500


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

Modified Files:
	EditView.py meta.zcml 
Added Files:
	subedit.pt 
Log Message:
Added form:subedit directive for setting up views that provide
"sub-forms". These are views that render dorm input fields, but not a
form tag or submit buttons. They are uses as parts of larger forms.

Also added options to both form:edit and form:subedit:

 omit="name1 name2 ..."

   This omits named fields from the form

 fields="name1 name2 ..."

   This specifies fields to be included *and* their order.
    


=== Zope3/lib/python/Zope/App/Forms/Browser/subedit.pt 1.1 => 1.2 ===
--- /dev/null	Wed Dec 11 08:55:59 2002
+++ Zope3/lib/python/Zope/App/Forms/Browser/subedit.pt	Wed Dec 11 08:55:59 2002
@@ -0,0 +1,41 @@
+<div metal:define-macro="formbody">
+
+  <span tal:condition="view/label"
+      tal:content="view/label"
+      metal:define-slot="heading"
+      >Edit something</span>
+
+  <p tal:define="status view/update"
+     tal:condition="status"
+     tal:content="status" />
+
+  <div tal:condition="view/errors">
+     <ul>
+        <li tal:repeat="error view/errors">
+           <strong tal:content="error/__class__">
+              Error Type</strong>:
+           <span tal:content="error">Error text</span>
+        </li>
+     </ul>
+  </div>
+
+  <div metal:define-slot="extra_info" tal:replace="nothing">
+  </div>
+
+  <table width="100%" border="0">
+  <tr metal:define-slot="extra_top" tal:replace="nothing">
+      <td>Extra top</td>
+      <td><input type="text" style="width:100%" /></td>
+  </tr>
+  <tr metal:define-macro="widget_rows" tal:repeat="widget view/widgets"
+      tal:content="structure widget/row">
+      <td>Name</td>
+      <td><input type="text" style="width:100%" /></td>
+  </tr>
+  <tr metal:define-slot="extra_bottom" tal:replace="nothing">
+      <td>Extra bottom</td>
+      <td><input type="text" style="width:100%" /></td>
+  </tr>
+  </table>
+
+</div>


=== Zope3/lib/python/Zope/App/Forms/Browser/EditView.py 1.3 => 1.4 ===
--- Zope3/lib/python/Zope/App/Forms/Browser/EditView.py:1.3	Sun Dec  1 05:22:33 2002
+++ Zope3/lib/python/Zope/App/Forms/Browser/EditView.py	Wed Dec 11 08:55:59 2002
@@ -42,22 +42,20 @@
     errors = ()
     label = ''
 
-    generated_form = index = ViewPageTemplateFile('edit.pt')
-
+    # Fall-back field names computes from schema
+    fieldNames = property(lambda self: fieldNames(self.schema))
+        
     def __init__(self, context, request):
         super(EditView, self).__init__(context, request)
-        setUpEditWidgets(self, self.schema)
+        setUpEditWidgets(self, self.schema, names=self.fieldNames)
 
     def setPrefix(self, prefix):
         for widget in self.widgets():
             widget.setPrefix(prefix)
 
-    def __call__(self, *args, **kw):
-        return self.index(*args, **kw)
-
     def widgets(self):
         return [getattr(self, name)
-                for name in fieldNames(self.schema)
+                for name in self.fieldNames
                 ]
 
     def apply_update(self, data):
@@ -110,7 +108,8 @@
         if Update in self.request:
             unchanged = True
             try:
-                data = getWidgetsData(self, self.schema, required=0)
+                data = getWidgetsData(self, self.schema,
+                                      required=0, names=self.fieldNames)
                 unchanged = self.apply_update(data)
             except WidgetsError, errors:
                 self.errors = errors
@@ -119,22 +118,17 @@
                 self.errors = (v, )
                 return u"An error occured."
             else:
-                setUpEditWidgets(self, self.schema, force=1)
+                setUpEditWidgets(self, self.schema, force=1,
+                                 names=self.fieldNames)
                 if not unchanged:
                     return "Updated %s" % datetime.utcnow()
 
         return ''
 
-    
 
 def EditViewFactory(name, schema, label, permission, layer,
-                    template, class_, for_):
+                    template, default_template, bases, for_, fields):
 
-    if class_ is None:
-        bases = EditView,
-    else:
-        bases = class_, EditView
-    
     class_  = SimpleViewClass(
         template,
         used_for = schema, bases = bases
@@ -142,6 +136,9 @@
 
     class_.schema = schema
     class_.label = label
+    class_.fieldNames = fields
+
+    class_.generated_form = ViewPageTemplateFile(default_template)
 
     defineChecker(class_,
                   NamesChecker(
@@ -152,34 +149,87 @@
 
     provideView(for_, name, IBrowserPresentation, class_, layer)
                   
-        
 
-def directive(_context, name, schema, label,
-              permission = 'Zope.Public', layer = "default",
-              class_ = None, for_ = None,
-              template = None):
+def _normalize(_context, schema_, for_, class_, template, default_template,
+               fields, omit):
+    schema = _context.resolve(schema_)
 
-    schema = _context.resolve(schema)
     if for_ is None:
         for_ = schema
     else:
         for_ = _context.resolve(for_)
-    if class_ is not None:
-        class_ = _context.resolve(class_)
+
+    if class_ is None:
+        bases = (EditView, )
+    else:
+        bases = (_context.resolve(class_), EditView)
+        
+
     if template is not None:
         template = _context.path(template)
     else:
-        template = 'edit.pt'
+        template = default_template
 
     template = str(template)
 
+    names = fieldNames(schema)
+    
+    if fields:
+        fields = fields.split()
+        for name in fields:
+            if name not in names:
+                raise ValueError("Field name %s is not in schema %s",
+                                 name, schema_)
+    else:
+        fields = names
+
+    if omit:
+        omit = omit.split()
+        for name in omit:
+            if name not in names:
+                raise ValueError("Field name %s is not in schema %s",
+                                 name, schema_)
+        fields = [name for name in fields if name not in omit]
+
+    return schema, for_, bases, template, fields
+
+def edit(_context, name, schema, label,
+              permission = 'Zope.Public', layer = "default",
+              class_ = None, for_ = None,
+              template = None, omit=None, fields=None):
+
+    (schema, for_, bases, template, fields,
+     ) = _normalize(
+        _context, schema, for_, class_, template, 'edit.pt', fields, omit)
+
     return [
         Action(
         discriminator = ('http://namespaces.zope.org/form/edit',
                          name, for_, layer),
         callable = EditViewFactory,
-        args = (name, schema, label, permission, layer, template, class_,
-                for_),
+        args = (name, schema, label, permission, layer, template, 'edit.pt',
+                bases,
+                for_, fields),
+        )
+        ]
+
+def subedit(_context, name, schema, label,
+              permission = 'Zope.Public', layer = "default",
+              class_ = None, for_ = None,
+              template = None, omit=None, fields=None):
+
+    (schema, for_, bases, template, fields,
+     ) = _normalize(
+        _context, schema, for_, class_, template, 'subedit.pt', fields, omit)
+
+    return [
+        Action(
+        discriminator = ('http://namespaces.zope.org/form/subedit',
+                         name, for_, layer),
+        callable = EditViewFactory,
+        args = (name, schema, label, permission, layer, template, 'subedit.pt',
+                bases,
+                for_, fields),
         )
         ]
 


=== Zope3/lib/python/Zope/App/Forms/Browser/meta.zcml 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/App/Forms/Browser/meta.zcml:1.1	Mon Nov 11 15:29:58 2002
+++ Zope3/lib/python/Zope/App/Forms/Browser/meta.zcml	Wed Dec 11 08:55:59 2002
@@ -5,7 +5,13 @@
     <directive
        name="edit" 
        attributes="name schema label for layer permission class template"
-       handler=".EditView.directive"
+       handler=".EditView.edit"
+       />
+
+    <directive
+       name="subedit" 
+       attributes="name schema label for layer permission class template"
+       handler=".EditView.subedit"
        />
 
   </directives>