Chris McDonough
2012-03-14 03f59aca72636b637ddaeb958e6a6db0dc3998c1
Merge branch 'master' of github.com:Pylons/pyramid
2 files deleted
2 files copied
10 files modified
3 files renamed
274 ■■■■■ changed files
docs/tutorials/wiki2/definingmodels.rst 28 ●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/design.rst 110 ●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/installation.rst 18 ●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/authorization/setup.py 2 ●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/authorization/tutorial/scripts/initializedb.py patch | view | raw | blame | history
docs/tutorials/wiki2/src/authorization/tutorial/scripts/populate.py 35 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/basiclayout/setup.py 2 ●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/basiclayout/tutorial/scripts/initializedb.py patch | view | raw | blame | history
docs/tutorials/wiki2/src/models/setup.py 2 ●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/models/tutorial/scripts/initializedb.py patch | view | raw | blame | history
docs/tutorials/wiki2/src/tests/setup.py 2 ●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/tests/tutorial/scripts/initializedb.py patch | view | raw | blame | history
docs/tutorials/wiki2/src/tests/tutorial/scripts/populate.py 36 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/tests/tutorial/tests.py 20 ●●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/views/setup.py 2 ●●● patch | view | raw | blame | history
docs/tutorials/wiki2/src/views/tutorial/scripts/initializedb.py patch | view | raw | blame | history
docs/tutorials/wiki2/tests.rst 17 ●●●● patch | view | raw | blame | history
docs/tutorials/wiki2/definingmodels.rst
@@ -54,48 +54,48 @@
which needs to be unique within the column.  The ``data`` attribute is a text
attribute that will hold the body of each page.
Changing ``scripts/populate.py``
--------------------------------
Changing ``scripts/initializedb.py``
------------------------------------
We haven't looked at the guts of this file yet, but within the ``scripts``
directory of your ``tutorial`` package is a file named ``populate.py``.  Code
in this file is executed whenever we run the ``populate_tutorial`` command
directory of your ``tutorial`` package is a file named ``initializedb.py``.  Code
in this file is executed whenever we run the ``initialize_tutorial_db`` command
(as we did in the installation step of this tutorial).
Since we've changed our model, we need to make changes to our ``populate.py``
Since we've changed our model, we need to make changes to our ``initializedb.py``
script.  In particular, we'll replace our import of ``MyModel`` with one of
``Page`` and we'll change the very end of the script to create a ``Page``
rather than a ``MyModel`` and add it to our ``DBSession``.
Open ``tutorial/tutorial/scripts/populate.py`` and edit it to look like the
Open ``tutorial/tutorial/scripts/initializedb.py`` and edit it to look like the
following:
.. literalinclude:: src/models/tutorial/scripts/populate.py
.. literalinclude:: src/models/tutorial/scripts/initializedb.py
   :linenos:
   :language: python
   :emphasize-lines: 14,34
(Only the highlighted lines need to be changed.)
Repopulating the Database
-------------------------
Reinitializing the Database
---------------------------
Because our model has changed, in order to repopulate the database, we need
to rerun the ``populate_tutorial`` command to pick up the changes you've made
to both the models.py file and to the populate.py file.  From the root of the
Because our model has changed, in order to reinitialize the database, we need
to rerun the ``initialize_tutorial_db`` command to pick up the changes you've made
to both the models.py file and to the initializedb.py file.  From the root of the
``tutorial`` project, directory execute the following commands.
On UNIX:
.. code-block:: text
   $ ../bin/populate_tutorial development.ini
   $ ../bin/initialize_tutorial_db development.ini
On Windows:
.. code-block:: text
   c:\pyramidtut\tutorial> ..\Scripts\populate_tutorial development.ini
   c:\pyramidtut\tutorial> ..\Scripts\initialize_tutorial_db development.ini
Success will look something like this::
docs/tutorials/wiki2/design.rst
@@ -65,63 +65,63 @@
Summary
-------
The URL, context, actions, template and permission associated to each view are
The URL, actions, template and permission associated to each view are
listed in the following table:
+----------------------+-------------+-----------------+-----------------------+------------+------------+
| URL                  |  View       |  Context        |  Action               |  Template  | Permission |
|                      |             |                 |                       |            |            |
+======================+=============+=================+=======================+============+============+
| /                    |  view_wiki  |  Wiki           |  Redirect to          |            |            |
|                      |             |                 |  /FrontPage           |            |            |
+----------------------+-------------+-----------------+-----------------------+------------+------------+
| /PageName            |  view_page  |  Page           |  Display existing     |  view.pt   |  view      |
|                      |  [1]_       |                 |  page [2]_            |            |            |
|                      |             |                 |                       |            |            |
|                      |             |                 |                       |            |            |
|                      |             |                 |                       |            |            |
+----------------------+-------------+-----------------+-----------------------+------------+------------+
| /edit_page/PageName  |  edit_page  |  Page           |  Display edit form    |  edit.pt   |  edit      |
|                      |             |                 |  with existing        |            |            |
|                      |             |                 |  content.             |            |            |
|                      |             |                 |                       |            |            |
|                      |             |                 |  If the form was      |            |            |
|                      |             |                 |  submitted, redirect  |            |            |
|                      |             |                 |  to /PageName         |            |            |
+----------------------+-------------+-----------------+-----------------------+------------+------------+
| /add_page/PageName   |  add_page   |  Wiki           |  Create the page      |  edit.pt   |  edit      |
|                      |             |                 |  *PageName* in        |            |            |
|                      |             |                 |  storage,  display    |            |            |
|                      |             |                 |  the edit form        |            |            |
|                      |             |                 |  without content.     |            |            |
|                      |             |                 |                       |            |            |
|                      |             |                 |  If the form was      |            |            |
|                      |             |                 |  submitted,           |            |            |
|                      |             |                 |  redirect to          |            |            |
|                      |             |                 |  /PageName            |            |            |
+----------------------+-------------+-----------------+-----------------------+------------+------------+
| /login               |  login      |  Wiki,          |  Display login form.  |  login.pt  |            |
|                      |             |  Forbidden [3]_ |                       |            |            |
|                      |             |                 |  If the form was      |            |            |
|                      |             |                 |  submitted,           |            |            |
|                      |             |                 |  authenticate.        |            |            |
|                      |             |                 |                       |            |            |
|                      |             |                 |  - If authentication  |            |            |
|                      |             |                 |    successful,        |            |            |
|                      |             |                 |    redirect to the    |            |            |
|                      |             |                 |    page that we       |            |            |
|                      |             |                 |    came from.         |            |            |
|                      |             |                 |                       |            |            |
|                      |             |                 |  - If authentication  |            |            |
|                      |             |                 |    fails, display     |            |            |
|                      |             |                 |    login form with    |            |            |
|                      |             |                 |    "login failed"     |            |            |
|                      |             |                 |    message.           |            |            |
|                      |             |                 |                       |            |            |
+----------------------+-------------+-----------------+-----------------------+------------+------------+
| /logout              |  logout     |  Wiki           |  Redirect to          |            |            |
|                      |             |                 |  /FrontPage           |            |            |
+----------------------+-------------+-----------------+-----------------------+------------+------------+
+----------------------+-----------------------+-------------+------------+------------+
| URL                  |  Action               |  View       |  Template  | Permission |
|                      |                       |             |            |            |
+======================+=======================+=============+============+============+
| /                    |  Redirect to          |  view_wiki  |            |            |
|                      |  /FrontPage           |             |            |            |
+----------------------+-----------------------+-------------+------------+------------+
| /PageName            |  Display existing     |  view_page  |  view.pt   |  view      |
|                      |  page [2]_            |  [1]_       |            |            |
|                      |                       |             |            |            |
|                      |                       |             |            |            |
|                      |                       |             |            |            |
+----------------------+-----------------------+-------------+------------+------------+
| /edit_page/PageName  |  Display edit form    |  edit_page  |  edit.pt   |  edit      |
|                      |  with existing        |             |            |            |
|                      |  content.             |             |            |            |
|                      |                       |             |            |            |
|                      |  If the form was      |             |            |            |
|                      |  submitted, redirect  |             |            |            |
|                      |  to /PageName         |             |            |            |
+----------------------+-----------------------+-------------+------------+------------+
| /add_page/PageName   |  Create the page      |  add_page   |  edit.pt   |  edit      |
|                      |  *PageName* in        |             |            |            |
|                      |  storage,  display    |             |            |            |
|                      |  the edit form        |             |            |            |
|                      |  without content.     |             |            |            |
|                      |                       |             |            |            |
|                      |  If the form was      |             |            |            |
|                      |  submitted,           |             |            |            |
|                      |  redirect to          |             |            |            |
|                      |  /PageName            |             |            |            |
+----------------------+-----------------------+-------------+------------+------------+
| /login               |  Display login form.  |  login      |  login.pt  |            |
|                      |                       |             |            |            |
|                      |  If the form was      |             |            |            |
|                      |  submitted,           |             |            |            |
|                      |  authenticate.        |             |            |            |
|                      |                       |             |            |            |
|                      |  - If authentication  |             |            |            |
|                      |    successful,        |             |            |            |
|                      |    redirect to the    |             |            |            |
|                      |    page that we       |             |            |            |
|                      |    came from.         |             |            |            |
|                      |                       |             |            |            |
|                      |  - If authentication  |             |            |            |
|                      |    fails, display     |             |            |            |
|                      |    login form with    |             |            |            |
|                      |    "login failed"     |             |            |            |
|                      |    message.           |             |            |            |
|                      |                       |             |            |            |
+----------------------+-----------------------+-------------+------------+------------+
| /logout              |  Redirect to          |  logout     |            |            |
|                      |  /FrontPage           |             |            |            |
+----------------------+-----------------------+-------------+------------+------------+
.. [1] This is the default view for a Page context
       when there is no view name.
docs/tutorials/wiki2/installation.rst
@@ -96,9 +96,9 @@
Success executing this command will end with a line to the console something
like::
   Please run the "populate_tutorial" script to set up the SQL
   Please run the "initialize_tutorial_db" script to set up the SQL
   database before starting the application (e.g. 
   "$myvirtualenv/bin/populate_tutorial development.ini".)
   "$myvirtualenv/bin/initialize_tutorial_db development.ini".)
Installing the Project in "Development Mode"
============================================
@@ -242,8 +242,8 @@
This means the server is ready to accept requests.
Populating the Database
=======================
Initializing the Database
=========================
In a web browser, visit ``http://localhost:6543/``. 
@@ -255,9 +255,9 @@
Oh no!  Something isn't working!
This happens because we haven't populated the SQL database with any table
information yet.  We need to use the ``populate_tutorial`` :term:`console
script` to populate our database before we can see the page render correctly.
This happens because we haven't initialized the SQL database with any table
information yet.  We need to use the ``initialize_tutorial_db`` :term:`console
script` to initialize our database before we can see the page render correctly.
Stop the running Pyramid application by pressing ``ctrl-C`` in the console.
Make sure you're still in the ``tutorial`` directory (the directory with a
@@ -267,13 +267,13 @@
.. code-block:: text
   $ ../bin/populate_tutorial development.ini
   $ ../bin/initialize_tutorial_db development.ini
On Windows:
.. code-block:: text
   c:\pyramidtut\tutorial> ..\Scripts\populate_tutorial development.ini
   c:\pyramidtut\tutorial> ..\Scripts\initialize_tutorial_db development.ini
The output to your console should be something like this::
docs/tutorials/wiki2/src/authorization/setup.py
@@ -40,6 +40,6 @@
      [paste.app_factory]
      main = tutorial:main
      [console_scripts]
      populate_tutorial = tutorial.scripts.populate:main
      initialize_tutorial_db = tutorial.scripts.initializedb:main
      """,
      )
docs/tutorials/wiki2/src/authorization/tutorial/scripts/initializedb.py
copy from docs/tutorials/wiki2/src/models/tutorial/scripts/populate.py copy to docs/tutorials/wiki2/src/authorization/tutorial/scripts/initializedb.py
docs/tutorials/wiki2/src/authorization/tutorial/scripts/populate.py
File was deleted
docs/tutorials/wiki2/src/basiclayout/setup.py
@@ -39,7 +39,7 @@
      [paste.app_factory]
      main = tutorial:main
      [console_scripts]
      populate_tutorial = tutorial.scripts.populate:main
      initialize_tutorial_db = tutorial.scripts.initializedb:main
      """,
      )
docs/tutorials/wiki2/src/basiclayout/tutorial/scripts/initializedb.py
docs/tutorials/wiki2/src/models/setup.py
@@ -39,7 +39,7 @@
      [paste.app_factory]
      main = tutorial:main
      [console_scripts]
      populate_tutorial = tutorial.scripts.populate:main
      initialize_tutorial_db = tutorial.scripts.initializedb:main
      """,
      )
docs/tutorials/wiki2/src/models/tutorial/scripts/initializedb.py
docs/tutorials/wiki2/src/tests/setup.py
@@ -41,6 +41,6 @@
      [paste.app_factory]
      main = tutorial:main
      [console_scripts]
      populate_tutorial = tutorial.scripts.populate:main
      initialize_tutorial_db = tutorial.scripts.initializedb:main
      """,
      )
docs/tutorials/wiki2/src/tests/tutorial/scripts/initializedb.py
copy from docs/tutorials/wiki2/src/models/tutorial/scripts/populate.py copy to docs/tutorials/wiki2/src/tests/tutorial/scripts/initializedb.py
docs/tutorials/wiki2/src/tests/tutorial/scripts/populate.py
File was deleted
docs/tutorials/wiki2/src/tests/tutorial/tests.py
@@ -246,23 +246,3 @@
        self.testapp.get(self.editor_login, status=302)
        res = self.testapp.get('/FrontPage', status=200)
        self.assertTrue('FrontPage' in res.body)
class Test_populate(unittest.TestCase):
    def setUp(self):
        from tutorial.models import DBSession
        DBSession.remove()
    def tearDown(self):
        from tutorial.models import DBSession
        DBSession.remove()
    def _callFUT(self, settings):
        from tutorial.scripts.populate import main
        main(['foo', 'development.ini'], settings)
    def test_it(self):
        self._callFUT({'sqlalchemy.url':'sqlite://'})
        from tutorial.models import DBSession, Page
        self.assertEqual(DBSession.query(Page).one().data,
            'This is the front page')
docs/tutorials/wiki2/src/views/setup.py
@@ -40,7 +40,7 @@
      [paste.app_factory]
      main = tutorial:main
      [console_scripts]
      populate_tutorial = tutorial.scripts.populate:main
      initialize_tutorial_db = tutorial.scripts.initializedb:main
      """,
      )
docs/tutorials/wiki2/src/views/tutorial/scripts/initializedb.py
docs/tutorials/wiki2/tests.rst
@@ -13,9 +13,8 @@
for the ``initialize_sql`` function.
To do so, we'll retain the ``tutorial.tests.ViewTests`` class provided as a
result of the ``alchemy`` scaffold.  We'll add two test classes: one for the
``Page`` model named ``PageModelTests``, and one for the ``populate`` script
named ``Test_populate``.
result of the ``alchemy`` scaffold.  We'll add a test class named
``PageModelTests`` for the ``Page`` model.
Testing the Views
=================
@@ -33,18 +32,6 @@
tested in the unit tests, like logging in, logging out, checking that
the ``viewer`` user cannot add or edit pages, but the ``editor`` user
can, and so on.
We must first modify ``main()`` in ``scripts/populate.py``, adding an
optional ``settings`` argument so we can pass in a URI to a
memory-resident database instead of our disk-based database we've
populated; this allows us to run our tests with a clean database each
time.  Replace ``main()`` with this version which adds an argument and
then uses it if set, and creates a ``Model`` for testing:
.. literalinclude:: src/tests/tutorial/scripts/populate.py
   :lines: 24-36
   :linenos:
   :language: python
Viewing the results of all our edits to ``tests.py``
====================================================