[Checkins] SVN: grok/trunk/src/grok/ form fields are now also derived from the interfaces the model

Martijn Faassen faassen at infrae.com
Thu Nov 30 16:48:17 EST 2006


Log message for revision 71341:
  form fields are now also derived from the interfaces the model 
  implements, not just the model-level fields description.
  

Changed:
  U   grok/trunk/src/grok/components.py
  A   grok/trunk/src/grok/tests/form/schemaform.py

-=-
Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2006-11-30 21:12:09 UTC (rev 71340)
+++ grok/trunk/src/grok/components.py	2006-11-30 21:48:16 UTC (rev 71341)
@@ -250,8 +250,10 @@
 
 class Form(View):
     def _init(self):
-        fields = schema_fields(self.context)
-        self.form_fields = form.Fields(*fields)
+        fields = form.Fields(*schema_fields(self.context))
+        fields += form.Fields(*interface.providedBy(self.context))
+        fields = fields.omit('__name__')
+        self.form_fields = fields
 
         self.template = component.getAdapter(self, INamedTemplate,
                                              name='default')

Added: grok/trunk/src/grok/tests/form/schemaform.py
===================================================================
--- grok/trunk/src/grok/tests/form/schemaform.py	2006-11-30 21:12:09 UTC (rev 71340)
+++ grok/trunk/src/grok/tests/form/schemaform.py	2006-11-30 21:48:16 UTC (rev 71341)
@@ -0,0 +1,70 @@
+"""
+A grok.Model may implement one or more interfaces that are schemas:
+
+  >>> grok.grok(__name__)
+  >>> manfred = Mammoth()
+
+A grok.EditForm is a special grok.View that renders an edit form.
+
+We need to set up the default formlib template first, because even though we
+don't use the formlib NamedTemplates directly they need to be present to create
+a formlib form.
+
+  >>> from zope import component
+  >>> from zope.formlib import form
+  >>> component.provideAdapter(form.default_page_template, name='default')
+
+  >>> from zope.publisher.browser import TestRequest
+  >>> request = TestRequest()
+  >>> view = component.getMultiAdapter((manfred, request), name='edit')
+  >>> len(view.form_fields)
+  2
+  >>> [w.__name__ for w in view.form_fields]
+  ['name', 'size']
+
+When there are multiple schemas in play, we get all the fields:
+
+  >>> view = component.getMultiAdapter((Manfred(), request), name='edit2')
+  >>> len(view.form_fields)
+  3
+  >>> [w.__name__ for w in view.form_fields]
+  ['can_talk', 'name', 'size']
+
+Schema fields and model level fields are combined:
+
+  >>> view = component.getMultiAdapter(
+  ...    (AnotherMammoth(), request), name='edit3')
+  >>> len(view.form_fields)
+  3
+  >>> [w.__name__ for w in view.form_fields]
+  ['can_talk', 'name', 'size']
+
+"""
+import grok
+from zope import interface, schema
+
+class IMammoth(interface.Interface):
+    name = schema.TextLine(title=u"Name")
+    size = schema.TextLine(title=u"Size", default=u"Quite normal")
+
+class Mammoth(grok.Model):
+    interface.implements(IMammoth)
+
+class Edit(grok.EditForm):
+    grok.context(Mammoth)
+
+class IMovieCharacter(interface.Interface):
+    can_talk = schema.Bool(title=u'Can talk', default=False)
+
+class Manfred(Mammoth):
+    interface.implements(IMovieCharacter)
+
+class Edit2(grok.EditForm):
+    grok.context(Manfred)
+
+class AnotherMammoth(Mammoth):
+    class fields:
+        can_talk = schema.Bool(title=u'Can talk', default=False)
+
+class Edit3(grok.EditForm):
+    grok.context(AnotherMammoth)



More information about the Checkins mailing list