[Checkins] SVN: grok/trunk/src/grok/ Implement redirect behaviour.

Martijn Faassen faassen at infrae.com
Tue Oct 31 09:28:20 EST 2006


Log message for revision 71006:
  Implement redirect behaviour.
  

Changed:
  U   grok/trunk/src/grok/components.py
  A   grok/trunk/src/grok/ftests/url/redirect.py

-=-
Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2006-10-31 14:07:19 UTC (rev 71005)
+++ grok/trunk/src/grok/components.py	2006-10-31 14:28:19 UTC (rev 71006)
@@ -123,6 +123,9 @@
         # URL to view on obj
         return url + '/' + urllib.quote(name.encode('utf-8'),
                                         SAFE_URL_CHARACTERS)
+
+    def redirect(self, url):
+        return self.request.response.redirect(url)
     
     def before(self):
         pass

Added: grok/trunk/src/grok/ftests/url/redirect.py
===================================================================
--- grok/trunk/src/grok/ftests/url/redirect.py	2006-10-31 14:07:19 UTC (rev 71005)
+++ grok/trunk/src/grok/ftests/url/redirect.py	2006-10-31 14:28:19 UTC (rev 71006)
@@ -0,0 +1,34 @@
+"""
+Views have a redirect() method to easily create redirects:
+
+  >>> import grok
+  >>> grok.grok('grok.ftests.url.redirect')
+
+  >>> from grok.ftests.url.redirect import Mammoth
+  >>> getRootFolder()['manfred'] = manfred = Mammoth()
+
+Since the index view redirects to mammoth, we expect to see the URL
+point to mammoth:
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+  >>> browser.open('http://localhost/manfred')
+  >>> browser.url
+  'http://localhost/manfred/another'
+  
+"""
+import grok
+
+class Mammoth(grok.Model):
+    pass
+
+class Index(grok.View):
+    def render(self):
+        self.redirect(self.url('another'))
+
+class Another(grok.View):
+    def render(self):
+        return "Another view"
+    
+



More information about the Checkins mailing list