[Checkins] SVN: grok/trunk/ define baseclasses for defining custom error views

Jan-Wijbrand Kolman janwijbrand at gmail.com
Wed Jan 19 12:27:37 EST 2011


Log message for revision 119726:
  define baseclasses for defining custom error views

Changed:
  U   grok/trunk/buildout.cfg
  U   grok/trunk/setup.py
  U   grok/trunk/src/grok/__init__.py
  U   grok/trunk/src/grok/components.py
  U   grok/trunk/src/grok/configure.zcml
  A   grok/trunk/src/grok/ftests/errorviews/
  A   grok/trunk/src/grok/ftests/errorviews/__init__.py
  A   grok/trunk/src/grok/ftests/errorviews/errorviews.py
  U   grok/trunk/src/grok/ftests/test_grok_functional.py

-=-
Modified: grok/trunk/buildout.cfg
===================================================================
--- grok/trunk/buildout.cfg	2011-01-19 17:11:48 UTC (rev 119725)
+++ grok/trunk/buildout.cfg	2011-01-19 17:27:37 UTC (rev 119726)
@@ -10,7 +10,11 @@
     .
     grokwiki
 versions = versions
-extensions = buildout.dumppickedversions
+extensions =
+  buildout.dumppickedversions
+  mr.developer
+auto-checkout =
+  zope.errorview
 
 [versions]
 grok =
@@ -32,7 +36,7 @@
 eggs =
     grok
     grok[test]
-defaults = ['--tests-pattern', '^f?tests$', '-v']
+defaults = ['--tests-pattern', '^f?tests$', '-v', '--auto-color']
 
 [zope_conf]
 recipe = z3c.recipe.template

Modified: grok/trunk/setup.py
===================================================================
--- grok/trunk/setup.py	2011-01-19 17:11:48 UTC (rev 119725)
+++ grok/trunk/setup.py	2011-01-19 17:27:37 UTC (rev 119726)
@@ -53,8 +53,8 @@
         'grokcore.site > 1.4',
         'grokcore.traverser',
         'grokcore.view',
+        'grokcore.viewlet >= 1.3',
         'grokcore.view [security_publication]',
-        'grokcore.viewlet >= 1.3',
         'grokcore.xmlrpc',
         'martian >= 0.14',
         'pytz',
@@ -70,6 +70,7 @@
         'zope.component',
         'zope.container',
         'zope.contentprovider',
+        'zope.errorview [browser]',
         'zope.event',
         'zope.exceptions',
         'zope.i18n',

Modified: grok/trunk/src/grok/__init__.py
===================================================================
--- grok/trunk/src/grok/__init__.py	2011-01-19 17:11:48 UTC (rev 119725)
+++ grok/trunk/src/grok/__init__.py	2011-01-19 17:27:37 UTC (rev 119726)
@@ -84,6 +84,7 @@
 
 from grok.components import Application
 from grok.components import View, Form, AddForm, EditForm, DisplayForm
+from grok.components import ExceptionView, NotFoundView, UnauthorizedView
 from grok.components import XMLRPC, REST, JSON
 from grok.components import Indexes
 from grok.components import Role

Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2011-01-19 17:11:48 UTC (rev 119725)
+++ grok/trunk/src/grok/components.py	2011-01-19 17:27:37 UTC (rev 119726)
@@ -21,16 +21,20 @@
 import simplejson
 
 import zope.location
+from zope.container.interfaces import IReadContainer
+from zope.errorview.browser import ExceptionView, NotFoundView, UnauthorizedView
 from zope import component
 from zope import interface
-from zope.securitypolicy.role import Role as securitypolicy_Role
+from zope.interface.common.interfaces import IException
 from zope.publisher.browser import BrowserPage
-from zope.publisher.interfaces import NotFound
+from zope.publisher.defaultview import getDefaultViewName
 from zope.publisher.interfaces.browser import IBrowserPublisher
 from zope.publisher.interfaces.http import IHTTPRequest
+from zope.publisher.interfaces import INotFound
+from zope.publisher.interfaces import NotFound
 from zope.publisher.publish import mapply
-from zope.publisher.defaultview import getDefaultViewName
-from zope.container.interfaces import IReadContainer
+from zope.security.interfaces import IUnauthorized
+from zope.securitypolicy.role import Role as securitypolicy_Role
 
 import grok
 import martian.util
@@ -67,7 +71,7 @@
 
     Implements the :class:`grokcore.view.interfaces.IGrokView`
     interface.
-    
+
     Each class that inherits from `grok.View` is designed to "render" a
     category of content objects by reducing them to a document (often an
     HTML document).  Every view has a name, and is invoked when users
@@ -149,6 +153,21 @@
         grokcore.message.send(message, type=type, name='session')
 
 
+class ExceptionView(ExceptionView, grokcore.view.View):
+    grok.context(IException)
+    grok.name('index.html')
+
+
+class NotFoundView(NotFoundView, grokcore.view.View):
+    grok.context(INotFound)
+    grok.name('index.html')
+
+
+class UnauthorizedView(UnauthorizedView, grokcore.view.View):
+    grok.context(IUnauthorized)
+    grok.name('index.html')
+
+
 class Form(grokcore.formlib.Form, View):
     """The base class for forms in Grok applications.
 
@@ -201,7 +220,7 @@
     :class:`grok.index.Field`, :class:`grok.index.Text`, or
     :class:`grok.index.Set` instances naming object attributes that
     should be indexed (and therefore searchable).::
-    
+
         class ArticleIndex(grok.Indexes):
             grok.site(Newspaper)
             grok.context(Article)

Modified: grok/trunk/src/grok/configure.zcml
===================================================================
--- grok/trunk/src/grok/configure.zcml	2011-01-19 17:11:48 UTC (rev 119725)
+++ grok/trunk/src/grok/configure.zcml	2011-01-19 17:27:37 UTC (rev 119726)
@@ -14,13 +14,14 @@
 
   <include package="grok" file="meta.zcml" />
 
+  <include package="zope.annotation" />
   <include package="zope.app.appsetup" />
   <include package="zope.app.publication" />
-  <include package="zope.annotation" />
   <include package="zope.browserpage" />
   <include package="zope.catalog" />
   <include package="zope.component" />
   <include package="zope.contentprovider" />
+  <include package="zope.errorview" file="browser.zcml"/>
   <include package="zope.intid" />
   <include package="zope.keyreference" />
   <include package="zope.location" />

Added: grok/trunk/src/grok/ftests/errorviews/errorviews.py
===================================================================
--- grok/trunk/src/grok/ftests/errorviews/errorviews.py	                        (rev 0)
+++ grok/trunk/src/grok/ftests/errorviews/errorviews.py	2011-01-19 17:27:37 UTC (rev 119726)
@@ -0,0 +1,78 @@
+"""
+
+By default, the views rendered for error situations is handled by
+zope.errorviews::
+
+  >>> from zope.component import getMultiAdapter
+  >>> from zope.publisher.browser import TestRequest
+  >>> view = getMultiAdapter((Exception(), TestRequest()), name='index.html')
+  >>> print view()
+  A system error occurred.
+
+  >>> view.isSystemError()
+  True
+
+  >>> from zope.publisher.interfaces import NotFound
+  >>> request = TestRequest()
+  >>> error = NotFound(object(), request)
+  >>> view = getMultiAdapter((error, request), name='index.html')
+  >>> print view()
+  The requested resource can not be found.
+
+  >>> from zope.security.interfaces import Unauthorized
+  >>> request = TestRequest()
+  >>> request.setPrincipal(MockPrincipal())
+  >>> view = getMultiAdapter((Unauthorized(), request), name='index.html')
+  >>> print view()
+  Access to the requested resource is forbidden.
+
+The default views can be selectively overridden in your application::
+
+  >>> from grok import ExceptionView
+  >>> class MyExceptionView(ExceptionView):
+  ...     def render(self):
+  ...         return u'This is my idea of an exception view.'
+  >>> from grok.testing import grok_component
+  >>> grok_component('MyExceptionView', MyExceptionView)
+  True
+
+  >>> view = getMultiAdapter((Exception(), TestRequest()), name='index.html')
+  >>> print view()
+  This is my idea of an exception view.
+
+  >>> view.isSystemError()
+  True
+
+  >>> from grok import NotFoundView
+  >>> class MyNotFoundView(NotFoundView):
+  ...     def render(self):
+  ...         return u'This is my idea of a not found view.'
+  >>> from grok.testing import grok_component
+  >>> grok_component('MyNotFoundView', MyNotFoundView)
+  True
+
+  >>> request = TestRequest()
+  >>> error = NotFound(object(), request)
+  >>> view = getMultiAdapter((error, request), name='index.html')
+  >>> print view()
+  This is my idea of a not found view.
+
+  >>> from grok import UnauthorizedView
+  >>> class MyUnauthorizedView(UnauthorizedView):
+  ...     def render(self):
+  ...         return u'This is my idea of an unauthorized view.'
+  >>> from grok.testing import grok_component
+  >>> grok_component('MyUnauthorizedView', MyUnauthorizedView)
+  True
+
+  >>> request = TestRequest()
+  >>> request.setPrincipal(MockPrincipal())
+  >>> view = getMultiAdapter((Unauthorized(), request), name='index.html')
+  >>> print view()
+  This is my idea of an unauthorized view.
+
+"""
+import grok
+
+class MockPrincipal(object):
+    id = 'mockprincipal'

Modified: grok/trunk/src/grok/ftests/test_grok_functional.py
===================================================================
--- grok/trunk/src/grok/ftests/test_grok_functional.py	2011-01-19 17:11:48 UTC (rev 119725)
+++ grok/trunk/src/grok/ftests/test_grok_functional.py	2011-01-19 17:27:37 UTC (rev 119726)
@@ -62,6 +62,7 @@
     for name in [
         'application',
         'catalog',
+        'errorviews',
         'form',
         'lifecycle',
         'security',



More information about the checkins mailing list