[Checkins] SVN: grok/trunk/src/grok/ integrated @form.action as @grok.action. It's not as smooth as I'd like it to be, but... I don't really have any better idea right now.

Wolfgang Schnerring wosc at wosc.de
Thu Oct 19 09:20:45 EDT 2006


Log message for revision 70810:
  integrated @form.action as @grok.action. It's not as smooth as I'd like it to be, but... I don't really have any better idea right now.

Changed:
  U   grok/trunk/src/grok/__init__.py
  U   grok/trunk/src/grok/components.py
  A   grok/trunk/src/grok/ftests/form/actions.py
  U   grok/trunk/src/grok/interfaces.py

-=-
Modified: grok/trunk/src/grok/__init__.py
===================================================================
--- grok/trunk/src/grok/__init__.py	2006-10-19 12:35:17 UTC (rev 70809)
+++ grok/trunk/src/grok/__init__.py	2006-10-19 13:20:44 UTC (rev 70810)
@@ -16,6 +16,7 @@
 
 from zope.interface import implements
 from zope.component import adapts
+from zope.formlib.form import action
 from zope.event import notify
 from zope.lifecycleevent import (
     IObjectCreatedEvent, ObjectCreatedEvent,

Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2006-10-19 12:35:17 UTC (rev 70809)
+++ grok/trunk/src/grok/components.py	2006-10-19 13:20:44 UTC (rev 70810)
@@ -208,6 +208,9 @@
         super(EditForm, self).__init__(context, request)
         self._init()
 
+    def default_handle_apply(self, action, data):
+         form.EditForm.handle_edit_action.success_handler(self, action, data)
+
 class DisplayForm(Form, form.DisplayForm):
     def __init__(self, context, request):
         super(DisplayForm, self).__init__(context, request)

Added: grok/trunk/src/grok/ftests/form/actions.py
===================================================================
--- grok/trunk/src/grok/ftests/form/actions.py	2006-10-19 12:35:17 UTC (rev 70809)
+++ grok/trunk/src/grok/ftests/form/actions.py	2006-10-19 13:20:44 UTC (rev 70810)
@@ -0,0 +1,50 @@
+"""
+Using the @grok.action decorator, different actions can be defined on a
+grok.EditForm. When @grok.action is used, the default behaviour (the 'Apply'
+action) is not available anymore, but it can triggered manually by calling
+self.default_handle_apply(action, data).
+
+  >>> import grok
+  >>> from grok.ftests.form.actions import Mammoth
+  >>> grok.grok('grok.ftests.form.actions')
+  >>> getRootFolder()["manfred"] = Mammoth()
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+  >>> browser.open("http://localhost/manfred/@@edit")
+  >>> browser.getControl(name="form.name").value = "Manfred the Mammoth"
+  >>> browser.getControl(name="form.size").value = "Really big"
+  >>> browser.getControl("Apply").click()
+  >>> print browser.contents
+  <!DOCTYPE ...
+  ...Manfred the Mammoth...
+  ...Really big...
+  ...
+
+  >>> browser.open("http://localhost/manfred/@@edit")
+  >>> browser.getControl("Hairy").click()
+  >>> print browser.contents
+  <!DOCTYPE ...
+  ...Manfred the Mammoth...
+  ...Really big and hairy...
+  ...
+"""
+import grok
+from zope import schema
+
+class Mammoth(grok.Model):
+    class fields:
+        name = schema.TextLine(title=u"Name")
+        size = schema.TextLine(title=u"Size")
+
+class Edit(grok.EditForm):
+    @grok.action("Apply")
+    def handle_apply(self, action, data):
+        self.default_handle_apply(action, data)
+
+    @grok.action("Hairy")
+    def handle_hairy(self, action, data):
+        self.default_handle_apply(action, data)
+        self.context.size += " and hairy"
+    

Modified: grok/trunk/src/grok/interfaces.py
===================================================================
--- grok/trunk/src/grok/interfaces.py	2006-10-19 12:35:17 UTC (rev 70809)
+++ grok/trunk/src/grok/interfaces.py	2006-10-19 13:20:44 UTC (rev 70810)
@@ -77,6 +77,9 @@
         """Declare that a function subscribes to an event or a
         combination of objects and events."""
 
+    def action():
+        """XXX see zope.formlib.form.action"""
+
     traverse = interface.Attribute("Specify a method to be used for "
                                    "traversing URL paths.")
 



More information about the Checkins mailing list