python-gallery/gallery/__init__.py

109 lines
3.4 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
"""
2023-03-04 13:45:26 +00:00
# Import system modules
import os
import logging
# Flask
from flask_compress import Compress
from flask_caching import Cache
from flask_assets import Environment, Bundle
from flask_login import LoginManager
from flask import Flask, render_template, abort
from werkzeug.exceptions import HTTPException
2023-03-04 13:45:26 +00:00
# Configuration
import platformdirs
2023-03-20 17:19:42 +00:00
from dotenv import load_dotenv
from yaml import safe_load
2023-03-20 17:19:42 +00:00
# Import database
from sqlalchemy.orm import sessionmaker
from gallery import db
USER_DIR = platformdirs.user_config_dir('onlylegs')
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
login_manager = LoginManager()
assets = Environment()
cache = Cache(config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 300})
compress = Compress()
def create_app(test_config=None):
2023-03-04 13:45:26 +00:00
"""
Create and configure the main app
"""
app = Flask(__name__, instance_path=os.path.join(USER_DIR, 'instance'))
2023-03-20 17:19:42 +00:00
# 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:
2023-03-20 17:57:47 +00:00
conf = safe_load(file)
print("Loaded config")
# App configuration
app.config.from_mapping(
SECRET_KEY=os.environ.get('FLASK_SECRET'),
DATABASE=os.path.join(app.instance_path, 'gallery.sqlite'),
2023-03-04 13:45:26 +00:00
UPLOAD_FOLDER=os.path.join(USER_DIR, 'uploads'),
ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'],
MAX_CONTENT_LENGTH=1024 * 1024 * conf['upload']['max-size'],
WEBSITE=conf['website'],
)
if test_config is None:
app.config.from_pyfile('config.py', silent=True)
else:
app.config.from_mapping(test_config)
login_manager.init_app(app)
login_manager.login_view = 'gallery.index'
@login_manager.user_loader
def load_user(user_id):
return db_session.query(db.Users).filter_by(id=user_id).first()
# Load JS assets
# TODO: disable caching for sass files as it makes it hard to work on when it is enabled
assets.register('js_pre', Bundle('js/pre/*.js', output='gen/pre_packed.js'))
assets.register('js_post', Bundle('js/post/*.js', output='gen/post_packed.js'))
assets.register('styles', Bundle('sass/*.sass', filters='libsass', output='gen/styles.css'))
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-02 16:50:52 +00:00
return render_template('error.html',
error=err.code,
msg=err.description), err.code
# Load login, registration and logout manager
2023-03-12 15:52:23 +00:00
from gallery import auth
app.register_blueprint(auth.blueprint)
2023-03-12 15:52:23 +00:00
# Load the different routes
2023-04-03 01:17:11 +00:00
from gallery.views import api, groups, routing, settings
2023-03-12 15:52:23 +00:00
app.register_blueprint(api.blueprint)
app.register_blueprint(groups.blueprint)
2023-03-12 15:52:23 +00:00
app.register_blueprint(routing.blueprint)
app.register_blueprint(settings.blueprint)
# Log to file that the app has started
logging.info('Gallery started successfully!')
# Initialize extensions and return app
assets.init_app(app)
cache.init_app(app)
compress.init_app(app)
2023-03-04 13:45:26 +00:00
return app