~fluix/tilde

ref: 8385d93a45d225db407548099e08a11cb983d995 tilde/app.py -rw-r--r-- 1.4 KiB
8385d93aSteven Guikal Update privacy policy and require acceptance of it for registration 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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 flatpages.views import flatpages

    app.register_blueprint(flatpages)

    from 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)