Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. # all the imports
  2. import os
  3. import sqlite3
  4. from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
  5.  
  6. app = Flask(__name__) # create the application instance :)
  7. app.config.from_object(__name__) # load config from this file , flaskr.py
  8.  
  9. # Load default config and override config from an environment variable
  10. app.config.update(dict(
  11. DATABASE=os.path.join(app.root_path, 'flaskr.db'),
  12. SECRET_KEY='development key',
  13. USERNAME='admin',
  14. PASSWORD='default'
  15. ))
  16. app.config.from_envvar('FLASKR_SETTINGS', silent=True)
  17.  
  18. def connect_db():
  19. """Connects to the specific database."""
  20. rv = sqlite3.connect(app.config['DATABASE'])
  21. rv.row_factory = sqlite3.Row
  22. return rv
  23.  
  24. def init_db():
  25. db = get_db()
  26. with app.open_resource('schema.sql', mode='r') as f:
  27. db.cursor().executescript(f.read())
  28. db.commit()
  29.  
  30. @app.cli.command('initdb')
  31. def initdb_command():
  32. """Initializes the database."""
  33. init_db()
  34. print('Initialized the database.')
  35.  
  36. def get_db():
  37. """Opens a new database connection if there is none yet for the
  38. current application context.
  39. """
  40. if not hasattr(g, 'sqlite_db'):
  41. g.sqlite_db = connect_db()
  42. return g.sqlite_db
  43.  
  44. @app.teardown_appcontext
  45. def close_db(error):
  46. """Closes the database again at the end of the request."""
  47. if hasattr(g, 'sqlite_db'):
  48. g.sqlite_db.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement