Skip to content

Commit 134190f

Browse files
committed
Take advantage of 'literalinclude' and 'lines' directive
This results in less duplication
1 parent 319fe83 commit 134190f

File tree

1 file changed

+18
-59
lines changed

1 file changed

+18
-59
lines changed

single_file_tasks/single_file_tasks.rst

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,9 @@ start the application, our subscribed function will be executed.
125125
Consequently, our database will be created or updated as necessary when the
126126
application is started.
127127

128-
.. code-block:: python
129-
130-
@subscriber(ApplicationCreated)
131-
def application_created_subscriber(event):
132-
log.warn('Initializing database...')
133-
f = open(os.path.join(here, 'schema.sql'), 'r')
134-
stmt = f.read()
135-
settings = event.app.registry.settings
136-
db = sqlite3.connect(settings['db'])
137-
db.executescript(stmt)
138-
db.commit()
139-
f.close()
128+
.. literalinclude:: src/tasks.py
129+
:lines: 72-80
130+
:language: python
140131

141132
We also need to make our database connection available to the application.
142133
We'll provide the connection object as an attribute of the application's
@@ -145,17 +136,9 @@ a connection to the database when a Pyramid request begins. It will be
145136
available as ``request.db``. We'll arrange to close it down by the end of
146137
the request lifecycle using the ``request.add_finished_callback`` method.
147138

148-
.. code-block:: python
149-
150-
@subscriber(NewRequest)
151-
def new_request_subscriber(event):
152-
request = event.request
153-
settings = request.registry.settings
154-
request.db = sqlite3.connect(settings['db'])
155-
request.add_finished_callback(close_db_connection)
156-
157-
def close_db_connection(request):
158-
request.db.close()
139+
.. literalinclude:: src/tasks.py
140+
:lines: 61-69
141+
:language: python
159142

160143
To make those changes active, we'll have to specify the database location in
161144
the configuration settings and make sure our ``@subscriber`` decorator is
@@ -203,13 +186,9 @@ convert them into a dictionary for easier accessibility within the template.
203186
The view function will pass a dictionary defining ``tasks`` to the
204187
``list.mako`` template.
205188

206-
.. code-block:: python
207-
208-
@view_config(route_name='list', renderer='list.mako')
209-
def list_view(request):
210-
rs = request.db.execute("select id, name from tasks where closed = 0")
211-
tasks = [dict(id=row[0], name=row[1]) for row in rs.fetchall()]
212-
return {'tasks': tasks}
189+
.. literalinclude:: src/tasks.py
190+
:lines: 23-27
191+
:language: python
213192

214193
When using the ``view_config`` decorator, it's important to specify a
215194
``route_name`` to match a defined route, and a ``renderer`` if the function is
@@ -226,20 +205,9 @@ message is flashed to be displayed on the next request, and the user's browser
226205
is redirected back to the *list_view*. If nothing is provided, a warning
227206
message is flashed and the *new_view* is displayed again.
228207

229-
.. code-block:: python
230-
231-
@view_config(route_name='new', renderer='new.mako')
232-
def new_view(request):
233-
if request.method == 'POST':
234-
if request.POST.get('name'):
235-
request.db.execute('insert into tasks (name, closed) values (?, ?)',
236-
[request.POST['name'], 0])
237-
request.db.commit()
238-
request.session.flash('New task was successfully added!')
239-
return HTTPFound(location=request.route_url('list'))
240-
else:
241-
request.session.flash('Please enter a name for the task!')
242-
return {}
208+
.. literalinclude:: src/tasks.py
209+
:lines: 30-42
210+
:language: python
243211

244212
.. warning::
245213

@@ -253,15 +221,9 @@ Close View
253221
This view lets the user mark a task as closed, flashes a success message, and
254222
redirects back to the *list_view* page.
255223

256-
.. code-block:: python
257-
258-
@view_config(route_name='close')
259-
def close_view(request):
260-
task_id = int(request.matchdict['id'])
261-
request.db.execute("update tasks set closed = ? where id = ?", (1, task_id))
262-
request.db.commit()
263-
request.session.flash('Task was successfully closed!')
264-
return HTTPFound(location=request.route_url('list'))
224+
.. literalinclude:: src/tasks.py
225+
:lines: 45-52
226+
:language: python
265227

266228
NotFound View
267229
+++++++++++++
@@ -271,12 +233,9 @@ by using our own template. The ``NotFound`` view is displayed by Pyramid when
271233
a URL cannot be mapped to a Pyramid view. We'll add the template in a
272234
subsequent step.
273235

274-
.. code-block:: python
275-
276-
@view_config(context='pyramid.exceptions.NotFound',
277-
renderer='notfound.mako')
278-
def notfound_view(self):
279-
return {}
236+
.. literalinclude:: src/tasks.py
237+
:lines: 55-57
238+
:language: python
280239

281240
Adding Routes
282241
+++++++++++++

0 commit comments

Comments
 (0)