59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
import os
|
|
from dotenv import load_dotenv
|
|
from flask import Flask
|
|
from the_works.database import init_db
|
|
from flask_debugtoolbar import DebugToolbarExtension
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
|
|
# read config values
|
|
load_dotenv()
|
|
app.config.from_prefixed_env()
|
|
if os.getenv("SQLALCHEMY_DATABASE_DIALECT") == "sqlite":
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///" + os.path.abspath(app.root_path + "/..") + "/" + os.getenv("SQLALCHEMY_DATABASE_SQLITE_FILENAME")
|
|
else:
|
|
exit("no SQLite database URI given; exiting")
|
|
|
|
# initialize database
|
|
init_db(app)
|
|
|
|
# register blueprints
|
|
from the_works.views import home, text, werk, verlag, sprache, textform, werksform, genre, pseudonym, reihe, herausgeber, veroeffentlichung, titelbild
|
|
app.register_blueprint(home.bp)
|
|
app.register_blueprint(text.bp)
|
|
app.register_blueprint(werk.bp)
|
|
app.register_blueprint(verlag.bp)
|
|
app.register_blueprint(sprache.bp)
|
|
app.register_blueprint(textform.bp)
|
|
app.register_blueprint(werksform.bp)
|
|
app.register_blueprint(genre.bp)
|
|
app.register_blueprint(pseudonym.bp)
|
|
app.register_blueprint(reihe.bp)
|
|
app.register_blueprint(herausgeber.bp)
|
|
app.register_blueprint(veroeffentlichung.bp)
|
|
app.register_blueprint(titelbild.bp)
|
|
|
|
### DEBUG
|
|
toolbar = DebugToolbarExtension(app)
|
|
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
|
|
app.config["SQLALCHEMY_ECHO"] = True
|
|
app.config['SQLALCHEMY_RECORD_QUERIES'] = os.getenv("SQLALCHEMY_RECORD_QUERIES")
|
|
print(f"Current Environment: " + app.config['ENVIRONMENT'])
|
|
|
|
# register helper function
|
|
app.jinja_env.globals.update(sizeof_fmt=sizeof_fmt)
|
|
|
|
return app
|
|
|
|
|
|
# helper function to print formatted file size from https://stackoverflow.com/a/1094933
|
|
def sizeof_fmt(num, suffix="B"):
|
|
if type(num) == "String":
|
|
num = int(num)
|
|
for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"):
|
|
if abs(num) < 1024.0:
|
|
return f"{num:3.1f} {unit}{suffix}"
|
|
num /= 1024.0
|
|
return f"{num:.1f} Yi{suffix}"
|