From dc97176989ab06d260f1561e89681b607c1b9fc9 Mon Sep 17 00:00:00 2001 From: Steve Piercy Date: Sat, 24 Nov 2018 04:31:43 -0800 Subject: Clean up introduction to views, synch views files --- docs/tutorials/wiki/definingviews.rst | 49 ++++++++---------- .../wiki/src/views/tutorial/views/__init__.py | 0 .../wiki/src/views/tutorial/views/default.py | 60 ++++++++++++++++++++++ .../wiki/src/views/tutorial/views/notfound.py | 7 +++ 4 files changed, 87 insertions(+), 29 deletions(-) create mode 100644 docs/tutorials/wiki/src/views/tutorial/views/__init__.py create mode 100644 docs/tutorials/wiki/src/views/tutorial/views/default.py create mode 100644 docs/tutorials/wiki/src/views/tutorial/views/notfound.py (limited to 'docs/tutorials/wiki') 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 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 '%s' % (view_url, word) + else: + add_url = request.application_url + '/add_page/' + word + return '%s' % (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 {} -- cgit v1.2.3