summaryrefslogtreecommitdiff
path: root/docs/tutorials/wiki
diff options
context:
space:
mode:
authorSteve Piercy <web@stevepiercy.com>2018-11-24 04:31:43 -0800
committerSteve Piercy <web@stevepiercy.com>2018-11-24 04:31:43 -0800
commitdc97176989ab06d260f1561e89681b607c1b9fc9 (patch)
tree322e199e257622b314bfdd6235ae9190b3f2d573 /docs/tutorials/wiki
parent5a2a68d508c3c433dcb6a8ed5824bd99a0b6d8d4 (diff)
downloadpyramid-dc97176989ab06d260f1561e89681b607c1b9fc9.tar.gz
pyramid-dc97176989ab06d260f1561e89681b607c1b9fc9.tar.bz2
pyramid-dc97176989ab06d260f1561e89681b607c1b9fc9.zip
Clean up introduction to views, synch views files
Diffstat (limited to 'docs/tutorials/wiki')
-rw-r--r--docs/tutorials/wiki/definingviews.rst49
-rw-r--r--docs/tutorials/wiki/src/views/tutorial/views/__init__.py0
-rw-r--r--docs/tutorials/wiki/src/views/tutorial/views/default.py60
-rw-r--r--docs/tutorials/wiki/src/views/tutorial/views/notfound.py7
4 files changed, 87 insertions, 29 deletions
diff --git a/docs/tutorials/wiki/definingviews.rst b/docs/tutorials/wiki/definingviews.rst
index 9bafa87c7..7773fbe0f 100644
--- a/docs/tutorials/wiki/definingviews.rst
+++ b/docs/tutorials/wiki/definingviews.rst
@@ -25,14 +25,11 @@ We will define several :term:`view callable` functions, then wire them into :app
Declaring Dependencies in Our ``setup.py`` File
===============================================
-The view code in our application will depend on a package which is not a
-dependency of the original "tutorial" application. The original "tutorial"
-application was generated by the cookiecutter; it doesn't know
-about our custom application requirements.
+The view code in our application will depend on a package which is not a dependency of the original "tutorial" application.
+The original "tutorial" application was generated by the cookiecutter.
+It does not know about our custom application requirements.
-We need to add a dependency on the ``docutils`` package to our ``tutorial``
-package's ``setup.py`` file by assigning this dependency to the ``requires``
-parameter in the ``setup()`` function.
+We need to add a dependency on the ``docutils`` package to our ``tutorial`` package's ``setup.py`` file by assigning this dependency to the ``requires`` parameter in the ``setup()`` function.
Open ``setup.py`` and edit it to look like the following:
@@ -43,17 +40,15 @@ Open ``setup.py`` and edit it to look like the following:
Only the highlighted line needs to be added.
+
.. _wiki-running-pip-install:
Running ``pip install -e .``
============================
-Since a new software dependency was added, you will need to run ``pip install
--e .`` again inside the root of the ``tutorial`` package to obtain and register
-the newly added dependency distribution.
+Since a new software dependency was added, you need to run ``pip install -e .`` again inside the root of the ``tutorial`` package to obtain and register the newly added dependency distribution.
-Make sure your current working directory is the root of the project (the
-directory in which ``setup.py`` lives) and execute the following command.
+Make sure your current working directory is the root of the project (the directory in which ``setup.py`` lives) and execute the following command.
On Unix:
@@ -69,47 +64,43 @@ On Windows:
cd tutorial
%VENV%\Scripts\pip install -e .
-Success executing this command will end with a line to the console something
-like:
+Success executing this command will end with a line to the console similar to the following:
.. code-block:: text
- Successfully installed docutils-0.13.1 tutorial
+ Successfully installed docutils-0.14 tutorial
Adding view functions in ``views.py``
=====================================
-It's time for a major change. Open ``tutorial/views.py`` and edit it to look
-like the following:
+It is time for a major change.
+Open ``tutorial/views/default.py`` and edit it to look like the following:
-.. literalinclude:: src/views/tutorial/views.py
+.. literalinclude:: src/views/tutorial/views/default.py
:linenos:
:language: python
We added some imports and created a regular expression to find "WikiWords".
-We got rid of the ``my_view`` view function and its decorator that was added
-when originally rendered after we selected the ``zodb`` backend option in the
-cookiecutter. It was only an example and isn't relevant to our application.
+We got rid of the ``my_view`` view function and its decorator that was added when originally rendered after we selected the ``zodb`` backend option in the cookiecutter.
+It was only an example and is not relevant to our application.
-Then we added four :term:`view callable` functions to our ``views.py``
-module:
+Then we added four :term:`view callable` functions to our ``views.py`` module:
* ``view_wiki()`` - Displays the wiki itself. It will answer on the root URL.
* ``view_page()`` - Displays an individual page.
* ``add_page()`` - Allows the user to add a page.
* ``edit_page()`` - Allows the user to edit a page.
-We'll describe each one briefly in the following sections.
+We will describe each one briefly in the following sections.
.. note::
- There is nothing special about the filename ``views.py``. A project may
- have many view callables throughout its codebase in arbitrarily named
- files. Files implementing view callables often have ``view`` in their
- filenames (or may live in a Python subpackage of your application package
- named ``views``), but this is only by convention.
+ There is nothing special about the filename ``views.py``.
+ A project may have many view callables throughout its codebase in arbitrarily named files.
+ Files that implement view callables often have ``view`` in their names (or may live in a Python subpackage of your application package named ``views``), but this is only by convention.
+
The ``view_wiki`` view function
-------------------------------
diff --git a/docs/tutorials/wiki/src/views/tutorial/views/__init__.py b/docs/tutorials/wiki/src/views/tutorial/views/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/docs/tutorials/wiki/src/views/tutorial/views/__init__.py
diff --git a/docs/tutorials/wiki/src/views/tutorial/views/default.py b/docs/tutorials/wiki/src/views/tutorial/views/default.py
new file mode 100644
index 000000000..dfc42c96a
--- /dev/null
+++ b/docs/tutorials/wiki/src/views/tutorial/views/default.py
@@ -0,0 +1,60 @@
+from docutils.core import publish_parts
+import re
+
+from pyramid.httpexceptions import HTTPFound
+from pyramid.view import view_config
+
+from .models import Page
+
+# regular expression used to find WikiWords
+wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)")
+
+@view_config(context='.models.Wiki')
+def view_wiki(context, request):
+ return HTTPFound(location=request.resource_url(context, 'FrontPage'))
+
+@view_config(context='.models.Page', renderer='templates/view.pt')
+def view_page(context, request):
+ wiki = context.__parent__
+
+ def check(match):
+ word = match.group(1)
+ if word in wiki:
+ page = wiki[word]
+ view_url = request.resource_url(page)
+ return '<a href="%s">%s</a>' % (view_url, word)
+ else:
+ add_url = request.application_url + '/add_page/' + word
+ return '<a href="%s">%s</a>' % (add_url, word)
+
+ content = publish_parts(context.data, writer_name='html')['html_body']
+ content = wikiwords.sub(check, content)
+ edit_url = request.resource_url(context, 'edit_page')
+ return dict(page=context, content=content, edit_url=edit_url)
+
+@view_config(name='add_page', context='.models.Wiki',
+ renderer='templates/edit.pt')
+def add_page(context, request):
+ pagename = request.subpath[0]
+ if 'form.submitted' in request.params:
+ body = request.params['body']
+ page = Page(body)
+ page.__name__ = pagename
+ page.__parent__ = context
+ context[pagename] = page
+ return HTTPFound(location=request.resource_url(page))
+ save_url = request.resource_url(context, 'add_page', pagename)
+ page = Page('')
+ page.__name__ = pagename
+ page.__parent__ = context
+ return dict(page=page, save_url=save_url)
+
+@view_config(name='edit_page', context='.models.Page',
+ renderer='templates/edit.pt')
+def edit_page(context, request):
+ if 'form.submitted' in request.params:
+ context.data = request.params['body']
+ return HTTPFound(location=request.resource_url(context))
+
+ return dict(page=context,
+ save_url=request.resource_url(context, 'edit_page'))
diff --git a/docs/tutorials/wiki/src/views/tutorial/views/notfound.py b/docs/tutorials/wiki/src/views/tutorial/views/notfound.py
new file mode 100644
index 000000000..728791d0a
--- /dev/null
+++ b/docs/tutorials/wiki/src/views/tutorial/views/notfound.py
@@ -0,0 +1,7 @@
+from pyramid.view import notfound_view_config
+
+
+@notfound_view_config(renderer='../templates/404.pt')
+def notfound_view(request):
+ request.response.status = 404
+ return {}