summaryrefslogtreecommitdiff
path: root/docs/tutorials
diff options
context:
space:
mode:
Diffstat (limited to 'docs/tutorials')
-rw-r--r--docs/tutorials/wiki/authorization.rst37
-rw-r--r--docs/tutorials/wiki/src/authorization/tutorial/login.py43
-rw-r--r--docs/tutorials/wiki/src/authorization/tutorial/views.py45
-rw-r--r--docs/tutorials/wiki/src/tests/tutorial/login.py43
-rw-r--r--docs/tutorials/wiki/src/tests/tutorial/views.py45
-rw-r--r--docs/tutorials/wiki/tests.rst7
6 files changed, 114 insertions, 106 deletions
diff --git a/docs/tutorials/wiki/authorization.rst b/docs/tutorials/wiki/authorization.rst
index d900f17a3..fa18d4a41 100644
--- a/docs/tutorials/wiki/authorization.rst
+++ b/docs/tutorials/wiki/authorization.rst
@@ -128,18 +128,24 @@ We'll also add a ``logout`` view to our application and provide a link
to it. This view will clear the credentials of the logged in user and
redirect back to the front page.
-We'll add a different file (for presentation convenience) to add login
-and logout views. Add a file named ``login.py`` to your application
-(in the same directory as ``views.py``) with the following content:
+We'll add these views to the existing ``views.py`` file we have in our
+project. Here's what the ``login`` view callable will look like:
-.. literalinclude:: src/authorization/tutorial/login.py
+.. literalinclude:: src/authorization/tutorial/views.py
+ :pyobject: login
:linenos:
:language: python
-Note that the ``login`` view callable in the ``login.py`` file has *two* view
-configuration decorators. The order of these decorators is unimportant.
-Each just adds a different :term:`view configuration` for the ``login`` view
-callable.
+Here's what the ``logout`` view callable will look like:
+
+.. literalinclude:: src/authorization/tutorial/views.py
+ :pyobject: logout
+ :linenos:
+ :language: python
+
+Note that the ``login`` view callable has *two* view configuration
+decorators. The order of these decorators is unimportant. Each just adds a
+different :term:`view configuration` for the ``login`` view callable.
The first view configuration decorator configures the ``login`` view callable
so it will be invoked when someone visits ``/login`` (when the context is a
@@ -156,14 +162,18 @@ login form. Before being allowed to continue on to the add or edit form, he
will have to provide credentials that give him permission to add or edit via
this login form.
+Note that we're relying on some additional imports within the bodies of these
+views (e.g. ``remember`` and ``forget``). We'll see a rendering of the
+entire views.py file a little later here to show you where those come from.
+
Change Existing Views
~~~~~~~~~~~~~~~~~~~~~
-Then we need to change each of our ``view_page``, ``edit_page`` and
-``add_page`` views in ``views.py`` to pass a "logged in" parameter
-into its template. We'll add something like this to each view body:
+In order to indicate whether the current user is logged in, we need to change
+each of our ``view_page``, ``edit_page`` and ``add_page`` views in
+``views.py`` to pass a "logged in" parameter into its template. We'll add
+something like this to each view body:
-.. ignore-next-block
.. code-block:: python
:linenos:
@@ -174,7 +184,6 @@ We'll then change the return value of each view that has an associated
``renderer`` to pass the resulting ``logged_in`` value to the
template. For example:
-.. ignore-next-block
.. code-block:: python
:linenos:
@@ -219,7 +228,7 @@ Add the ``login.pt`` Template
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Add a ``login.pt`` template to your templates directory. It's
-referred to within the login view we just added to ``login.py``.
+referred to within the login view we just added to ``views.py``.
.. literalinclude:: src/authorization/tutorial/templates/login.pt
:language: xml
diff --git a/docs/tutorials/wiki/src/authorization/tutorial/login.py b/docs/tutorials/wiki/src/authorization/tutorial/login.py
deleted file mode 100644
index 11dea050f..000000000
--- a/docs/tutorials/wiki/src/authorization/tutorial/login.py
+++ /dev/null
@@ -1,43 +0,0 @@
-from pyramid.httpexceptions import HTTPFound
-
-from pyramid.security import remember
-from pyramid.security import forget
-from pyramid.view import view_config
-
-from .security import USERS
-
-@view_config(context='.models.Wiki', name='login',
- renderer='templates/login.pt')
-@view_config(context='pyramid.httpexceptions.HTTPForbidden',
- renderer='templates/login.pt')
-def login(request):
- login_url = request.resource_url(request.context, 'login')
- referrer = request.url
- if referrer == login_url:
- referrer = '/' # never use the login form itself as came_from
- came_from = request.params.get('came_from', referrer)
- message = ''
- login = ''
- password = ''
- if 'form.submitted' in request.params:
- login = request.params['login']
- password = request.params['password']
- if USERS.get(login) == password:
- headers = remember(request, login)
- return HTTPFound(location = came_from,
- headers = headers)
- message = 'Failed login'
-
- return dict(
- message = message,
- url = request.application_url + '/login',
- came_from = came_from,
- login = login,
- password = password,
- )
-
-@view_config(context='.models.Wiki', name='logout')
-def logout(request):
- headers = forget(request)
- return HTTPFound(location = request.resource_url(request.context),
- headers = headers)
diff --git a/docs/tutorials/wiki/src/authorization/tutorial/views.py b/docs/tutorials/wiki/src/authorization/tutorial/views.py
index 7ac5eeab6..2f0502c17 100644
--- a/docs/tutorials/wiki/src/authorization/tutorial/views.py
+++ b/docs/tutorials/wiki/src/authorization/tutorial/views.py
@@ -2,9 +2,16 @@ from docutils.core import publish_parts
import re
from pyramid.httpexceptions import HTTPFound
+
from pyramid.view import view_config
-from pyramid.security import authenticated_userid
+from pyramid.security import (
+ authenticated_userid,
+ remember,
+ forget,
+ )
+
+from .security import USERS
from .models import Page
# regular expression used to find WikiWords
@@ -72,3 +79,39 @@ def edit_page(context, request):
return dict(page = context,
save_url = request.resource_url(context, 'edit_page'),
logged_in = logged_in)
+
+@view_config(context='.models.Wiki', name='login',
+ renderer='templates/login.pt')
+@view_config(context='pyramid.httpexceptions.HTTPForbidden',
+ renderer='templates/login.pt')
+def login(request):
+ login_url = request.resource_url(request.context, 'login')
+ referrer = request.url
+ if referrer == login_url:
+ referrer = '/' # never use the login form itself as came_from
+ came_from = request.params.get('came_from', referrer)
+ message = ''
+ login = ''
+ password = ''
+ if 'form.submitted' in request.params:
+ login = request.params['login']
+ password = request.params['password']
+ if USERS.get(login) == password:
+ headers = remember(request, login)
+ return HTTPFound(location = came_from,
+ headers = headers)
+ message = 'Failed login'
+
+ return dict(
+ message = message,
+ url = request.application_url + '/login',
+ came_from = came_from,
+ login = login,
+ password = password,
+ )
+
+@view_config(context='.models.Wiki', name='logout')
+def logout(request):
+ headers = forget(request)
+ return HTTPFound(location = request.resource_url(request.context),
+ headers = headers)
diff --git a/docs/tutorials/wiki/src/tests/tutorial/login.py b/docs/tutorials/wiki/src/tests/tutorial/login.py
deleted file mode 100644
index 11dea050f..000000000
--- a/docs/tutorials/wiki/src/tests/tutorial/login.py
+++ /dev/null
@@ -1,43 +0,0 @@
-from pyramid.httpexceptions import HTTPFound
-
-from pyramid.security import remember
-from pyramid.security import forget
-from pyramid.view import view_config
-
-from .security import USERS
-
-@view_config(context='.models.Wiki', name='login',
- renderer='templates/login.pt')
-@view_config(context='pyramid.httpexceptions.HTTPForbidden',
- renderer='templates/login.pt')
-def login(request):
- login_url = request.resource_url(request.context, 'login')
- referrer = request.url
- if referrer == login_url:
- referrer = '/' # never use the login form itself as came_from
- came_from = request.params.get('came_from', referrer)
- message = ''
- login = ''
- password = ''
- if 'form.submitted' in request.params:
- login = request.params['login']
- password = request.params['password']
- if USERS.get(login) == password:
- headers = remember(request, login)
- return HTTPFound(location = came_from,
- headers = headers)
- message = 'Failed login'
-
- return dict(
- message = message,
- url = request.application_url + '/login',
- came_from = came_from,
- login = login,
- password = password,
- )
-
-@view_config(context='.models.Wiki', name='logout')
-def logout(request):
- headers = forget(request)
- return HTTPFound(location = request.resource_url(request.context),
- headers = headers)
diff --git a/docs/tutorials/wiki/src/tests/tutorial/views.py b/docs/tutorials/wiki/src/tests/tutorial/views.py
index 7ac5eeab6..2f0502c17 100644
--- a/docs/tutorials/wiki/src/tests/tutorial/views.py
+++ b/docs/tutorials/wiki/src/tests/tutorial/views.py
@@ -2,9 +2,16 @@ from docutils.core import publish_parts
import re
from pyramid.httpexceptions import HTTPFound
+
from pyramid.view import view_config
-from pyramid.security import authenticated_userid
+from pyramid.security import (
+ authenticated_userid,
+ remember,
+ forget,
+ )
+
+from .security import USERS
from .models import Page
# regular expression used to find WikiWords
@@ -72,3 +79,39 @@ def edit_page(context, request):
return dict(page = context,
save_url = request.resource_url(context, 'edit_page'),
logged_in = logged_in)
+
+@view_config(context='.models.Wiki', name='login',
+ renderer='templates/login.pt')
+@view_config(context='pyramid.httpexceptions.HTTPForbidden',
+ renderer='templates/login.pt')
+def login(request):
+ login_url = request.resource_url(request.context, 'login')
+ referrer = request.url
+ if referrer == login_url:
+ referrer = '/' # never use the login form itself as came_from
+ came_from = request.params.get('came_from', referrer)
+ message = ''
+ login = ''
+ password = ''
+ if 'form.submitted' in request.params:
+ login = request.params['login']
+ password = request.params['password']
+ if USERS.get(login) == password:
+ headers = remember(request, login)
+ return HTTPFound(location = came_from,
+ headers = headers)
+ message = 'Failed login'
+
+ return dict(
+ message = message,
+ url = request.application_url + '/login',
+ came_from = came_from,
+ login = login,
+ password = password,
+ )
+
+@view_config(context='.models.Wiki', name='logout')
+def logout(request):
+ headers = forget(request)
+ return HTTPFound(location = request.resource_url(request.context),
+ headers = headers)
diff --git a/docs/tutorials/wiki/tests.rst b/docs/tutorials/wiki/tests.rst
index 941302d67..1ddb8f408 100644
--- a/docs/tutorials/wiki/tests.rst
+++ b/docs/tutorials/wiki/tests.rst
@@ -9,10 +9,9 @@ that it continues to work after some changes are made in the future.
Test the Models
===============
-We write tests for the model
-classes and the appmaker. Changing ``tests.py``, we'll write a separate test
-class for each model class, and we'll write a test class for the
-``appmaker``.
+We write tests for the model classes and the appmaker. Changing
+``tests.py``, we'll write a separate test class for each model class, and
+we'll write a test class for the ``appmaker``.
To do so, we'll retain the ``tutorial.tests.ViewTests`` class provided as a
result of the ``zodb`` project generator. We'll add three test