[Checkins] SVN: plone.z3cform/branches/infrae-grok/plone/z3cform/ - Grok support for simple forms, using z3cform.

Sylvain Viollon sylvain at infrae.com
Tue Aug 5 04:44:50 EDT 2008


Log message for revision 89363:
  
  - Grok support for simple forms, using z3cform.
  
  

Changed:
  A   plone.z3cform/branches/infrae-grok/plone/z3cform/components.py
  A   plone.z3cform/branches/infrae-grok/plone/z3cform/meta.py
  A   plone.z3cform/branches/infrae-grok/plone/z3cform/meta.zcml
  U   plone.z3cform/branches/infrae-grok/plone/z3cform/templates.py
  U   plone.z3cform/branches/infrae-grok/plone/z3cform/templates.zcml

-=-
Added: plone.z3cform/branches/infrae-grok/plone/z3cform/components.py
===================================================================
--- plone.z3cform/branches/infrae-grok/plone/z3cform/components.py	                        (rev 0)
+++ plone.z3cform/branches/infrae-grok/plone/z3cform/components.py	2008-08-05 08:44:49 UTC (rev 89363)
@@ -0,0 +1,100 @@
+
+from zope import interface
+from zope.component.interfaces import IFactory
+from zope.publisher.publish import mapply
+
+import martian
+from grokcore import view
+from z3c.form import form, field
+
+from plone.z3cform import z2
+from z3c.form.interfaces import IFormLayer
+
+class IGrokForm(interface.Interface):
+    """A grok z3c form. This marker interface is used to have a
+    different default template.
+    """
+
+class DefaultFields(field.Fields):
+    """Marker for default fields.
+    """
+
+class GrokForm(object):
+    """A z3c grok form. This is based on the GrokForm designed for
+    Formlib.
+    """
+
+    interface.implements(IGrokForm)
+    martian.baseclass()
+
+    fields = DefaultFields()
+
+    def __init__(self, context, request):
+        super(GrokForm, self).__init__(context, request)
+        self.__name__ = self.__view_name__ # For Zope2 publisher
+
+    def update(self):
+        """Subclasses can override this method just like on regular
+        grok.Views. It will be called before any form processing
+        happens."""
+
+    def update_form(self):
+        """Update the form, i.e. process form input using widgets.
+
+        On z3c.form forms, this is what the update() method is.
+        In grok views, the update() method has a different meaning.
+        That's why this method is called update_form() in grok forms.
+        """
+        super(GrokForm, self).update()
+
+
+    def render(self):
+        """People don't have to define a render method here, and we
+        have to use the one provided by z3c.form (people can provide
+        render method in grok).
+        """
+        return super(GrokForm, self).render()
+
+    render.base_method = True   # Mark the method to prevent people to
+                                # override it.
+
+    def __call__(self):
+        mapply(self.update, (), self.request)
+        if self.request.response.getStatus() in (302, 303):
+            # A redirect was triggered somewhere in update().  Don't
+            # continue rendering the template or doing anything else.
+            return
+
+        z2.switch_on(self, request_layer=IFormLayer)
+        self.update_form()
+        return self.render()
+
+
+class Form(GrokForm, form.Form, view.View):
+    """Normal z3c form.
+    """
+
+    martian.baseclass()
+
+
+class AddForm(GrokForm, form.AddForm, view.View):
+    """z3c add form.
+    """
+
+    interface.implements(IFactory)
+    martian.baseclass()
+
+
+class EditForm(GrokForm, form.EditForm, view.View):
+    """z3c edit form.
+    """
+
+    martian.baseclass()
+
+
+class DisplayForm(GrokForm, form.DisplayForm, view.View):
+    """z3c display form.
+    """
+    
+    martian.baseclass()
+


Property changes on: plone.z3cform/branches/infrae-grok/plone/z3cform/components.py
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision

Added: plone.z3cform/branches/infrae-grok/plone/z3cform/meta.py
===================================================================
--- plone.z3cform/branches/infrae-grok/plone/z3cform/meta.py	                        (rev 0)
+++ plone.z3cform/branches/infrae-grok/plone/z3cform/meta.py	2008-08-05 08:44:49 UTC (rev 89363)
@@ -0,0 +1,54 @@
+
+from zope.interface.interfaces import IInterface
+
+import grokcore.component
+import grokcore.view
+from grokcore.view.meta import ViewGrokker
+from grokcore.formlib.formlib import most_specialized_interfaces
+
+from martian.error import GrokError
+import martian
+
+from plone.z3cform import components
+from z3c.form import field
+
+def get_auto_fields(context):
+    """Get the form fields for context.
+
+    This methods is the same than for formlib implementation, but use
+    z3cform fields instead.
+    """
+    # for an interface context, we generate them from that interface
+    if IInterface.providedBy(context):
+        return field.Fields(context)
+    # if we have a non-interface context, we're autogenerating them
+    # from any schemas defined by the context
+    fields = field.Fields(*most_specialized_interfaces(context))
+    # we pull in this field by default, but we don't want it in our form
+    fields = field.omit('__name__')
+    return fields
+
+
+class FormGrokker(martian.ClassGrokker):
+
+    martian.component(components.GrokForm)
+    martian.directive(grokcore.component.context)
+    # execute this grokker before grokcore.view's ViewGrokker
+    martian.priority(martian.priority.bind().get(ViewGrokker) + 10)
+
+    def execute(self, form, context, **kw):
+
+        # Set fields by default.
+        if isinstance(form.fields, components.DefaultFields):
+            form.fields = get_auto_fields(context)
+
+        # Don't override render method.
+        if not getattr(form.render, 'base_method', False):
+            raise GrokError(
+                "It is not allowed to specify a custom 'render' "
+                "method for form %r. Forms either use the default "
+                "template or a custom-supplied one." % factory,
+                factory)
+
+        return True
+


Property changes on: plone.z3cform/branches/infrae-grok/plone/z3cform/meta.py
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision

Added: plone.z3cform/branches/infrae-grok/plone/z3cform/meta.zcml
===================================================================
--- plone.z3cform/branches/infrae-grok/plone/z3cform/meta.zcml	                        (rev 0)
+++ plone.z3cform/branches/infrae-grok/plone/z3cform/meta.zcml	2008-08-05 08:44:49 UTC (rev 89363)
@@ -0,0 +1,10 @@
+<configure
+   xmlns="http://namespaces.zope.org/zope"
+   xmlns:grok="http://namespaces.zope.org/grok">
+
+  <!-- DON'T INCLUDE THIS FILE IF YOU DON'T USE GROK -->
+
+  <include package="five.grok" file="meta.zcml" />
+  <grok:grok package=".meta" />
+</configure>
+

Modified: plone.z3cform/branches/infrae-grok/plone/z3cform/templates.py
===================================================================
--- plone.z3cform/branches/infrae-grok/plone/z3cform/templates.py	2008-08-05 07:43:57 UTC (rev 89362)
+++ plone.z3cform/branches/infrae-grok/plone/z3cform/templates.py	2008-08-05 08:44:49 UTC (rev 89363)
@@ -12,6 +12,7 @@
 from z3c.form.form import FormTemplateFactory
 
 import plone.z3cform
+import plone.z3cform.components
 
 path = lambda p: os.path.join(os.path.dirname(plone.z3cform.__file__), p)
 
@@ -20,6 +21,11 @@
 subform_factory = FormTemplateFactory(
     path('subform.pt'), form=z3c.form.interfaces.ISubForm)
 
+# I am not sure the form.pt work with grok, and I can test since I
+# don't have plone, so no main template. 
+grok_form_factory = FormTemplateFactory(
+    path('macros.pt'), form=plone.z3cform.components.IGrokForm)
+
 class Macros(zope.publisher.browser.BrowserView):
     template = zope.app.pagetemplate.viewpagetemplatefile.ViewPageTemplateFile(
         'macros.pt')

Modified: plone.z3cform/branches/infrae-grok/plone/z3cform/templates.zcml
===================================================================
--- plone.z3cform/branches/infrae-grok/plone/z3cform/templates.zcml	2008-08-05 07:43:57 UTC (rev 89362)
+++ plone.z3cform/branches/infrae-grok/plone/z3cform/templates.zcml	2008-08-05 08:44:49 UTC (rev 89363)
@@ -14,5 +14,6 @@
 
   <adapter factory=".templates.form_factory" />
   <adapter factory=".templates.subform_factory" />
+  <adapter factory=".templates.grok_form_factory" />
 
 </configure>



More information about the Checkins mailing list