Move colours to HSL, probably a mistake

This commit is contained in:
Michał Gdula 2023-09-26 19:36:49 +01:00
parent 9821db72c6
commit 1a59e413a9
29 changed files with 852 additions and 730 deletions

View file

@ -5,14 +5,20 @@ This is the main app file, checks on app stability and runs all da shit
import os
import logging
from flask import Flask, render_template, abort, request
from werkzeug.security import generate_password_hash
from werkzeug.exceptions import HTTPException
from flask_assets import Bundle
from flask_migrate import init as migrate_init
from flask import Flask, render_template, abort, request
from werkzeug.exceptions import HTTPException
from onlylegs.utils import startup
from onlylegs.extensions import db, migrate, login_manager, assets, compress, cache
from onlylegs.config import INSTANCE_DIR, MIGRATIONS_DIR, APPLICATION_ROOT
from onlylegs.config import (
INSTANCE_DIR,
MIGRATIONS_DIR,
APPLICATION_ROOT,
DATABASE_NAME,
)
from onlylegs.models import Users
from onlylegs.views.index import blueprint as view_index
@ -25,14 +31,130 @@ from onlylegs.auth import blueprint as view_auth
from onlylegs.filters import blueprint as filters
logging.getLogger("werkzeug").disabled = True
logging.basicConfig(
filename=os.path.join(APPLICATION_ROOT, "only.log"),
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
format="%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s",
encoding="utf-8",
)
def set_logger():
file_name = os.path.join(APPLICATION_ROOT, "only.log")
logging_level = logging.INFO
date_format = "%Y-%m-%d %H:%M:%S"
log_format = "%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s"
logging.getLogger("werkzeug").disabled = True
logging.basicConfig(
filename=file_name,
level=logging_level,
datefmt=date_format,
format=log_format,
encoding="utf-8",
)
def create_db():
path_to_database = os.path.join(INSTANCE_DIR, DATABASE_NAME)
if not os.path.exists(path_to_database):
print("Database not found, creating...")
user = Users(
username=app.config["ADMIN_CONF"]["username"],
email=app.config["ADMIN_CONF"]["email"],
password=generate_password_hash("changeme!", method="scrypt"),
)
with app.app_context():
db.create_all()
db.session.add(user)
db.session.commit()
migrate_init(directory=MIGRATIONS_DIR)
print(
"####################################################",
"# DEFAULT ADMIN USER GENERATED WITH GIVEN USERNAME #",
'# THE DEFAULT PASSWORD "changeme!" HAS BEEN USED, #',
"# PLEASE RESET IT IN THE SETTINGS! #",
"####################################################",
sep="\n",
)
return
print("Database found, continuing...")
def set_login_manager():
"""
LOGIN MANAGER
can also set session_protection to "strong"
this would protect against session hijacking
"""
login_manager.init_app(app)
login_manager.login_view = "onlylegs.index"
@login_manager.user_loader
def load_user(user_id):
return Users.query.filter_by(alt_id=user_id).first()
@login_manager.unauthorized_handler
def unauthorized():
error = 401
msg = "You are not authorized to view this page!!!!"
return render_template("error.html", error=error, msg=msg), error
def page_assets():
"""
ASSETS
bundles all the sass and js and minifies them
"""
assets.init_app(app)
page_scripts = Bundle(
"js/*.js", filters="jsmin", output="gen/main.js", depends="js/*.js"
)
page_styling = Bundle(
"sass/style.sass",
filters="libsass, cssmin",
output="gen/styles.css",
depends="sass/**/*.sass",
)
assets.register("scripts", page_scripts)
assets.register("styles", page_styling)
def handle_errors():
"""
ERROR HANDLER
handles all the errors and returns a nice error page
Code errors are displayed as 500 errors so no
sensitive information is leaked
"""
@app.errorhandler(Exception)
def error_page(err):
if not isinstance(err, HTTPException):
abort(500)
if request.method == "GET":
return (
render_template("error.html", error=err.code, msg=err.description),
err.code,
)
else:
return str(err.code) + ": " + err.description, err.code
def register_blueprints():
"""
BLUEPRINTS
registers all the blueprints
"""
app.register_blueprint(view_auth)
app.register_blueprint(view_index)
app.register_blueprint(view_image)
app.register_blueprint(view_group)
app.register_blueprint(view_profile)
app.register_blueprint(view_settings)
app.register_blueprint(api)
app.register_blueprint(filters)
app = Flask(__name__, instance_path=INSTANCE_DIR)
@ -41,85 +163,17 @@ app.config.from_pyfile("config.py")
db.init_app(app)
migrate.init_app(app, db, directory=MIGRATIONS_DIR)
# App Sanity Checks
startup.check_dirs()
startup.check_env()
startup.check_conf()
create_db()
if not os.path.exists(os.path.join(INSTANCE_DIR, "gallery.sqlite3")):
startup.make_admin_user(app)
migrate_init(directory=MIGRATIONS_DIR)
set_logger()
set_login_manager()
page_assets()
handle_errors()
register_blueprints()
# LOGIN MANAGER
# can also set session_protection to "strong"
# this would protect against session hijacking
login_manager.init_app(app)
login_manager.login_view = "onlylegs.index"
@login_manager.user_loader
def load_user(user_id):
return Users.query.filter_by(alt_id=user_id).first()
@login_manager.unauthorized_handler
def unauthorized():
error = 401
msg = "You are not authorized to view this page!!!!"
return render_template("error.html", error=error, msg=msg), error
# ERROR HANDLERS
@app.errorhandler(Exception)
def error_page(err):
"""
Error handlers, if the error is not a HTTP error, return 500
"""
if not isinstance(err, HTTPException):
abort(500)
if request.method == "GET":
return (
render_template("error.html", error=err.code, msg=err.description),
err.code,
)
else:
return str(err.code) + ": " + err.description, err.code
# ASSETS
assets.init_app(app)
page_scripts = Bundle(
"js/*.js", filters="jsmin", output="gen/main.js", depends="js/*.js"
)
page_styling = Bundle(
"sass/style.sass",
filters="libsass, cssmin",
output="gen/styles.css",
depends="sass/**/*.sass",
)
assets.register("scripts", page_scripts)
assets.register("styles", page_styling)
# BLUEPRINTS
app.register_blueprint(view_auth)
app.register_blueprint(view_index)
app.register_blueprint(view_image)
app.register_blueprint(view_group)
app.register_blueprint(view_profile)
app.register_blueprint(view_settings)
app.register_blueprint(api)
app.register_blueprint(filters)
# CACHE AND COMPRESS
cache.init_app(app)
compress.init_app(app)
# Yupee! We got there :3
print("Done!")
logging.info("Gallery started successfully!")