distribusi-verse/verse/start.py

195 lines
4.7 KiB
Python

"""This is the main flask distribusi page"""
from datetime import timedelta
from flask import (
render_template,
redirect,
url_for,
session,
send_from_directory,
Blueprint,
)
from flask_login import (
logout_user,
login_required,
current_user,
)
from flask_mail import Mail
from flask_wtf.csrf import CSRFError
from app import create_app, login_manager
from usermodel import User
from distribusimodel import Distribusis
# Use upload form to populate filters
from forms.uploadform import UploadForm
# Interface! these are seperate files in main folder
from adminpage import AdminPage
from editor import Editor
from themeselector import ThemeSelector
from distribusiworkflow import DistribusiWorkflow
from distribusiselector import DistribusiSelector
from uploadpage import UploadPage
# UserPengguna
from statuspengguna.helper import UserHelper
from statuspengguna.loginuser import LoginUser
from statuspengguna.registeruser import RegisterUser
from statuspengguna.forgotpassword import ForgotPassword
from statuspengguna.resetpassword import ResetPassword
# Distribusi Information
from distribusisinfo import DistribusisInfo
APP = create_app()
stash_page = Blueprint("stash_page", __name__, static_folder="stash")
APP.register_blueprint(stash_page)
mail = Mail(APP)
@APP.before_request
def session_handler():
session.permanent = True
APP.permanent_session_lifetime = timedelta(minutes=30)
@APP.route("/")
def index():
UserHelper.reset_user_state()
# http://localhost:5000/themes/publicthemes/RomeroTape/blueskies.css
uploadform = UploadForm()
distribusis = DistribusisInfo.visibledistribusis()
distribusisindex = {}
for distribusi in distribusis:
user = User.query.filter_by(id=distribusi.userid).first()
singledistribusi = {
"username": user.username,
"publictheme": distribusi.publictheme,
"term": distribusi.term,
"course": distribusi.course,
"year": distribusi.year,
"tags": distribusi.tags.split(","),
}
distribusisindex[distribusi.distribusiname] = singledistribusi
years = uploadform.academicyear.choices
terms = uploadform.term.choices
courses = uploadform.course.choices
adminuser = isadminuser()
template = render_template(
"index.html",
distribusisindex=distribusisindex,
years=years,
terms=terms,
courses=courses,
adminuser=adminuser,
)
return template
@APP.route("/help")
def help():
return render_template("help.html")
@APP.route("/distribusi", methods=["GET", "POST"])
@login_required
def distribusi():
return DistribusiWorkflow()
@APP.route("/upload", methods=["POST"])
@login_required
def upload():
uploadfolder = APP.config["UPLOAD_FOLDER"]
return UploadPage(uploadfolder)
@APP.route("/theme", methods=["GET", "POST"])
@login_required
def theme():
return ThemeSelector()
@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"
print(cssfile)
return send_from_directory("themes", cssfile, as_attachment=True)
@APP.route("/editor", methods=["GET", "POST"])
@login_required
def editor():
return Editor()
@APP.route("/selector", methods=["GET", "POST"])
@login_required
def selector():
return DistribusiSelector()
@APP.route("/stash")
def shortstashurl():
return redirect(url_for("index"))
@APP.route("/admin", methods=["GET", "POST"])
@login_required
def admin():
if not isadminuser():
return redirect(url_for("index"))
return AdminPage()
@APP.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for("index"))
@APP.route("/login", methods=["GET", "POST"])
def login():
return LoginUser()
@APP.route("/register", methods=["GET", "POST"])
def register():
return RegisterUser()
@APP.route("/forgotpassword", methods=["GET", "POST"])
def forgotpassword():
return ForgotPassword(mail)
@APP.route("/resetpassword/<path>", methods=["GET", "POST"])
def resetpassword(path):
return ResetPassword(path)
@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))
def isadminuser():
if not current_user.is_authenticated:
return False
user = User.query.filter_by(email=current_user.email).first()
return user.admin
if __name__ == "__main__":
APP.debug = True
APP.run(port=5000)