[Checkins] SVN: grok/trunk/src/grok/ Enable argument dispatching to grok.View's update and render methods. That means

Philipp von Weitershausen philikon at philikon.de
Wed Feb 7 06:45:41 EST 2007


Log message for revision 72417:
  Enable argument dispatching to grok.View's update and render methods. That means
  you can now simply write
  
    def update(self, name, title):
        ...
  
  instead of:
  
    def update(self):
        name = self.request.form.get('name')
        title = self.request.form.get('title')
        if name and title:
            ...
  

Changed:
  U   grok/trunk/src/grok/components.py
  A   grok/trunk/src/grok/ftests/view/argument.py

-=-
Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2007-02-07 10:59:28 UTC (rev 72416)
+++ grok/trunk/src/grok/components.py	2007-02-07 11:45:40 UTC (rev 72417)
@@ -26,6 +26,7 @@
 from zope.publisher.interfaces import NotFound
 from zope.publisher.interfaces.browser import (IBrowserPublisher,
                                                IBrowserRequest)
+from zope.publisher.publish import mapply
 from zope.pagetemplate import pagetemplate, pagetemplatefile
 from zope.formlib import form
 from zope.formlib.namedtemplate import INamedTemplate
@@ -146,7 +147,7 @@
                 interface.Interface, name=self.module_info.package_dotted_name)
 
     def __call__(self):
-        self.update()
+        mapply(self.update, (), self.request)
         if self.request.response.getStatus() in (302, 303):
             # Somewhere in update(), a redirect was triggered.  Don't
             # continue rendering the template or doing anything else.
@@ -154,7 +155,7 @@
 
         template = getattr(self, 'template', None)
         if not template:
-            return self.render()
+            return mapply(self.render, (), self.request)
 
         namespace = template.pt_getContext()
         namespace['request'] = self.request

Added: grok/trunk/src/grok/ftests/view/argument.py
===================================================================
--- grok/trunk/src/grok/ftests/view/argument.py	2007-02-07 10:59:28 UTC (rev 72416)
+++ grok/trunk/src/grok/ftests/view/argument.py	2007-02-07 11:45:40 UTC (rev 72417)
@@ -0,0 +1,76 @@
+"""
+  >>> import grok
+  >>> from grok.ftests.view.argument import Mammoth
+  >>> grok.grok('grok.ftests.view.argument')
+  >>> getRootFolder()["manfred"] = Mammoth()
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+
+Form variables such as GET parameters are dispatched to arguments of
+the render() method, should the method choose to take them:
+
+  >>> browser.open("http://localhost/manfred/render?message=Foo&another=Bar")
+  >>> print browser.contents
+  Message: Foo
+  Another: Bar
+
+Supplying more arguments than those specified has no effect:
+
+  >>> browser.open("http://localhost/manfred/render?message=There&another=Is&last=More")
+  >>> print browser.contents
+  Message: There
+  Another: Is
+
+If you don't supply all of the arguments, there will be a System Error:
+
+  >>> browser.open("http://localhost/manfred/render?message=Foo")
+  Traceback (most recent call last):
+  ...
+  TypeError: Missing argument to render(): another
+
+The same works with views that define update():
+
+  >>> browser.open("http://localhost/manfred/update?message=Foo&another=Bar")
+  >>> print browser.contents
+  Coming to us from update():
+  Message: Foo
+  Another: Bar
+
+  >>> browser.open("http://localhost/manfred/update?message=There&another=Is&last=More")
+  >>> print browser.contents
+  Coming to us from update():
+  Message: There
+  Another: Is
+
+  >>> browser.open("http://localhost/manfred/update?another=Bar")
+  Traceback (most recent call last):
+  ...
+  TypeError: Missing argument to update(): message
+
+"""
+import grok
+
+class Mammoth(grok.Model):
+    pass
+
+class RenderWithArguments(grok.View):
+    grok.name('render')
+
+    def render(self, message, another):
+        return "Message: %s\nAnother: %s" % (message, another)
+
+class UpdateWithArguments(grok.View):
+    grok.name('update')
+    grok.template('update')
+
+    def update(self, message, another):
+        self.message = message
+        self.another = another
+
+update = grok.PageTemplate("""
+Coming to us from update():
+Message: <span tal:replace="view/message" />
+Another: <span tal:replace="view/another" />
+""")


Property changes on: grok/trunk/src/grok/ftests/view/argument.py
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list