From e7cb14b67f8b8b9a1e8ce312e4e6c4a282a312a0 Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Mon, 10 Apr 2023 15:43:42 +0000 Subject: [PATCH] Move Flask configs to new file --- gallery/__init__.py | 38 ++++++------------- gallery/api.py | 16 ++++---- gallery/config.py | 36 ++++++++++++++++++ .../sass/components/image-view/view.sass | 4 +- 4 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 gallery/config.py diff --git a/gallery/__init__.py b/gallery/__init__.py index 833570e..00a519f 100644 --- a/gallery/__init__.py +++ b/gallery/__init__.py @@ -19,46 +19,31 @@ from gallery import auth # Configuration import platformdirs -from dotenv import load_dotenv -from yaml import safe_load -USER_DIR = platformdirs.user_config_dir("onlylegs") +INSTACE_DIR = os.path.join(platformdirs.user_config_dir("onlylegs"), + "instance") def create_app(): # pylint: disable=R0914 """ Create and configure the main app """ - app = Flask(__name__, instance_path=os.path.join(USER_DIR, "instance")) - - # Get environment variables - load_dotenv(os.path.join(USER_DIR, ".env")) - print("Loaded environment variables") - - # Get config file - with open(os.path.join(USER_DIR, "conf.yml"), encoding="utf-8", mode="r") as file: - conf = safe_load(file) - print("Loaded config") - - # App configuration - app.config.from_mapping( - SECRET_KEY=os.environ.get("FLASK_SECRET"), - SQLALCHEMY_DATABASE_URI=("sqlite:///gallery.sqlite3"), - UPLOAD_FOLDER=os.path.join(USER_DIR, "uploads"), - ALLOWED_EXTENSIONS=conf["upload"]["allowed-extensions"], - MAX_CONTENT_LENGTH=1024 * 1024 * conf["upload"]["max-size"], - ADMIN_CONF=conf["admin"], - UPLOAD_CONF=conf["upload"], - WEBSITE_CONF=conf["website"], - ) + app = Flask(__name__, instance_path=INSTACE_DIR) + app.config.from_pyfile("config.py") db.init_app(app) migrate.init_app(app, db) + # 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) login_manager.login_view = "gallery.index" - login_manager.session_protection = "strong" + login_manager.session_protection = "normal" @login_manager.user_loader def load_user(user_id): @@ -105,5 +90,6 @@ def create_app(): # pylint: disable=R0914 cache.init_app(app) compress.init_app(app) + print("Done!") logging.info("Gallery started successfully!") return app diff --git a/gallery/api.py b/gallery/api.py index a3ac1fc..0c97e39 100644 --- a/gallery/api.py +++ b/gallery/api.py @@ -9,14 +9,12 @@ import platformdirs from flask import Blueprint, send_from_directory, abort, flash, request, current_app from werkzeug.utils import secure_filename - from flask_login import login_required, current_user from colorthief import ColorThief from gallery.extensions import db from gallery.models import Posts, Groups, GroupJunction - from gallery.utils import metadata as mt from gallery.utils.generate_image import generate_thumbnail @@ -131,7 +129,7 @@ def delete_image(image_id): post = Posts.query.filter_by(id=image_id).first() db.session.delete(post) - + groups = GroupJunction.query.filter_by(post_id=image_id).all() for group in groups: db.session.delete(group) @@ -180,10 +178,14 @@ def modify_group(): abort(403) if action == "add": - if not GroupJunction.query.filter_by(group_id=group_id, post_id=image_id).first(): + if not GroupJunction.query.filter_by( + group_id=group_id, post_id=image_id + ).first(): db.session.add(GroupJunction(group_id=group_id, post_id=image_id)) elif request.form["action"] == "remove": - db.session.delete(GroupJunction.query.filter_by(group_id=group_id, post_id=image_id).first()) + db.session.delete( + GroupJunction.query.filter_by(group_id=group_id, post_id=image_id).first() + ) db.session.commit() @@ -206,11 +208,11 @@ def delete_group(): group_del = Groups.query.filter_by(id=group_id).first() db.session.delete(group_del) - + junction_del = GroupJunction.query.filter_by(group_id=group_id).all() for junction in junction_del: db.session.delete(junction) - + db.session.commit() flash(["Group yeeted!", "1"]) diff --git a/gallery/config.py b/gallery/config.py new file mode 100644 index 0000000..57f42da --- /dev/null +++ b/gallery/config.py @@ -0,0 +1,36 @@ +""" +Gallery configuration file +""" +import os +import platformdirs +from dotenv import load_dotenv +from yaml import safe_load + + +# Set dirs +user_dir = platformdirs.user_config_dir("onlylegs") +instance_dir = os.path.join(user_dir, "instance") + +# Load environment variables +print("Loading environment variables...") +load_dotenv(os.path.join(user_dir, ".env")) + +# Load config from user dir +print("Loading config...") +with open(os.path.join(user_dir, "conf.yml"), encoding="utf-8", mode="r") as file: + conf = safe_load(file) + + +# Flask config +SECRET_KEY = os.environ.get("FLASK_SECRET") +SQLALCHEMY_DATABASE_URI = "sqlite:///gallery.sqlite3" + +# Upload config +MAX_CONTENT_LENGTH = 1024 * 1024 * conf["upload"]["max-size"] +UPLOAD_FOLDER = os.path.join(user_dir, "uploads") +ALLOWED_EXTENSIONS = conf["upload"]["allowed-extensions"] + +# Pass YAML config to app +ADMIN_CONF = conf["admin"] +UPLOAD_CONF = conf["upload"] +WEBSITE_CONF = conf["website"] diff --git a/gallery/static/sass/components/image-view/view.sass b/gallery/static/sass/components/image-view/view.sass index 7ddf6a9..297560a 100644 --- a/gallery/static/sass/components/image-view/view.sass +++ b/gallery/static/sass/components/image-view/view.sass @@ -20,7 +20,7 @@ margin: 0 0 0 27rem padding: 0 - width: calc(100% - 25rem) + width: calc(100% - 27rem) height: 100vh position: relative @@ -78,4 +78,4 @@ .info-tab.collapsed .info-header border-radius: $rad - \ No newline at end of file +