summaryrefslogtreecommitdiff
path: root/docs/narr/resources.rst
diff options
context:
space:
mode:
Diffstat (limited to 'docs/narr/resources.rst')
-rw-r--r--docs/narr/resources.rst79
1 files changed, 44 insertions, 35 deletions
diff --git a/docs/narr/resources.rst b/docs/narr/resources.rst
index f90b1eb12..a11466a87 100644
--- a/docs/narr/resources.rst
+++ b/docs/narr/resources.rst
@@ -1,21 +1,23 @@
+.. _resources_chapter:
+
Resources
=========
-A :term:`resource` is an object that represents a "place" in a tree related
-to your application. Every :app:`Pyramid` application has at least one
-resource object: the :term:`root` resource (even if you don't define one
-manually, a default root resource is created for you). The root resource is
-the root of a :term:`resource tree`. A resource tree is a set of nested
-dictionary-like objects which you can use to represent your website's
-structure.
+A :term:`resource` is an object that represents a "place" in a tree
+related to your application. Every :app:`Pyramid` application has at
+least one resource object: the :term:`root` resource. Even if you don't
+define a root resource manually, a default one is created for you. The
+root resource is the root of a :term:`resource tree`. A resource tree
+is a set of nested dictionary-like objects which you can use to
+represent your website's structure.
In an application which uses :term:`traversal` to map URLs to code, the
-resource tree structure is used heavily to map a URL to a :term:`view
-callable`. :app:`Pyramid` will walk "up" the resource tree by traversing
-through the nested dictionary structure of the tree when :term:`traversal` is
-used in order to find a :term:`context` resource. Once a context resource is
-found, the context resource and data in the request will be used to find a
-:term:`view callable`.
+resource tree structure is used heavily to map each URL to a :term:`view
+callable`. When :term:`traversal` is used, :app:`Pyramid` will walk
+through the resource tree by traversing through its nested dictionary
+structure in order to find a :term:`context` resource. Once a context
+resource is found, the context resource and data in the request will be
+used to find a :term:`view callable`.
In an application which uses :term:`URL dispatch`, the resource tree is only
used indirectly, and is often "invisible" to the developer. In URL dispatch
@@ -26,7 +28,7 @@ much less important in applications that use URL dispatch than applications
that use traversal.
In "Zope-like" :app:`Pyramid` applications, resource objects also often store
-data persistently and offer methods related to mutating that persistent data.
+data persistently, and offer methods related to mutating that persistent data.
In these kinds of applications, resources not only represent the site
structure of your website, but they become the :term:`domain model` of the
application.
@@ -34,8 +36,8 @@ application.
Also:
- The ``context`` and ``containment`` predicate arguments to
- :meth:`pyramid.config.Configurator.add_view` (or a
- :func:`pyramid.view.view_config` decorator) reference a resource class
+ :meth:`~pyramid.config.Configurator.add_view` (or a
+ :func:`~pyramid.view.view_config` decorator) reference a resource class
or resource :term:`interface`.
- A :term:`root factory` returns a resource.
@@ -44,7 +46,7 @@ Also:
view.
- Various helpful :app:`Pyramid` API methods expect a resource as an
- argument (e.g. :func:`pyramid.url.resource_url` and others).
+ argument (e.g. :func:`~pyramid.url.resource_url` and others).
.. index::
single: resource tree
@@ -72,8 +74,8 @@ tree:
the container's ``__getitem__`` should return the sub-resource.
- Leaf resources, which do not contain other resources, must not implement a
- ``__getitem__``, or if they do, their ``__getitem__`` method must raise a
- :exc:`KeyError`.
+ ``__getitem__``, or if they do, their ``__getitem__`` method must always
+ raise a :exc:`KeyError`.
See :ref:`traversal_chapter` for more information about how traversal
works against resource instances.
@@ -160,9 +162,9 @@ you will reach the filesystem root directory.
.. warning:: If your root resource has a ``__name__`` argument
that is not ``None`` or the empty string, URLs returned by the
- :func:`pyramid.url.resource_url` function and paths generated by
- the :func:`pyramid.traversal.resource_path` and
- :func:`pyramid.traversal.resource_path_tuple` APIs will be
+ :func:`~pyramid.url.resource_url` function and paths generated by
+ the :func:`~pyramid.traversal.resource_path` and
+ :func:`~pyramid.traversal.resource_path_tuple` APIs will be
generated improperly. The value of ``__name__`` will be prepended
to every path and URL generated (as opposed to a single leading
slash or empty tuple element).
@@ -184,7 +186,7 @@ you will reach the filesystem root directory.
objects "by hand". Instead, as necessary, during traversal :app:`Pyramid`
will wrap each resource (even the root resource) in a ``LocationProxy``
which will dynamically assign a ``__name__`` and a ``__parent__`` to the
- traversed resrouce (based on the last traversed resource and the name
+ 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``.
@@ -207,6 +209,8 @@ location-aware.
single: resource_url
pair: generating; resource url
+.. _generating_the_url_of_a_resource:
+
Generating The URL Of A Resource
--------------------------------
@@ -227,7 +231,7 @@ The simplest call to :func:`~pyramid.url.resource_url` looks like this:
url = resource_url(resource, request)
The ``request`` passed to ``resource_url`` in the above example is an
-instance of an :app:`Pyramid` :term:`request` object.
+instance of a :app:`Pyramid` :term:`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
@@ -239,7 +243,7 @@ A slash is appended to all resource URLs when
:func:`~pyramid.url.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 typically more
+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 :func:`~pyramid.url.resource_url`:
@@ -277,7 +281,7 @@ about virtually rooting a resource.
The shortcut method of the :term:`request` named
:meth:`pyramid.request.Request.resource_url` can be used instead of
-:func:`pyramid.url.resource_url` to generate a resource URL.
+:func:`~pyramid.url.resource_url` to generate a resource URL.
For more information about generating resource URLs, see the documentation
for :func:`pyramid.url.resource_url`.
@@ -288,13 +292,13 @@ Overriding Resource URL Generation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If a resource object implements a ``__resource_url__`` method, this method
-will be called when :func:`pyramid.url.resource_url` is called to generate a
+will be called when :func:`~pyramid.url.resource_url` is called to generate a
URL for the resource, overriding the default URL returned for the resource by
:func:`~pyramid.url.resource_url`.
The ``__resource_url__`` hook is passed two arguments: ``request`` and
``info``. ``request`` is the :term:`request` object passed to
-:func:`pyramid.url.resource_url`. ``info`` is a dictionary with two
+:func:`~pyramid.url.resource_url`. ``info`` is a dictionary with two
keys:
``physical_path``
@@ -324,6 +328,11 @@ would have been what was returned anyway, but your code can perform arbitrary
logic as necessary. For example, your code may wish to override the hostname
or port number of the generated URL.
+Note that the URL generated by ``__resource_url__`` should be fully
+qualified, should end in a slash, and should not contain any query string or
+anchor elements (only path elements) to work best with
+:func:`~pyramid.url.resource_url`.
+
Generating the Path To a Resource
---------------------------------
@@ -341,7 +350,7 @@ character.
If ``resource`` in the example above was accessible in the tree as
``root['a']['b']``, the above example would generate the string ``/a/b``.
-Any positional arguments passed in to :func:`pyramid.traversal.resource_path`
+Any positional arguments passed in to :func:`~pyramid.traversal.resource_path`
will be appended as path segments to the end of the resource path.
.. code-block:: python
@@ -399,7 +408,7 @@ Obtaining the Lineage of a Resource
-----------------------------------
:func:`pyramid.location.lineage` returns a generator representing the
-:term:`lineage` of the :term:`location` aware:term:`resource` object.
+:term:`lineage` of the :term:`location` aware :term:`resource` object.
The :func:`~pyramid.location.lineage` function returns the resource it is
passed, then each parent of the resource, in order. For example, if the
@@ -528,7 +537,7 @@ declares that the blog entry implements an :term:`interface`.
implements(IBlogEntry)
def __init__(self, title, body, author):
self.title = title
- self.body = body
+ self.body = body
self.author = author
self.created = datetime.datetime.now()
@@ -563,7 +572,7 @@ To do so, use the :func:`zope.interface.directlyProvides` function:
class BlogEntry(object):
def __init__(self, title, body, author):
self.title = title
- self.body = body
+ self.body = body
self.author = author
self.created = datetime.datetime.now()
@@ -591,7 +600,7 @@ the :func:`zope.interface.alsoProvides` function:
class BlogEntry(object):
def __init__(self, title, body, author):
self.title = title
- self.body = body
+ self.body = body
self.author = author
self.created = datetime.datetime.now()
@@ -609,7 +618,7 @@ configuration, see :ref:`using_resource_interfaces`.
Finding a Resource With a Class or Interface in Lineage
-------------------------------------------------------
-Use the :func:`pyramid.traversal.find_interface` API to locate a parent that
+Use the :func:`~pyramid.traversal.find_interface` API to locate a parent that
is of a particular Python class, or which implements some :term:`interface`.
For example, if your resource tree is composed as follows:
@@ -660,7 +669,7 @@ These can be used to walk down a resource tree, or conveniently locate one
resource "inside" another.
Some APIs in :ref:`security_module` accept a resource object as a parameter.
-For example, the :func:`pyramid.security.has_permission` API accepts a
+For example, the :func:`~pyramid.security.has_permission` API accepts a
resource object as one of its arguments; the ACL is obtained from this
resource or one of its ancestors. Other APIs in the :mod:`pyramid.security`
module also accept :term:`context` as an argument, and a context is always a