distribusi-verse/verse/start.py

129 lines
3.5 KiB
Python
Raw Normal View History

2022-01-05 13:42:24 +01:00
"""This is the main flask distribusi page"""
2024-04-21 20:44:08 +02:00
from datetime import timedelta
2024-04-21 20:44:08 +02:00
from flask import (
Blueprint,
redirect,
render_template,
send_from_directory,
session,
url_for,
)
2024-04-24 23:05:36 +02:00
from flask_login import current_user, login_required, logout_user
from flask_mail import Mail
from flask_wtf.csrf import CSRFError
2024-04-28 13:04:07 +02:00
# Interface! these are seperate files in main folder
from adminpage import AdminPage
from app import create_app, login_manager
from distribusikan.distribusikan import distribusikan
from distribusikan.distribusisinfo import DistribusisInfo
2024-04-21 20:44:08 +02:00
# Use upload form to populate filters
from forms.uploadform import UploadForm
2024-04-28 15:34:29 +02:00
from models.distribusi_model import Distribusis
from models.user_model import User
2024-04-28 13:04:07 +02:00
from statuspengguna.forgotpassword import forgot_password
from statuspengguna.helper import UserHelper
2024-04-28 13:04:07 +02:00
from statuspengguna.loginuser import login_section
from statuspengguna.registeruser import register_user
2022-01-05 13:42:24 +01:00
APP = create_app()
stash_page = Blueprint("stash_page", __name__, static_folder="stash")
2024-04-28 13:04:07 +02:00
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(distribusikan)
APP.register_blueprint(stash_page)
2022-01-05 13:42:24 +01:00
@APP.before_request
def session_handler():
session.permanent = True
APP.permanent_session_lifetime = timedelta(minutes=30)
2022-01-05 13:42:24 +01:00
@APP.route("/")
def index():
UserHelper.reset_user_state()
uploadform = UploadForm()
distribusis = DistribusisInfo.visibledistribusis()
2022-03-27 12:46:08 +02:00
distribusisindex = {}
for distribusi in distribusis:
user = User.query.filter_by(id=distribusi.userid).first()
singledistribusi = {
"username": user.username,
2022-03-27 12:46:08 +02:00
"publictheme": distribusi.publictheme,
2024-04-21 21:28:44 +02:00
"category": distribusi.category,
"year": distribusi.year,
2022-03-18 18:05:42 +01:00
"tags": distribusi.tags.split(","),
}
2022-03-27 12:46:08 +02:00
distribusisindex[distribusi.distribusiname] = singledistribusi
2024-04-19 22:55:07 +02:00
years = uploadform.year.choices
categories = uploadform.category.choices
2022-03-20 11:24:33 +01:00
adminuser = isadminuser()
template = render_template(
2024-04-28 13:04:07 +02:00
"base/index.html",
2022-03-27 12:46:08 +02:00
distribusisindex=distribusisindex,
years=years,
2024-04-19 22:55:07 +02:00
categories=categories,
2022-03-20 11:24:33 +01:00
adminuser=adminuser,
)
return template
2022-01-05 13:42:24 +01:00
2022-03-27 12:46:08 +02:00
@APP.route("/help")
def help():
2024-04-28 13:04:07 +02:00
return render_template("base/help.html")
2022-01-05 13:42:24 +01:00
2022-03-28 18:33:48 +02:00
@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"))
2022-01-07 18:09:29 +01:00
@APP.route("/admin", methods=["GET", "POST"])
2022-01-05 13:42:24 +01:00
@login_required
def admin():
if not isadminuser():
return redirect(url_for("index"))
2022-03-20 11:24:33 +01:00
return AdminPage()
2022-01-05 13:42:24 +01:00
@APP.route("/logout")
@login_required
def logout():
logout_user()
2022-01-05 15:10:18 +01:00
return redirect(url_for("index"))
2022-01-05 13:42:24 +01:00
@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 User.query.get(int(user_id))
2022-03-20 11:24:33 +01:00
def isadminuser():
if not current_user.is_authenticated:
return False
user = User.query.filter_by(email=current_user.email).first()
return user.admin
2022-01-05 13:42:24 +01:00
if __name__ == "__main__":
APP.debug = True
APP.run(port=5000)