@@ -125,18 +125,9 @@ start the application, our subscribed function will be executed.
125
125
Consequently, our database will be created or updated as necessary when the
126
126
application is started.
127
127
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
140
131
141
132
We also need to make our database connection available to the application.
142
133
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
145
136
available as ``request.db ``. We'll arrange to close it down by the end of
146
137
the request lifecycle using the ``request.add_finished_callback `` method.
147
138
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
159
142
160
143
To make those changes active, we'll have to specify the database location in
161
144
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.
203
186
The view function will pass a dictionary defining ``tasks `` to the
204
187
``list.mako `` template.
205
188
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
213
192
214
193
When using the ``view_config `` decorator, it's important to specify a
215
194
``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
226
205
is redirected back to the *list_view *. If nothing is provided, a warning
227
206
message is flashed and the *new_view * is displayed again.
228
207
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
243
211
244
212
.. warning ::
245
213
@@ -253,15 +221,9 @@ Close View
253
221
This view lets the user mark a task as closed, flashes a success message, and
254
222
redirects back to the *list_view * page.
255
223
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
265
227
266
228
NotFound View
267
229
+++++++++++++
@@ -271,12 +233,9 @@ by using our own template. The ``NotFound`` view is displayed by Pyramid when
271
233
a URL cannot be mapped to a Pyramid view. We'll add the template in a
272
234
subsequent step.
273
235
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
280
239
281
240
Adding Routes
282
241
+++++++++++++
0 commit comments