Commits

Fran Peruzzi  committed 9a8756f Draft

added pre and post request code

  • Participants
  • Parent commits 58c1dd8

Comments (0)

Files changed (1)

 import sqlite3
-from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
-from contextlib import closing
+from flask import Flask, g
 
 # configuration
 DATABASE = './nicelist.db'
 app = Flask(__name__)
 app.config.from_object(__name__)
 
+
 def connect_db():
     return sqlite3.connect(app.config['DATABASE'])
 
+
 def init_db():
+    from contextlib import closing
     with closing(connect_db()) as db:
         with app.open_resource('schema.sql') as f:
             db.cursor().executescript(f.read())
         db.commit()
 
+
+@app.before_request
+def before_request():
+    g.db = connect_db()
+
+
+@app.teardown_request
+def teardown_request(exception):
+    g.db.close()
+
+
 if __name__ == '__main__':
     app.run()