[Checkins] SVN: z3c.bobopublisher/trunk/src/z3c support the 'attribute' field for the bobo:page directive

Fabio Tranchitella kobold at kobold.it
Mon Aug 17 05:14:27 EDT 2009


Log message for revision 102869:
  support the 'attribute' field for the bobo:page directive

Changed:
  U   z3c.bobopublisher/trunk/src/z3c/bobopublisher/README.txt
  U   z3c.bobopublisher/trunk/src/z3c/bobopublisher/browser.py
  U   z3c.bobopublisher/trunk/src/z3c/bobopublisher/metaconfigure.py
  U   z3c.bobopublisher/trunk/src/z3c/bobopublisher/metadirectives.py
  U   z3c.bobopublisher/trunk/src/z3c/bobopublisher/tests.py
  U   z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/PKG-INFO

-=-
Modified: z3c.bobopublisher/trunk/src/z3c/bobopublisher/README.txt
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/README.txt	2009-08-16 22:47:50 UTC (rev 102868)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/README.txt	2009-08-17 09:14:26 UTC (rev 102869)
@@ -235,11 +235,19 @@
     ...       name="something.html"
     ...       for="zope.location.interfaces.IRoot"
     ...       class="z3c.bobopublisher.tests.TestBrowserPage"
+    ...       permission="zope.Public"
     ...       />
     ...   <page
+    ...       name="attribute.html"
+    ...       for="zope.location.interfaces.IRoot"
+    ...       class="z3c.bobopublisher.tests.TestAttributeBrowserPage"
+    ...       attribute="attribute"
+    ...       />
+    ...   <page
     ...       name="delete.html"
     ...       for="zope.location.interfaces.IRoot"
     ...       class="z3c.bobopublisher.tests.TestBrowserPage"
+    ...       permission="zope.Public"
     ...       methods="DELETE"
     ...       />
     ...   <defaultView
@@ -254,6 +262,9 @@
     >>> testapp.get('/something.html', status=200).body
     'TEST PAGE'
 
+    >>> testapp.get('/attribute.html', status=200).body
+    'ATTRIBUTE'
+
 As shown above, it is possible to register browser pages for one or more
 specific HTTP methods:
 
@@ -286,10 +297,12 @@
     ...   <resources
     ...       name="images"
     ...       directory="%s"
+    ...       permission="zope.Public"
     ...       />
     ...   <resource
     ...       name="resource.txt"
     ...       file="%s/resource.txt"
+    ...       permission="zope.Public"
     ...       />
     ... </configure>
     ... """ % (tempdir, tempdir), context=context, execute=True)
@@ -335,3 +348,8 @@
 
     >>> os.unlink(os.path.join(tempdir, 'resource.txt'))
     >>> os.rmdir(tempdir)
+
+Clean-up the global registries:
+
+    >>> from zope.testing.cleanup import cleanUp
+    >>> cleanUp()

Modified: z3c.bobopublisher/trunk/src/z3c/bobopublisher/browser.py
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/browser.py	2009-08-16 22:47:50 UTC (rev 102868)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/browser.py	2009-08-17 09:14:26 UTC (rev 102869)
@@ -25,9 +25,13 @@
 
     implements(IBrowserView)
 
-    def __init__(self, context, request):
+    def __init__(self, context, request, attribute=None):
         self.context = context
         self.request = request
+        self._page_attribute = attribute
 
     def __call__(self):
+        if hasattr(self, '_page_attribute') and \
+           self._page_attribute is not None:
+            return getattr(self, self._page_attribute)()
         raise NotImplemented, '__call__ method not implemented'

Modified: z3c.bobopublisher/trunk/src/z3c/bobopublisher/metaconfigure.py
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/metaconfigure.py	2009-08-16 22:47:50 UTC (rev 102868)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/metaconfigure.py	2009-08-17 09:14:26 UTC (rev 102869)
@@ -28,11 +28,24 @@
 from zope.configuration.exceptions import ConfigurationError
 from zope.interface import Interface
 from zope.location.interfaces import IRoot
+from zope.security.checker import CheckerPublic
+from zope.security.interfaces import Unauthorized
+from zope.security.management import checkPermission
 
 
+def _page_factory(class_, name, permission, attribute=None):
+    def factory(context, request):
+        if permission is not None and \
+           not checkPermission(permission, context):
+            raise Unauthorized(context, name, permission)
+        if attribute is not None:
+            return class_(context, request, attribute)
+        return class_(context, request)
+    return factory
 
+
 def page(_context, name='index.html', for_=None, class_=None, permission=None,
-    methods=None):
+    methods=None, attribute=None):
     requests = []
     if methods == None:
         requests.append(IRequest)
@@ -46,6 +59,13 @@
                 requests.append(IPUTRequest)
             elif m == 'DELETE':
                 requests.append(IDELETERequest)
+    if attribute is not None and not hasattr(class_, attribute):
+        raise ConfigurationError("The provided class doesn't have the "
+            "specified attribute")
+    elif permission is not None or attribute is not None:
+        if permission == 'zope.Public':
+            permission = CheckerPublic
+        class_ = _page_factory(class_, name, permission, attribute)
     for request in requests:
         _context.action(
             discriminator = ('page', for_, name),
@@ -56,10 +76,14 @@
         )
 
 def resources(_context, name, directory, for_=IRoot, permission=None):
+    if not os.path.isdir(directory):
+        raise ConfigurationError('Directory %s does not exist' % directory)
+    if permission == 'zope.Public':
+        permission = CheckerPublic
     def resourcesFactory(context, request):
+        if not checkPermission(permission, context):
+            raise Unauthorized(context, name, permission)
         return Directory(directory)
-    if not os.path.isdir(directory):
-        raise ConfigurationError('Directory %s does not exist' % directory)
     _context.action(
         discriminator = ('page', for_, name),
         callable = handler,
@@ -71,15 +95,19 @@
 
 
 def resource(_context, name, file, for_=IRoot, permission=None):
-    def resourcesFactory(context, request):
-        return File(file)
     if not os.path.isfile(file):
         raise ConfigurationError('File %s does not exist' % directory)
+    if permission == 'zope.Public':
+        permission = CheckerPublic
+    def resourceFactory(context, request):
+        if not checkPermission(permission, context):
+            raise Unauthorized(context, name, permission)
+        return File(file)
     _context.action(
         discriminator = ('page', for_, name),
         callable = handler,
         args = ('registerAdapter',
-            resourcesFactory, (for_, IGETRequest), Interface, name,
+            resourceFactory, (for_, IGETRequest), Interface, name,
             _context.info,
         ),
     )

Modified: z3c.bobopublisher/trunk/src/z3c/bobopublisher/metadirectives.py
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/metadirectives.py	2009-08-16 22:47:50 UTC (rev 102868)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/metadirectives.py	2009-08-17 09:14:26 UTC (rev 102869)
@@ -17,7 +17,7 @@
 """
 
 from zope.configuration.fields import GlobalInterface, GlobalObject, Path, \
-    Tokens
+    PythonIdentifier, Tokens
 from zope.interface import Interface
 from zope.location.interfaces import IRoot
 from zope.schema import TextLine, BytesLine
@@ -57,7 +57,13 @@
         default=None,
     )
 
+    attribute = PythonIdentifier(
+        title=u"The name of the view attribute implementing the page.",
+        required=False,
+        default=u'__call__',
+    )
 
+
 class IResourcesDirective(Interface):
     """bobo:resources directive"""
 

Modified: z3c.bobopublisher/trunk/src/z3c/bobopublisher/tests.py
===================================================================
--- z3c.bobopublisher/trunk/src/z3c/bobopublisher/tests.py	2009-08-16 22:47:50 UTC (rev 102868)
+++ z3c.bobopublisher/trunk/src/z3c/bobopublisher/tests.py	2009-08-17 09:14:26 UTC (rev 102869)
@@ -47,3 +47,10 @@
 
     def __call__(self):
         return u'TEST PAGE'
+
+
+class TestAttributeBrowserPage(BrowserPage):
+    """Test browser page"""
+
+    def attribute(self):
+        return u'ATTRIBUTE'

Modified: z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/PKG-INFO
===================================================================
--- z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/PKG-INFO	2009-08-16 22:47:50 UTC (rev 102868)
+++ z3c.bobopublisher/trunk/src/z3c.bobopublisher.egg-info/PKG-INFO	2009-08-17 09:14:26 UTC (rev 102869)
@@ -243,11 +243,19 @@
         ...       name="something.html"
         ...       for="zope.location.interfaces.IRoot"
         ...       class="z3c.bobopublisher.tests.TestBrowserPage"
+        ...       permission="zope.Public"
         ...       />
         ...   <page
+        ...       name="attribute.html"
+        ...       for="zope.location.interfaces.IRoot"
+        ...       class="z3c.bobopublisher.tests.TestAttributeBrowserPage"
+        ...       attribute="attribute"
+        ...       />
+        ...   <page
         ...       name="delete.html"
         ...       for="zope.location.interfaces.IRoot"
         ...       class="z3c.bobopublisher.tests.TestBrowserPage"
+        ...       permission="zope.Public"
         ...       methods="DELETE"
         ...       />
         ...   <defaultView
@@ -262,6 +270,9 @@
         >>> testapp.get('/something.html', status=200).body
         'TEST PAGE'
         
+        >>> testapp.get('/attribute.html', status=200).body
+        'ATTRIBUTE'
+        
         As shown above, it is possible to register browser pages for one or more
         specific HTTP methods:
         
@@ -294,10 +305,12 @@
         ...   <resources
         ...       name="images"
         ...       directory="%s"
+        ...       permission="zope.Public"
         ...       />
         ...   <resource
         ...       name="resource.txt"
         ...       file="%s/resource.txt"
+        ...       permission="zope.Public"
         ...       />
         ... </configure>
         ... """ % (tempdir, tempdir), context=context, execute=True)
@@ -344,7 +357,12 @@
         >>> os.unlink(os.path.join(tempdir, 'resource.txt'))
         >>> os.rmdir(tempdir)
         
+        Clean-up the global registries:
         
+        >>> from zope.testing.cleanup import cleanUp
+        >>> cleanUp()
+        
+        
         CHANGES
         =======
         



More information about the Checkins mailing list