mirror of https://github.com/pallets/flask.git
65 lines
1.5 KiB
Python
65 lines
1.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
Flaskr
|
|
~~~~~~
|
|
|
|
A microblog example application written as Flask tutorial with
|
|
Flask and sqlite3.
|
|
|
|
:copyright: (c) 2015 by Armin Ronacher.
|
|
:license: BSD, see LICENSE for more details.
|
|
"""
|
|
|
|
import os
|
|
from flask import Flask, g
|
|
from werkzeug.utils import find_modules, import_string
|
|
from flaskr.blueprints.flaskr import init_db
|
|
|
|
|
|
def create_app(config=None):
|
|
app = Flask('flaskr')
|
|
|
|
app.config.update(dict(
|
|
DATABASE=os.path.join(app.root_path, 'flaskr.db'),
|
|
DEBUG=True,
|
|
SECRET_KEY='development key',
|
|
USERNAME='admin',
|
|
PASSWORD='default'
|
|
))
|
|
app.config.update(config or {})
|
|
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
|
|
|
|
register_blueprints(app)
|
|
register_cli(app)
|
|
register_teardowns(app)
|
|
|
|
return app
|
|
|
|
|
|
def register_blueprints(app):
|
|
"""Register all blueprint modules
|
|
|
|
Reference: Armin Ronacher, "Flask for Fun and for Profit" PyBay 2016.
|
|
"""
|
|
for name in find_modules('flaskr.blueprints'):
|
|
mod = import_string(name)
|
|
if hasattr(mod, 'bp'):
|
|
app.register_blueprint(mod.bp)
|
|
return None
|
|
|
|
|
|
def register_cli(app):
|
|
@app.cli.command('initdb')
|
|
def initdb_command():
|
|
"""Creates the database tables."""
|
|
init_db()
|
|
print('Initialized the database.')
|
|
|
|
|
|
def register_teardowns(app):
|
|
@app.teardown_appcontext
|
|
def close_db(error):
|
|
"""Closes the database again at the end of the request."""
|
|
if hasattr(g, 'sqlite_db'):
|
|
g.sqlite_db.close()
|