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
|
|
|
|
|
2023-04-09 19:12:35 +00:00
|
|
|
from flask_assets import Bundle
|
2023-04-10 23:03:47 +00:00
|
|
|
from flask_migrate import init as migrate_init
|
2023-04-11 11:46:54 +00:00
|
|
|
|
2023-03-25 20:24:38 +00:00
|
|
|
from flask import Flask, render_template, abort
|
|
|
|
from werkzeug.exceptions import HTTPException
|
2023-04-11 08:04:52 +00:00
|
|
|
from werkzeug.security import generate_password_hash
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-04-12 16:58:13 +00:00
|
|
|
from onlylegs.extensions import db, migrate, login_manager, assets, compress, cache
|
2023-04-20 19:20:58 +00:00
|
|
|
from onlylegs.config import INSTANCE_DIR, MIGRATIONS_DIR
|
2023-04-14 20:12:08 +00:00
|
|
|
from onlylegs.models import User
|
2023-04-21 17:35:31 +00:00
|
|
|
from onlylegs.views import (
|
|
|
|
index as view_index,
|
|
|
|
image as view_image,
|
|
|
|
group as view_group,
|
|
|
|
settings as view_settings,
|
|
|
|
profile as view_profile,
|
|
|
|
)
|
2023-04-21 17:34:25 +00:00
|
|
|
from onlylegs.api import media as api_media, group as api_group, account as api_account
|
|
|
|
from onlylegs import auth as view_auth
|
2023-04-22 14:11:25 +00:00
|
|
|
from onlylegs import filters
|
2023-04-20 19:21:33 +00:00
|
|
|
from onlylegs import gwagwa
|
2023-03-02 13:19:10 +00:00
|
|
|
|
2023-04-03 22:28:12 +00:00
|
|
|
|
2023-04-09 19:12:35 +00:00
|
|
|
def create_app(): # pylint: disable=R0914
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Create and configure the main app
|
|
|
|
"""
|
2023-04-20 19:20:58 +00:00
|
|
|
app = Flask(__name__, instance_path=INSTANCE_DIR)
|
2023-04-10 15:43:42 +00:00
|
|
|
app.config.from_pyfile("config.py")
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-04-10 23:03:47 +00:00
|
|
|
# DATABASE
|
2023-04-09 19:12:35 +00:00
|
|
|
db.init_app(app)
|
2023-04-20 13:57:03 +00:00
|
|
|
migrate.init_app(app, db, directory=MIGRATIONS_DIR)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-04-10 23:03:47 +00:00
|
|
|
# If database file doesn't exist, create it
|
2023-04-20 19:20:58 +00:00
|
|
|
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
|
|
|
|
2023-04-12 15:16:43 +00:00
|
|
|
register_user = User(
|
2023-04-11 08:04:52 +00:00
|
|
|
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"),
|
2023-04-11 08:04:52 +00:00
|
|
|
)
|
|
|
|
db.session.add(register_user)
|
|
|
|
db.session.commit()
|
2023-04-12 15:18:13 +00:00
|
|
|
|
2023-04-11 08:04:52 +00:00
|
|
|
print(
|
2023-04-12 15:18:13 +00:00
|
|
|
"""
|
2023-04-11 08:04:52 +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
|
|
|
|
2023-04-10 23:03:47 +00:00
|
|
|
# Check if migrations directory exists, if not create it
|
2023-04-11 11:46:54 +00:00
|
|
|
with app.app_context():
|
|
|
|
if not os.path.exists(MIGRATIONS_DIR):
|
|
|
|
print("Creating migrations directory")
|
2023-04-10 23:03:47 +00:00
|
|
|
migrate_init(directory=MIGRATIONS_DIR)
|
2023-04-12 15:18:13 +00:00
|
|
|
|
2023-04-10 23:03:47 +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
|
2023-04-03 18:04:49 +00:00
|
|
|
login_manager.init_app(app)
|
2023-04-14 20:12:08 +00:00
|
|
|
login_manager.login_view = "onlylegs.index"
|
2023-04-03 18:04:49 +00:00
|
|
|
|
|
|
|
@login_manager.user_loader
|
2023-04-19 17:38:46 +00:00
|
|
|
def load_user(user_id): # skipcq: PTC-W0065
|
2023-04-12 15:16:43 +00:00
|
|
|
return User.query.filter_by(alt_id=user_id).first()
|
2023-04-03 22:28:12 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
2023-04-10 23:03:47 +00:00
|
|
|
# ERROR HANDLERS
|
2023-03-25 20:24:38 +00:00
|
|
|
@app.errorhandler(Exception)
|
2023-04-19 17:36:31 +00:00
|
|
|
def error_page(err): # skipcq: PTC-W0065
|
2023-04-10 23:03:47 +00:00
|
|
|
"""
|
|
|
|
Error handlers, if the error is not a HTTP error, return 500
|
|
|
|
"""
|
2023-03-26 23:34:03 +00:00
|
|
|
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,
|
|
|
|
)
|
2023-03-10 12:32:23 +00:00
|
|
|
|
2023-04-10 23:03:47 +00:00
|
|
|
# ASSETS
|
|
|
|
assets.init_app(app)
|
2023-04-07 12:35:30 +00:00
|
|
|
|
2023-04-20 17:38:44 +00:00
|
|
|
scripts = Bundle(
|
2023-04-22 19:58:10 +00:00
|
|
|
"js/*.js", output="gen/js.js", depends="js/*.js"
|
2023-04-20 17:38:44 +00:00
|
|
|
) # filter jsmin is broken :c
|
2023-04-09 19:12:35 +00:00
|
|
|
styles = Bundle(
|
2023-04-20 13:57:03 +00:00
|
|
|
"sass/style.sass",
|
2023-04-09 19:12:35 +00:00
|
|
|
filters="libsass, cssmin",
|
|
|
|
output="gen/styles.css",
|
|
|
|
depends="sass/**/*.sass",
|
|
|
|
)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-04-09 19:12:35 +00:00
|
|
|
assets.register("scripts", scripts)
|
|
|
|
assets.register("styles", styles)
|
2023-04-07 12:35:30 +00:00
|
|
|
|
2023-04-10 23:03:47 +00:00
|
|
|
# BLUEPRINTS
|
2023-04-21 17:34:25 +00:00
|
|
|
app.register_blueprint(view_auth.blueprint)
|
|
|
|
app.register_blueprint(view_index.blueprint)
|
|
|
|
app.register_blueprint(view_image.blueprint)
|
|
|
|
app.register_blueprint(view_group.blueprint)
|
|
|
|
app.register_blueprint(view_profile.blueprint)
|
|
|
|
app.register_blueprint(view_settings.blueprint)
|
2023-04-21 17:35:31 +00:00
|
|
|
|
2023-04-21 17:34:25 +00:00
|
|
|
# APIS
|
|
|
|
app.register_blueprint(api_media.blueprint)
|
|
|
|
app.register_blueprint(api_group.blueprint)
|
|
|
|
app.register_blueprint(api_account.blueprint)
|
2023-04-22 14:11:47 +00:00
|
|
|
|
2023-04-22 14:11:25 +00:00
|
|
|
# FILTERS
|
|
|
|
app.register_blueprint(filters.blueprint)
|
2023-01-25 15:13:56 +00:00
|
|
|
|
2023-04-10 23:03:47 +00:00
|
|
|
# CACHE AND COMPRESS
|
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-04-09 19:12:35 +00:00
|
|
|
|
2023-04-10 23:03:47 +00:00
|
|
|
# Yupee! We got there :3
|
2023-04-10 15:43:42 +00:00
|
|
|
print("Done!")
|
2023-04-09 19:12:35 +00:00
|
|
|
logging.info("Gallery started successfully!")
|
2023-03-04 13:45:26 +00:00
|
|
|
return app
|