# SPDX-FileCopyrightText: 2021 Steven Guikal <void@fluix.one>
#
# SPDX-License-Identifier: AGPL-3.0-only
import logging
from flask import Flask, render_template
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
logger = logging.getLogger(__name__)
app = Flask(__name__, subdomain_matching=True)
app.config.from_pyfile("config.defaults")
for location in ["config", "/etc/tilde/config"]:
try:
app.config.from_pyfile(location)
except FileNotFoundError:
logger.warning(f"Could not find configuration file at '{location}'.")
else:
logger.info(f"Loaded configuration file at '{location}'.")
break
if not app.config["SECRET_KEY"]:
logger.fatal("SECRET_KEY must defined in some configuration file.")
exit(1)
app.config["db"] = SQLAlchemy(app)
with app.app_context():
from .blueprints.flatpages.views import flatpages
app.register_blueprint(flatpages)
from .blueprints.auth.views import auth
app.register_blueprint(auth, subdomain="auth")
@app.before_first_request
def create_tables():
app.config["db"].create_all()
login_manager = LoginManager()
login_manager.login_view = "auth.login"
login_manager.login_message = "Please log in to access that page."
login_manager.init_app(app)
@login_manager.user_loader
def load_user(id):
"""Check is_active property to log out users that are no longer active."""
from auth.models import User
return User.query.get(id)