2023-03-04 13:45:26 +00:00
|
|
|
"""
|
2023-01-10 12:39:29 +00:00
|
|
|
___ _ _
|
2023-03-04 13:45:26 +00:00
|
|
|
/ _ \ _ __ | |_ _| | ___ __ _ ___
|
|
|
|
| | | | '_ \| | | | | | / _ \/ _` / __|
|
|
|
|
| |_| | | | | | |_| | |__| __/ (_| \__ \
|
|
|
|
\___/|_| |_|_|\__, |_____\___|\__, |___/
|
2023-01-10 12:39:29 +00:00
|
|
|
|___/ |___/
|
2023-03-08 13:36:35 +00:00
|
|
|
Created by Fluffy Bean - Version 23.03.09
|
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-03-04 13:45:26 +00:00
|
|
|
from flask import Flask, render_template
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
# Configuration
|
2023-01-10 15:41:39 +00:00
|
|
|
from dotenv import load_dotenv
|
2023-03-02 13:19:10 +00:00
|
|
|
import platformdirs
|
2023-03-04 13:45:26 +00:00
|
|
|
import yaml
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
from . import theme_manager
|
2023-03-10 11:10:43 +00:00
|
|
|
from . import setup
|
2023-03-02 17:00:54 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-10 11:10:43 +00:00
|
|
|
# Run setup checks
|
|
|
|
setup.SetupApp()
|
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
|
|
|
|
|
|
|
# Get environment variables
|
2023-03-10 11:10:43 +00:00
|
|
|
load_dotenv(os.path.join(USER_DIR, '.env'))
|
|
|
|
print("Loaded environment variables")
|
2023-03-02 13:19:10 +00:00
|
|
|
|
|
|
|
# Get config file
|
2023-03-10 11:10:43 +00:00
|
|
|
with open(os.path.join(USER_DIR, 'conf.yml'), encoding='utf-8') as f:
|
|
|
|
conf = yaml.load(f, Loader=yaml.FullLoader)
|
|
|
|
print("Loaded gallery config")
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-25 15:13:56 +00:00
|
|
|
def create_app(test_config=None):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create and configure the main app
|
|
|
|
"""
|
2023-03-10 11:10:43 +00:00
|
|
|
app = Flask(__name__,instance_path=os.path.join(USER_DIR, 'instance'))
|
|
|
|
assets = Environment()
|
2023-03-08 09:01:20 +00:00
|
|
|
cache = Cache(config={'CACHE_TYPE': 'SimpleCache', 'CACHE_DEFAULT_TIMEOUT': 69})
|
2023-03-10 11:10:43 +00:00
|
|
|
compress = Compress()
|
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-03-09 23:31:58 +00:00
|
|
|
ADMIN=conf['admin'],
|
|
|
|
APP_VERSION='23.03.09',
|
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-01-10 12:39:29 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(app.instance_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2023-01-25 15:13:56 +00:00
|
|
|
|
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
theme_manager.CompileTheme('default', app.root_path)
|
2023-03-10 11:10:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Bundle JS files
|
|
|
|
js = Bundle('js/*.js', output='gen/packed.js')
|
|
|
|
assets.register('js_all', js)
|
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
@app.errorhandler(405)
|
2023-03-04 13:45:26 +00:00
|
|
|
def method_not_allowed(err):
|
2023-01-10 12:39:29 +00:00
|
|
|
error = '405'
|
2023-03-04 13:45:26 +00:00
|
|
|
msg = err.description
|
|
|
|
return render_template('error.html', error=error, msg=msg), 404
|
2023-01-10 12:39:29 +00:00
|
|
|
|
|
|
|
@app.errorhandler(404)
|
2023-03-04 13:45:26 +00:00
|
|
|
def page_not_found(err):
|
2023-01-10 12:39:29 +00:00
|
|
|
error = '404'
|
2023-03-04 13:45:26 +00:00
|
|
|
msg = err.description
|
2023-01-10 12:39:29 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 404
|
|
|
|
|
|
|
|
@app.errorhandler(403)
|
2023-03-04 13:45:26 +00:00
|
|
|
def forbidden(err):
|
2023-01-10 12:39:29 +00:00
|
|
|
error = '403'
|
2023-03-04 13:45:26 +00:00
|
|
|
msg = err.description
|
2023-01-10 12:39:29 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 403
|
|
|
|
|
|
|
|
@app.errorhandler(410)
|
2023-03-04 13:45:26 +00:00
|
|
|
def gone(err):
|
2023-01-10 12:39:29 +00:00
|
|
|
error = '410'
|
2023-03-04 13:45:26 +00:00
|
|
|
msg = err.description
|
2023-01-10 12:39:29 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 410
|
|
|
|
|
|
|
|
@app.errorhandler(500)
|
2023-03-04 13:45:26 +00:00
|
|
|
def internal_server_error(err):
|
2023-01-10 12:39:29 +00:00
|
|
|
error = '500'
|
2023-03-04 13:45:26 +00:00
|
|
|
msg = err.description
|
2023-01-10 12:39:29 +00:00
|
|
|
return render_template('error.html', error=error, msg=msg), 500
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
# Load login, registration and logout manager
|
2023-01-10 12:39:29 +00:00
|
|
|
from . import auth
|
2023-01-10 14:40:43 +00:00
|
|
|
app.register_blueprint(auth.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 14:40:43 +00:00
|
|
|
# Load routes for home and images
|
2023-01-31 22:08:37 +00:00
|
|
|
from . import routing
|
|
|
|
app.register_blueprint(routing.blueprint)
|
2023-01-10 12:39:29 +00:00
|
|
|
app.add_url_rule('/', endpoint='index')
|
2023-03-09 23:31:58 +00:00
|
|
|
|
|
|
|
# Load routes for groups
|
|
|
|
from . import groups
|
|
|
|
app.register_blueprint(groups.blueprint)
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
# Load routes for settings
|
|
|
|
from . import settings
|
|
|
|
app.register_blueprint(settings.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-01-10 18:12:55 +00:00
|
|
|
# Load APIs
|
|
|
|
from . import api
|
|
|
|
app.register_blueprint(api.blueprint)
|
2023-03-10 11:10:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
logging.info('Gallery started successfully!')
|
2023-01-10 12:39:29 +00:00
|
|
|
|
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
|