Move Flask configs to new file

This commit is contained in:
Michał 2023-04-10 15:43:42 +00:00
parent 8ae83a46c8
commit e7cb14b67f
4 changed files with 59 additions and 35 deletions

View file

@ -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

View file

@ -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
@ -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()

36
gallery/config.py Normal file
View file

@ -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"]

View file

@ -20,7 +20,7 @@
margin: 0 0 0 27rem
padding: 0
width: calc(100% - 25rem)
width: calc(100% - 27rem)
height: 100vh
position: relative