Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
11
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

24. RESOURCES

Using pyramid_traversalwrapper

If you’d rather not manage the __name__ and __parent__ attributes of your resources “by hand”, an add-on package named pyramid_traversalwrapper can help.

In order to use this helper feature, you must first install the pyramid_traversalwrapper package (available via PyPI), then register its ModelGraphTraverser as the traversal policy, rather than the default Pyramid traverser. The package contains instructions for doing so.

Once Pyramid is configured with this feature, you will no longer need to manage the __parent__ and __name__ attributes on resource objects “by hand”. Instead, as necessary, during traversal Pyramid will wrap each resource (even the root resource) in a LocationProxy which will dynamically assign a __name__ and a __parent__ to the traversed resource (based on the last traversed resource and the name supplied to __getitem__). The root resource will have a __name__ attribute of None and a __parent__ attribute of None.

Applications

which

use tree-walking Pyramid

APIs require location-aware resources.

These APIs

include

(but are not limited to)

resource_url(), find_resource(),

find_root(), find_interface(), resource_path(),

resource_path_tuple(),

or traverse(),

virtual_root(),

and

(usually)

has_permission() and

principals_allowed_by_permission().

 

 

 

In general, since so much Pyramid infrastructure depends on location-aware resources, it’s a good idea to make each resource in your tree location-aware.

24.3 Generating The URL Of A Resource

If your resources are location aware, you can use the pyramid.request.Request.resource_url()

API to generate a URL for the resource. This URL will use the resource’s position in the parent tree to create a resource path, and it will prefix the path with the current application URL to form a fully-qualified URL with the scheme, host, port, and path. You can also pass extra arguments to resource_url() to influence the generated URL.

The simplest call to resource_url() looks like this:

1 url = request.resource_url(resource)

The request in the above example is an instance of a Pyramid request object.

If the resource referred to as resource in the above example was the root resource, and the host that was used to contact the server was example.com, the URL generated would be

264

24.3. GENERATING THE URL OF A RESOURCE

http://example.com/. However, if the resource was a child of the root resource named a, the generated URL would be http://example.com/a/.

A slash is appended to all resource URLs when resource_url() is used to generate them in this simple manner, because resources are “places” in the hierarchy, and URLs are meant to be clicked on to be visited. Relative URLs that you include on HTML pages rendered as the result of the default view of a resource are more apt to be relative to these resources than relative to their parent.

You can also pass extra elements to resource_url():

1 url = request.resource_url(resource, ’foo’, ’bar’)

If

the resource referred to as resource in

the

above example

was the root resource, and

the

host that was used to contact the server

was

example.com,

the URL generated would

be http://example.com/foo/bar. Any number of extra elements can be passed to resource_url() as extra positional arguments. When extra elements are passed, they are appended to the resource’s URL. A slash is not appended to the final segment when elements are passed.

You can also pass a query string:

1 url = request.resource_url(resource, query={’a’:’1’})

If the resource referred to as resource in the above example was the root resource, and the host that was used to contact the server was example.com, the URL generated would be http://example.com/?a=1.

When a virtual root is active, the URL generated by resource_url() for a resource may be “shorter” than its physical tree path. See Virtual Root Support for more information about virtually rooting a resource.

For more information about generating resource URLs, see the documentation for pyramid.request.Request.resource_url().

24.3.1 Overriding Resource URL Generation

If a resource object implements a __resource_url__ method, this method will be called when resource_url() is called to generate a URL for the resource, overriding the default URL returned for the resource by resource_url().

The __resource_url__ hook is passed two arguments: request and info. request is the request object passed to resource_url(). info is a dictionary with two keys:

265

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]