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-01-10 12:39:29 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
# Import system modules
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# Flask
|
2023-01-25 15:13:56 +00:00
|
|
|
from flask_compress import Compress
|
2023-03-08 09:01:20 +00:00
|
|
|
from flask_caching import Cache
|
2023-03-10 11:10:43 +00:00
|
|
|
from flask_assets import Environment, Bundle
|
2023-04-03 18:04:49 +00:00
|
|
|
from flask_login import LoginManager
|
2023-03-25 20:24:38 +00:00
|
|
|
from flask import Flask, render_template, abort
|
|
|
|
from werkzeug.exceptions import HTTPException
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
# Configuration
|
2023-03-02 13:19:10 +00:00
|
|
|
import platformdirs
|
2023-03-20 17:19:42 +00:00
|
|
|
from dotenv import load_dotenv
|
2023-03-23 12:54:00 +00:00
|
|
|
from yaml import safe_load
|
2023-03-20 17:19:42 +00:00
|
|
|
|
2023-04-03 18:04:49 +00:00
|
|
|
# Import database
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from gallery import db
|
|
|
|
|
2023-03-02 17:00:54 +00:00
|
|
|
|
2023-03-10 11:10:43 +00:00
|
|
|
USER_DIR = platformdirs.user_config_dir('onlylegs')
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-04-03 22:28:12 +00:00
|
|
|
|
2023-04-03 18:04:49 +00:00
|
|
|
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()
|
|
|
|
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-12 13:12:38 +00:00
|
|
|
def create_app(test_config=None):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create and configure the main app
|
|
|
|
"""
|
2023-03-11 22:14:03 +00:00
|
|
|
app = Flask(__name__, instance_path=os.path.join(USER_DIR, 'instance'))
|
2023-03-20 17:19:42 +00:00
|
|
|
|
2023-03-12 12:29:29 +00:00
|
|
|
# Get environment variables
|
|
|
|
load_dotenv(os.path.join(USER_DIR, '.env'))
|
2023-03-12 13:12:38 +00:00
|
|
|
print("Loaded environment variables")
|
2023-03-12 12:29:29 +00:00
|
|
|
|
|
|
|
# Get config file
|
2023-04-03 17:28:28 +00:00
|
|
|
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)
|
2023-04-03 17:28:28 +00:00
|
|
|
print("Loaded config")
|
2023-01-25 15:13:56 +00:00
|
|
|
|
|
|
|
# App configuration
|
2023-01-10 12:39:29 +00:00
|
|
|
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'),
|
2023-01-11 15:25:35 +00:00
|
|
|
ALLOWED_EXTENSIONS=conf['upload']['allowed-extensions'],
|
2023-03-02 17:00:54 +00:00
|
|
|
MAX_CONTENT_LENGTH=1024 * 1024 * conf['upload']['max-size'],
|
2023-03-01 23:29:34 +00:00
|
|
|
WEBSITE=conf['website'],
|
2023-01-10 12:39:29 +00:00
|
|
|
)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
if test_config is None:
|
|
|
|
app.config.from_pyfile('config.py', silent=True)
|
|
|
|
else:
|
|
|
|
app.config.from_mapping(test_config)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-04-03 18:04:49 +00:00
|
|
|
login_manager.init_app(app)
|
|
|
|
login_manager.login_view = 'gallery.index'
|
2023-04-03 22:28:12 +00:00
|
|
|
login_manager.session_protection = 'strong'
|
2023-04-03 18:04:49 +00:00
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_id):
|
2023-04-03 22:28:12 +00:00
|
|
|
return db_session.query(db.Users).filter_by(alt_id=user_id).first()
|
|
|
|
|
|
|
|
@login_manager.unauthorized_handler
|
|
|
|
def unauthorized():
|
|
|
|
return render_template('error.html', error=401,
|
|
|
|
msg='You are not authorized to view this page!!!!'), 401
|
2023-04-03 18:04:49 +00:00
|
|
|
|
2023-04-03 00:43:21 +00:00
|
|
|
# Load JS assets
|
2023-04-03 22:49:28 +00:00
|
|
|
assets.register('js_pre', Bundle('js/pre/*.js', output='gen/pre_packed.js', depends='js/pre/*.js'))
|
|
|
|
assets.register('js_post', Bundle('js/post/*.js', output='gen/post_packed.js', depends='js/pre/*.js'))
|
|
|
|
assets.register('styles', Bundle('sass/*.sass', filters='libsass', output='gen/styles.css', depends='sass/**/*.sass'))
|
2023-03-03 00:26:46 +00:00
|
|
|
|
2023-04-02 16:50:52 +00:00
|
|
|
# Error handlers, if the error is not a HTTP error, return 500
|
2023-03-25 20:24:38 +00:00
|
|
|
@app.errorhandler(Exception)
|
2023-04-02 16:50:52 +00:00
|
|
|
def error_page(err): # noqa
|
2023-03-26 23:34:03 +00:00
|
|
|
if not isinstance(err, HTTPException):
|
|
|
|
abort(500)
|
2023-04-03 22:28:12 +00:00
|
|
|
return render_template('error.html', error=err.code, msg=err.description), err.code
|
2023-03-10 12:32:23 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
# Load login, registration and logout manager
|
2023-03-12 15:52:23 +00:00
|
|
|
from gallery import auth
|
2023-01-10 14:40:43 +00:00
|
|
|
app.register_blueprint(auth.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
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)
|
2023-03-09 23:31:58 +00:00
|
|
|
app.register_blueprint(groups.blueprint)
|
2023-03-12 15:52:23 +00:00
|
|
|
app.register_blueprint(routing.blueprint)
|
2023-03-01 23:29:34 +00:00
|
|
|
app.register_blueprint(settings.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-03-12 18:19:43 +00:00
|
|
|
# Log to file that the app has started
|
2023-03-11 22:14:03 +00:00
|
|
|
logging.info('Gallery started successfully!')
|
2023-03-10 12:32:23 +00:00
|
|
|
|
2023-03-12 18:19:43 +00:00
|
|
|
# Initialize extensions and return app
|
2023-03-10 11:10:43 +00:00
|
|
|
assets.init_app(app)
|
2023-03-08 09:01:20 +00:00
|
|
|
cache.init_app(app)
|
2023-03-10 11:10:43 +00:00
|
|
|
compress.init_app(app)
|
2023-03-04 13:45:26 +00:00
|
|
|
return app
|