Resource View: zope.browserresource.resources
¶
Resource URL access
-
class
zope.browserresource.resources.
Resources
(context, request)[source]¶ Bases:
zope.publisher.browser.BrowserView
A view that can be traversed further to access browser resources.
This view is usually registered for
zope.component.interfaces.ISite
objects with no name, so resources will be available at<site>/@@/<resource>
.Let’s test how it’s traversed to get registered resources. Let’s create a sample resource class and register it.
>>> from zope.component import provideAdapter >>> from zope.interface import Interface >>> from zope.publisher.interfaces import NotFound >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer >>> from zope.publisher.browser import TestRequest
>>> class Resource(object): ... def __init__(self,request): ... self.request = request ... def __call__(self): ... return 'http://localhost/testresource'
>>> provideAdapter(Resource, (IDefaultBrowserLayer,), Interface, 'test')
Now, create a site and request objects and get the Resources object to work with.
>>> site = object() >>> request = TestRequest() >>> resources = Resources(site, request)
Okay, let’s test the
publishTraverse
method. It should traverse to our registered resource.>>> resource = resources.publishTraverse(request, 'test') >>> resource.__parent__ is site True >>> resource.__name__ == 'test' True >>> resource() 'http://localhost/testresource'
However, it will raise
NotFound
exception if we try to traverse to an unregistered resource.>>> resources.publishTraverse(request, 'does-not-exist') Traceback (most recent call last): ... NotFound: Object: <zope.browserresource.resources.Resources object at 0x...>, name: 'does-not-exist'
When accessed without further traversing, it returns an empty page and no futher traversing steps.
>>> view, path = resources.browserDefault(request) >>> view() == b'' True >>> path == () True
The Resources view also provides
__getitem__
method for use in templates. It is equivalent topublishTraverse
.>>> resource = resources['test'] >>> resource.__parent__ is site True >>> resource.__name__ == 'test' True >>> resource() 'http://localhost/testresource'
-
publishTraverse
(request, name)[source]¶ Query for the default adapter on request named name and return it.
This is usually a
IResource
as registered withIResourceDirective
.The resource object is
located
beneath the context of this object with the given name.Raises: NotFound – If no adapter can be found. See also
zope.publisher.interfaces.browser.IBrowserPublisher
-