distribusi-verse/verse/start.py
2024-06-23 16:59:51 +02:00

115 lines
3.3 KiB
Python

"""This is the main flask distribusi page"""
from datetime import timedelta
from flask import (
Blueprint,
redirect,
render_template,
send_from_directory,
session,
url_for,
)
from flask_login import current_user, login_required, logout_user
from flask_wtf.csrf import CSRFError
from admin import is_adminuser
from admin_page.admin_page import admin_page
from app import create_app, login_manager, db
from describer.describe_files import describer
from distribusikan.distribusikan import distribusikan
from distribusikan.distribusis_info import DistribusisInfo
from distribusikan.forms.uploadform import UploadForm
from models.distribusi_model import Distribusis
from models.user_model import User
from statuspengguna.forgotpassword import forgot_password
from statuspengguna.helper import UserHelper
from statuspengguna.loginuser import login_section
from statuspengguna.registeruser import register_user
APP = create_app()
stash_page = Blueprint("stash_page", __name__, static_folder="stash")
APP.register_blueprint(stash_page)
APP.register_blueprint(describer, url_prefix="/describer")
APP.register_blueprint(login_section, url_prefix="/login")
APP.register_blueprint(register_user, url_prefix="/register")
APP.register_blueprint(forgot_password, url_prefix="/login/forgotpassword")
APP.register_blueprint(admin_page, url_prefix="/admin")
APP.register_blueprint(distribusikan, url_prefix="/distribusikan")
@APP.before_request
def session_handler():
session.permanent = True
APP.permanent_session_lifetime = timedelta(minutes=30)
@APP.route("/")
def index():
UserHelper.reset_user_state()
uploadform = UploadForm()
distribusis = DistribusisInfo.visible_distribusis()
distribusisindex = {}
for distribusi in distribusis:
user = User.query.filter_by(id=distribusi.userid).first()
singledistribusi = {
"username": user.username,
"publictheme": distribusi.publictheme,
"category": distribusi.category,
"year": distribusi.year,
"tags": distribusi.tags.split(","),
}
distribusisindex[distribusi.distribusiname] = singledistribusi
years = uploadform.year.choices
categories = uploadform.category.choices
adminuser = is_adminuser()
template = render_template(
"base/index.html",
distribusisindex=distribusisindex,
years=years,
categories=categories,
adminuser=adminuser,
)
return template
@APP.route("/help")
def help():
return render_template("base/help.html")
@APP.route("/publicthemes/<path>")
def publicthemes(path):
distribusi = Distribusis.query.filter_by(distribusiname=path).first()
publicthemefolder = f"publicthemes/{distribusi.distribusiname}/"
cssfile = f"{publicthemefolder}/{distribusi.publictheme}.css"
return send_from_directory("themes", cssfile, as_attachment=True)
@APP.route("/stash")
def shortstashurl():
return redirect(url_for("index"))
@APP.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for("index"))
@APP.errorhandler(CSRFError)
def handle_csrf_error(e):
return render_template("csrf_error.html", reason=e.description), 400
@login_manager.user_loader
def load_user(user_id):
return db.session.get(User, int(user_id))
if __name__ == "__main__":
APP.debug = True
APP.run(port=5000)