python-gallery/onlylegs/app.py

182 lines
4.8 KiB
Python
Raw Normal View History

2023-08-04 17:34:08 +00:00
"""
Onlylegs Gallery
This is the main app file, checks on app stability and runs all da shit
2023-08-04 17:34:08 +00:00
"""
import os
import logging
from flask import Flask, render_template, abort, request
from werkzeug.security import generate_password_hash
2023-08-04 17:34:08 +00:00
from werkzeug.exceptions import HTTPException
from flask_assets import Bundle
from flask_migrate import init as migrate_init
2023-08-04 17:34:08 +00:00
from onlylegs.extensions import db, migrate, login_manager, assets, compress, cache
from onlylegs.config import (
INSTANCE_DIR,
MIGRATIONS_DIR,
APPLICATION_ROOT,
DATABASE_NAME,
)
2023-08-04 17:34:08 +00:00
from onlylegs.models import Users
from onlylegs.views.index import blueprint as view_index
from onlylegs.views.image import blueprint as view_image
from onlylegs.views.group import blueprint as view_group
from onlylegs.views.settings import blueprint as view_settings
from onlylegs.views.profile import blueprint as view_profile
from onlylegs.api import blueprint as api
from onlylegs.auth import blueprint as view_auth
from onlylegs.filters import blueprint as filters
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",
)
2023-08-04 17:34:08 +00:00
def create_db():
path_to_database = os.path.join(INSTANCE_DIR, DATABASE_NAME)
2023-08-04 17:34:08 +00:00
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",
)
2023-08-04 17:34:08 +00:00
return
2023-08-04 17:34:08 +00:00
print("Database found, continuing...")
2023-08-04 17:34:08 +00:00
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"
2023-08-04 17:34:08 +00:00
@login_manager.user_loader
def load_user(user_id):
return Users.query.filter_by(alt_id=user_id).first()
2023-08-04 17:34:08 +00:00
@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
2023-08-04 17:34:08 +00:00
def page_assets():
2023-08-04 17:34:08 +00:00
"""
ASSETS
bundles all the sass and js and minifies them
2023-08-04 17:34:08 +00:00
"""
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",
)
2023-08-04 17:34:08 +00:00
assets.register("scripts", page_scripts)
assets.register("styles", page_styling)
2023-08-04 17:34:08 +00:00
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)
app.config.from_pyfile("config.py")
db.init_app(app)
migrate.init_app(app, db, directory=MIGRATIONS_DIR)
2023-08-04 17:34:08 +00:00
create_db()
2023-08-04 17:34:08 +00:00
set_logger()
set_login_manager()
page_assets()
handle_errors()
register_blueprints()
2023-08-04 17:34:08 +00:00
cache.init_app(app)
compress.init_app(app)
logging.info("Gallery started successfully!")
2023-09-24 09:48:03 +00:00
if __name__ == "__main__":
app.run()