49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
# app/__init__.py
|
|
|
|
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_migrate import Migrate
|
|
from flask_login import LoginManager
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
from sqlalchemy import event
|
|
from sqlalchemy.engine import Engine
|
|
import sqlite3
|
|
|
|
db = SQLAlchemy()
|
|
migrate = Migrate()
|
|
login_manager = LoginManager()
|
|
|
|
@event.listens_for(Engine, "connect")
|
|
def set_sqlite_pragma(dbapi_connection, connection_record):
|
|
# Only apply to SQLite connections
|
|
if isinstance(dbapi_connection, sqlite3.Connection):
|
|
cursor = dbapi_connection.cursor()
|
|
cursor.execute("PRAGMA foreign_keys = ON")
|
|
cursor.close()
|
|
|
|
def create_app(config_class='config.Config'):
|
|
app = Flask(__name__)
|
|
app.config.from_object(config_class)
|
|
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
|
"pool_pre_ping": True
|
|
}
|
|
|
|
db.init_app(app)
|
|
migrate.init_app(app, db)
|
|
login_manager.init_app(app)
|
|
login_manager.login_view = 'auth.login'
|
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_port=1)
|
|
|
|
from app.main import main
|
|
from app.auth import auth
|
|
|
|
app.register_blueprint(main)
|
|
app.register_blueprint(auth)
|
|
|
|
@app.template_filter('datetimeformat')
|
|
def datetimeformat(value, format="%Y-%m-%d"):
|
|
return value.strftime(format) if value else ""
|
|
|
|
return app
|