python-gallery/gallery/__init__.py

96 lines
2.7 KiB
Python
Raw Normal View History

2023-03-04 13:45:26 +00:00
"""
2023-03-11 23:16:27 +00:00
Onlylegs Gallery
This is the main app file, it loads all the other files and sets up the app
2023-03-04 13:45:26 +00:00
"""
# Import system modules
import os
import logging
# Flask
from flask_assets import Bundle
from flask import Flask, render_template, abort
from werkzeug.exceptions import HTTPException
from gallery.extensions import db, migrate, login_manager, assets, compress, cache
from gallery.models import Users
from gallery.views import index, image, group, settings, profile
from gallery import api
from gallery import auth
2023-03-04 13:45:26 +00:00
# Configuration
import platformdirs
2023-03-20 17:19:42 +00:00
2023-04-10 15:43:42 +00:00
INSTACE_DIR = os.path.join(platformdirs.user_config_dir("onlylegs"),
"instance")
def create_app(): # pylint: disable=R0914
2023-03-04 13:45:26 +00:00
"""
Create and configure the main app
"""
2023-04-10 15:43:42 +00:00
app = Flask(__name__, instance_path=INSTACE_DIR)
app.config.from_pyfile("config.py")
db.init_app(app)
migrate.init_app(app, db)
2023-04-10 15:43:42 +00:00
# if database file doesn't exist, create it
if not os.path.exists(os.path.join(INSTACE_DIR, "gallery.sqlite3")):
print("Creating database")
with app.app_context():
db.create_all()
login_manager.init_app(app)
2023-04-07 12:35:30 +00:00
login_manager.login_view = "gallery.index"
2023-04-10 15:43:42 +00:00
login_manager.session_protection = "normal"
@login_manager.user_loader
def load_user(user_id):
return Users.query.filter_by(alt_id=user_id).first()
@login_manager.unauthorized_handler
def unauthorized():
2023-04-04 14:00:00 +00:00
error = 401
2023-04-07 12:35:30 +00:00
msg = "You are not authorized to view this page!!!!"
return render_template("error.html", error=error, msg=msg), error
2023-04-04 14:00:00 +00:00
2023-04-02 16:50:52 +00:00
# Error handlers, if the error is not a HTTP error, return 500
@app.errorhandler(Exception)
2023-04-02 16:50:52 +00:00
def error_page(err): # noqa
if not isinstance(err, HTTPException):
abort(500)
2023-04-07 12:35:30 +00:00
return (
render_template("error.html", error=err.code, msg=err.description),
err.code,
)
scripts = Bundle("js/*.js", filters="jsmin", output="gen/js.js", depends="js/*.js")
2023-04-07 12:35:30 +00:00
styles = Bundle(
"sass/*.sass",
filters="libsass, cssmin",
output="gen/styles.css",
depends="sass/**/*.sass",
)
assets.register("scripts", scripts)
assets.register("styles", styles)
2023-04-07 12:35:30 +00:00
# Load all the blueprints
app.register_blueprint(auth.blueprint)
2023-03-12 15:52:23 +00:00
app.register_blueprint(api.blueprint)
2023-04-06 16:22:56 +00:00
app.register_blueprint(index.blueprint)
app.register_blueprint(image.blueprint)
app.register_blueprint(group.blueprint)
app.register_blueprint(profile.blueprint)
app.register_blueprint(settings.blueprint)
assets.init_app(app)
cache.init_app(app)
compress.init_app(app)
2023-04-10 15:43:42 +00:00
print("Done!")
logging.info("Gallery started successfully!")
2023-03-04 13:45:26 +00:00
return app