Skip to content

Commit

Permalink
Merge pull request #117 from stevepiercy/master
Browse files Browse the repository at this point in the history
minor grammar, add rst markup, correct narrative, wrap 79 columns
  • Loading branch information
stevepiercy committed Dec 16, 2015
2 parents 2a9b292 + 752586b commit 1ab60e9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
67 changes: 30 additions & 37 deletions quick_traversal/hierarchy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ Objects with subobjects and views, all via URLs.
Background
==========

In :doc:`siteroot` we took the simplest possible step: a
root object with little need for the stitching-together of a tree known
as traversal.
In :doc:`siteroot` we took the simplest possible step: a root object with
little need for the stitching together of a tree known as traversal.

In this step we remain simple, but make a basic hierarchy::

Expand All @@ -23,11 +22,11 @@ In this step we remain simple, but make a basic hierarchy::
Objectives
==========

- Multi-level nested hierarchy of Python objects
- Use a multi-level nested hierarchy of Python objects.

- Show how ``__name__`` and ``__parent__`` glue the hierarchy together
- Show how ``__name__`` and ``__parent__`` glue the hierarchy together.

- Objects which last between requests
- Use objects which last between requests.

Steps
=====
Expand All @@ -39,51 +38,45 @@ Steps
$ cd ..; cp -r siteroot hierarchy; cd hierarchy
$ $VENV/bin/python setup.py develop
#. Provide a richer set of objects in
``hierarchy/tutorial/resources.py``:
#. Provide a richer set of objects in ``hierarchy/tutorial/resources.py``:

.. literalinclude:: hierarchy/tutorial/resources.py
:linenos:

#. Have ``hierarchy/tutorial/views.py`` show information about
the resource tree:
#. Have ``hierarchy/tutorial/views.py`` show information about the resource
tree:

.. literalinclude:: hierarchy/tutorial/views.py
:linenos:
:emphasize-lines: 1,9

#. Update the ``hierarchy/tutorial/templates/home.jinja2``
view template:
#. Update the ``hierarchy/tutorial/templates/home.jinja2`` view template:

.. literalinclude:: hierarchy/tutorial/templates/home.jinja2
:language: html
:language: jinja
:linenos:
:emphasize-lines: 4-12

#. Update the ``hierarchy/tutorial/templates/hello.jinja2`` view
template as well:

.. literalinclude:: hierarchy/tutorial/templates/hello.jinja2
:language: html
:linenos:

#. The ``hierarchy/tutorial/templates/breadcrumbs.jinja2`` template now
has a hierarchy to show:
#. The ``hierarchy/tutorial/templates/breadcrumbs.jinja2`` template now has a
hierarchy to show:

.. literalinclude:: hierarchy/tutorial/templates/breadcrumbs.jinja2
:language: html
:language: jinja
:linenos:

#. Update the tests in ``hierarchy/tutorial/tests.py``:

.. literalinclude:: hierarchy/tutorial/tests.py
:linenos:
:emphasize-lines: 8,13,26-28

#. Now run the tests:

.. code-block:: bash
$ $VENV/bin/nosetests tutorial
.
..
----------------------------------------------------------------------
Ran 2 tests in 0.141s
Expand All @@ -95,29 +88,29 @@ Steps
$ $VENV/bin/pserve development.ini --reload
#. Open ``http://localhost:6543/`` in your browser.
#. Open http://localhost:6543/ in your browser.

Analysis
========

In this example we have to manage our tree by assigning ``__name__`` as
an identifier on each child and ``__parent__`` as a reference to the
parent. The template used now shows different information based on the
object URL which you traversed to.
In this example we have to manage our tree by assigning ``__name__`` as an
identifier on each child, and ``__parent__`` as a reference to the parent. The
template used now shows different information based on the object URL to which
you traversed.

We also show that ``@view_config`` can set a "default" view on a
context by omitting the ``@name`` attribute. Thus, if you visit
``http://localhost:6543/folder1/`` without providing anything after,
the configured default view is used.
We also show that ``@view_config`` can set a "default" view on a context by
omitting the ``@name`` attribute. Thus, if you visit
``http://localhost:6543/folder1/`` without providing anything after, the
configured default view is used.

Extra Credit
============

#. In ``resources.py``, we moved the instantiation of ``root`` out to
global scope. Why?
#. In ``resources.py``, we moved the instantiation of ``root`` out to global
scope. Why?

#. If you go to a resource that doesn't exist, will Pyramid handle it
gracefully?

#. If you ask for a default view on a resource and none is configured,
Pyramid handle it gracefully?
#. If you ask for a default view on a resource and none is configured, will
Pyramid handle it gracefully?
22 changes: 11 additions & 11 deletions quick_traversal/hierarchy/tutorial/templates/home.jinja2
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{% extends "templates/layout.jinja2" %}
{% block content %}

<ul>
<li><a href="/">Site Folder</a></li>
<li><a href="/doc1">Document 01</a></li>
<li><a href="/doc2">Document 02</a></li>
<li><a href="/folder1">Folder 01</a></li>
<li><a href="/folder1/doc1">Document 01 in Folder 01</a></li>
</ul>
<ul>
<li><a href="/">Site Folder</a></li>
<li><a href="/doc1">Document 01</a></li>
<li><a href="/doc2">Document 02</a></li>
<li><a href="/folder1">Folder 01</a></li>
<li><a href="/folder1/doc1">Document 01 in Folder 01</a></li>
</ul>

<h2>{{ context.title }}</h2>
<h2>{{ context.title }}</h2>

<p>Welcome to {{ context.title }}. Visit
<a href="{{ request.resource_url(context, 'hello') }}">hello</a>
</p>
<p>Welcome to {{ context.title }}. Visit
<a href="{{ request.resource_url(context, 'hello') }}">hello</a>
</p>

{% endblock content %}
8 changes: 5 additions & 3 deletions quick_traversal/hierarchy/tutorial/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
from pyramid.testing import DummyRequest
from pyramid.testing import DummyResource


class TutorialViewsUnitTests(unittest.TestCase):
def test_home_view(self):
from .views import TutorialViews

request = DummyRequest()
title = "Dummy Context"
title = 'Dummy Context'
context = DummyResource(title=title, __name__='dummy')
inst = TutorialViews(context, request)
result = inst.home()
self.assertIn('Home', result['page_title'])


class TutorialFunctionalTests(unittest.TestCase):
def setUp(self):
from tutorial import main
Expand All @@ -22,5 +24,5 @@ def setUp(self):
self.testapp = TestApp(app)

def test_home(self):
res = self.testapp.get('/', status=200)
self.assertIn(b'Site Folder', res.body)
result = self.testapp.get('/', status=200)
self.assertIn(b'Site Folder', result.body)
5 changes: 3 additions & 2 deletions quick_traversal/siteroot/tutorial/templates/home.jinja2
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{% extends "templates/layout.jinja2" %}
{% block content %}

<p>Welcome to {{ context.title }}. Visit
<a href="{{ request.resource_url(context, 'hello') }}">hello</a></p>
<p>Welcome to {{ context.title }}. Visit
<a href="{{ request.resource_url(context, 'hello') }}">hello</a>
</p>

{% endblock content %}

0 comments on commit 1ab60e9

Please sign in to comment.