python-gallery/onlylegs/__init__.py

126 lines
3.8 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
"""
import os
import logging
from flask_assets import Bundle
from flask_migrate import init as migrate_init
from flask import Flask, render_template, abort
from werkzeug.exceptions import HTTPException
from werkzeug.security import generate_password_hash
from onlylegs.extensions import db, migrate, login_manager, assets, compress, cache
from onlylegs.config import INSTANCE_DIR, MIGRATIONS_DIR
2023-04-14 20:12:08 +00:00
from onlylegs.models import User
from onlylegs.views import index, image, group, settings, profile
from onlylegs import api, auth
2023-04-20 19:21:33 +00:00
from onlylegs import gwagwa
def create_app(): # pylint: disable=R0914
2023-03-04 13:45:26 +00:00
"""
Create and configure the main app
"""
app = Flask(__name__, instance_path=INSTANCE_DIR)
2023-04-10 15:43:42 +00:00
app.config.from_pyfile("config.py")
# DATABASE
db.init_app(app)
2023-04-20 13:57:03 +00:00
migrate.init_app(app, db, directory=MIGRATIONS_DIR)
# If database file doesn't exist, create it
if not os.path.exists(os.path.join(INSTANCE_DIR, "gallery.sqlite3")):
2023-04-10 15:43:42 +00:00
print("Creating database")
with app.app_context():
db.create_all()
2023-04-12 15:18:13 +00:00
register_user = User(
username=app.config["ADMIN_CONF"]["username"],
2023-04-11 08:23:36 +00:00
email=app.config["ADMIN_CONF"]["email"],
2023-04-12 15:18:13 +00:00
password=generate_password_hash("changeme!", method="sha256"),
)
db.session.add(register_user)
db.session.commit()
2023-04-12 15:18:13 +00:00
print(
2023-04-12 15:18:13 +00:00
"""
####################################################
# DEFAULY ADMIN USER GENERATED WITH GIVEN USERNAME #
# THE DEFAULT PASSWORD "changeme!" HAS BEEN USED, #
# PLEASE UPDATE IT IN THE SETTINGS! #
####################################################
"""
)
2023-04-10 15:43:42 +00:00
# Check if migrations directory exists, if not create it
with app.app_context():
if not os.path.exists(MIGRATIONS_DIR):
print("Creating migrations directory")
migrate_init(directory=MIGRATIONS_DIR)
2023-04-12 15:18:13 +00:00
# LOGIN MANAGER
2023-04-12 15:30:54 +00:00
# can also set session_protection to "strong"
# this would protect against session hijacking
login_manager.init_app(app)
2023-04-14 20:12:08 +00:00
login_manager.login_view = "onlylegs.index"
@login_manager.user_loader
2023-04-19 17:38:46 +00:00
def load_user(user_id): # skipcq: PTC-W0065
return User.query.filter_by(alt_id=user_id).first()
@login_manager.unauthorized_handler
2023-04-19 17:36:31 +00:00
def unauthorized(): # skipcq: PTC-W0065
2023-04-04 14:00:00 +00:00
error = 401
2023-04-07 12:35:30 +00:00
msg = "You are not authorized to view this page!!!!"
return render_template("error.html", error=error, msg=msg), error
2023-04-04 14:00:00 +00:00
# ERROR HANDLERS
@app.errorhandler(Exception)
2023-04-19 17:36:31 +00:00
def error_page(err): # skipcq: PTC-W0065
"""
Error handlers, if the error is not a HTTP error, return 500
"""
if not isinstance(err, HTTPException):
abort(500)
2023-04-07 12:35:30 +00:00
return (
render_template("error.html", error=err.code, msg=err.description),
err.code,
)
# ASSETS
assets.init_app(app)
2023-04-07 12:35:30 +00:00
2023-04-20 17:38:44 +00:00
scripts = Bundle(
"js/*.js", output="gen/js.js", depends="js/*.js"
) # filter jsmin is broken :c
styles = Bundle(
2023-04-20 13:57:03 +00:00
"sass/style.sass",
filters="libsass, cssmin",
output="gen/styles.css",
depends="sass/**/*.sass",
)
assets.register("scripts", scripts)
assets.register("styles", styles)
2023-04-07 12:35:30 +00:00
# BLUEPRINTS
app.register_blueprint(auth.blueprint)
2023-03-12 15:52:23 +00:00
app.register_blueprint(api.blueprint)
2023-04-06 16:22:56 +00:00
app.register_blueprint(index.blueprint)
app.register_blueprint(image.blueprint)
app.register_blueprint(group.blueprint)
app.register_blueprint(profile.blueprint)
app.register_blueprint(settings.blueprint)
# CACHE AND COMPRESS
cache.init_app(app)
compress.init_app(app)
# Yupee! We got there :3
2023-04-10 15:43:42 +00:00
print("Done!")
logging.info("Gallery started successfully!")
2023-03-04 13:45:26 +00:00
return app