diff options
| author | Patricio Paez <pp@pp.com.mx> | 2012-11-20 19:46:10 -0600 |
|---|---|---|
| committer | Patricio Paez <pp@pp.com.mx> | 2012-11-20 19:46:10 -0600 |
| commit | 5fbaf7be1b82247e21fa7d3b98d76e7d92ac522c (patch) | |
| tree | 6f5e91967a48248443dc25f507aec1c2e5e546a9 /docs | |
| parent | 9a8a2133e49b42594e27bd1b8a08e8bf88a117e6 (diff) | |
| download | pyramid-5fbaf7be1b82247e21fa7d3b98d76e7d92ac522c.tar.gz pyramid-5fbaf7be1b82247e21fa7d3b98d76e7d92ac522c.tar.bz2 pyramid-5fbaf7be1b82247e21fa7d3b98d76e7d92ac522c.zip | |
Sync views.py on SQL wiki tutorial with the scaffold
- Explain the added lines in the Basic Layout chapter.
- The user is told to remove the added lines in the
Defining Views chapter, there was no need to fix
any emphasized lines.
- Sync the file in the other sections even if it is not
shown, for consistency.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/tutorials/wiki2/basiclayout.rst | 6 | ||||
| -rw-r--r-- | docs/tutorials/wiki2/src/basiclayout/tutorial/views.py | 25 | ||||
| -rw-r--r-- | docs/tutorials/wiki2/src/models/tutorial/views.py | 25 | ||||
| -rw-r--r-- | docs/tutorials/wiki2/src/tests/tutorial/views.py | 13 |
4 files changed, 59 insertions, 10 deletions
diff --git a/docs/tutorials/wiki2/basiclayout.rst b/docs/tutorials/wiki2/basiclayout.rst index dbd130c36..4f73dc914 100644 --- a/docs/tutorials/wiki2/basiclayout.rst +++ b/docs/tutorials/wiki2/basiclayout.rst @@ -171,6 +171,12 @@ application. Without being processed by ``scan``, the decorator effectively does nothing. ``@view_config`` is inert without being detected via a :term:`scan`. +The sample ``my_view()`` created by the scaffold uses a ``try:`` and ``except:`` +clause, to detect if there is a problem accessing the project database and +provide an alternate error response. That response will include the text +shown at the end of the file, which will be displayed in the browser to +inform the user about possible actions to take to solve the problem. + Content Models with ``models.py`` --------------------------------- diff --git a/docs/tutorials/wiki2/src/basiclayout/tutorial/views.py b/docs/tutorials/wiki2/src/basiclayout/tutorial/views.py index 3e6abf2c2..daf21bb7b 100644 --- a/docs/tutorials/wiki2/src/basiclayout/tutorial/views.py +++ b/docs/tutorials/wiki2/src/basiclayout/tutorial/views.py @@ -1,5 +1,8 @@ +from pyramid.response import Response from pyramid.view import view_config +from sqlalchemy.exc import DBAPIError + from .models import ( DBSession, MyModel, @@ -7,5 +10,25 @@ from .models import ( @view_config(route_name='home', renderer='templates/mytemplate.pt') def my_view(request): - one = DBSession.query(MyModel).filter(MyModel.name=='one').first() + try: + one = DBSession.query(MyModel).filter(MyModel.name=='one').first() + except DBAPIError: + return Response(conn_err_msg, content_type='text/plain', status_int=500) return {'one':one, 'project':'tutorial'} + +conn_err_msg = """\ +Pyramid is having a problem using your SQL database. The problem +might be caused by one of the following things: + +1. You may need to run the "initialize_tutorial_db" script + to initialize your database tables. Check your virtual + environment's "bin" directory for this script and try to run it. + +2. Your database server may not be running. Check that the + database server referred to by the "sqlalchemy.url" setting in + your "development.ini" file is running. + +After you fix the problem, please restart the Pyramid application to +try it again. +""" + diff --git a/docs/tutorials/wiki2/src/models/tutorial/views.py b/docs/tutorials/wiki2/src/models/tutorial/views.py index 3e6abf2c2..daf21bb7b 100644 --- a/docs/tutorials/wiki2/src/models/tutorial/views.py +++ b/docs/tutorials/wiki2/src/models/tutorial/views.py @@ -1,5 +1,8 @@ +from pyramid.response import Response from pyramid.view import view_config +from sqlalchemy.exc import DBAPIError + from .models import ( DBSession, MyModel, @@ -7,5 +10,25 @@ from .models import ( @view_config(route_name='home', renderer='templates/mytemplate.pt') def my_view(request): - one = DBSession.query(MyModel).filter(MyModel.name=='one').first() + try: + one = DBSession.query(MyModel).filter(MyModel.name=='one').first() + except DBAPIError: + return Response(conn_err_msg, content_type='text/plain', status_int=500) return {'one':one, 'project':'tutorial'} + +conn_err_msg = """\ +Pyramid is having a problem using your SQL database. The problem +might be caused by one of the following things: + +1. You may need to run the "initialize_tutorial_db" script + to initialize your database tables. Check your virtual + environment's "bin" directory for this script and try to run it. + +2. Your database server may not be running. Check that the + database server referred to by the "sqlalchemy.url" setting in + your "development.ini" file is running. + +After you fix the problem, please restart the Pyramid application to +try it again. +""" + diff --git a/docs/tutorials/wiki2/src/tests/tutorial/views.py b/docs/tutorials/wiki2/src/tests/tutorial/views.py index 42ac0eb7f..0d085b0e2 100644 --- a/docs/tutorials/wiki2/src/tests/tutorial/views.py +++ b/docs/tutorials/wiki2/src/tests/tutorial/views.py @@ -37,14 +37,13 @@ def view_wiki(request): permission='view') def view_page(request): pagename = request.matchdict['pagename'] - session = DBSession() - page = session.query(Page).filter_by(name=pagename).first() + page = DBSession.query(Page).filter_by(name=pagename).first() if page is None: return HTTPNotFound('No such page') def check(match): word = match.group(1) - exists = session.query(Page).filter_by(name=word).all() + exists = DBSession.query(Page).filter_by(name=word).all() if exists: view_url = request.route_url('view_page', pagename=word) return '<a href="%s">%s</a>' % (view_url, word) @@ -63,10 +62,9 @@ def view_page(request): def add_page(request): pagename = request.matchdict['pagename'] if 'form.submitted' in request.params: - session = DBSession() body = request.params['body'] page = Page(pagename, body) - session.add(page) + DBSession.add(page) return HTTPFound(location = request.route_url('view_page', pagename=pagename)) save_url = request.route_url('add_page', pagename=pagename) @@ -78,11 +76,10 @@ def add_page(request): permission='edit') def edit_page(request): pagename = request.matchdict['pagename'] - session = DBSession() - page = session.query(Page).filter_by(name=pagename).one() + page = DBSession.query(Page).filter_by(name=pagename).one() if 'form.submitted' in request.params: page.data = request.params['body'] - session.add(page) + DBSession.add(page) return HTTPFound(location = request.route_url('view_page', pagename=pagename)) return dict( |
