[ZDP] BackTalk to Document Zope Developer's Guide (2.4 edition)/Object Publishing

webmaster@zope.org webmaster@zope.org
Fri, 23 May 2003 07:36:25 -0400


A comment to the paragraph below was recently added via http://www.zope.org/Documentation/Books/ZDG/current/ObjectPublishing.stx#3-26

---------------

      Traversal Methods

        During traversal, 'ZPublisher' cuts the URL into path elements
        delimited by slashes, and uses each path element to traverse
        from the current object to the next object. 'ZPublisher'
        locates the next object in one of three ways:

          1. Using '__bobo_traverse__'

          2. Using 'getattr'

          3. Using dictionary access.

        First the publisher attempts to call the traversal hook
        method, '__bobo_traverse__'. If the current object has this
        method it is called with the request and the current path
        element. The method should return the next object or 'None' to
        indicate that a next object can't be found. You can also
        return a tuple of objects from '__bobo_traverse__' indicating
        a sequence of sub-objects. This allows you to add additional
        parent objects into the request. This is almost never
        necessary.

          % Anonymous User - Jan. 11, 2002 10:43 am - typo? method, --> method  maurer mentions also "__before_publishing_traverse__".

          % Anonymous User - June 4, 2002 11:10 am:
           It might be nice to have a footnote briefly describing where "bobo" came from.  Storks don't count.

          % Anonymous User - July 12, 2002 2:50 pm:
           It might be nicer to get rid of the "bobo" name altogether, why can't this be called __traverse__ ? Probably
           too much legacy code out there.

        Here's an example of how to use '__bobo_traverse__'::

          def __bobo_traverse__(self, request, key):
              # if there is a special cookie set, return special
              # subobjects, otherwise return normal subobjects

              if request.cookies.has_key('special'):
                  # return a subobject from the special dict
                  return self.special_subobjects.get(key, None)

              # otherwise return a subobject from the normal dict
              return self.normal_subobjects.get(key, None)

        This example shows how you can examine the request during
        the traversal process. 

        If the current object does not define a '__bobo_traverse__'
        method, then the next object is searched for using 'getattr'.
        This locates sub-objects in the normal Python sense.

        If the next object can't be found with 'getattr', 'ZPublisher'
        calls on the current object as though it were a
        dictionary. Note: the path element will be a string, not an
        integer, so you cannot traverse sequences using index numbers
        in the URL.

        For example, suppose 'a' is the current object, and 'next' is
        the name of the path element. Here are the three things that
        'ZPublisher' will try in order to find the next object:

          1. 'a.__bobo_traverse__("next")'

          2. 'a.next'

          3. 'a["next"]'

          % Anonymous User - Jan. 18, 2002 6:13 pm - In the definition above there are three arguments. Where is the "request"   argument here?

        If the next object isn't found by any of these means
        'ZPublisher' returns a HTTP 404 (Not Found) exception.

        % Anonymous User - May 23, 2003 7:36 am:
         blabla jackedy schmackedy