distribusi-verse/verse/start.py

195 lines
4.7 KiB
Python
Raw Normal View History

2022-01-05 13:42:24 +01:00
"""This is the main flask distribusi page"""
from datetime import timedelta
2022-01-05 13:42:24 +01:00
from flask import (
render_template,
redirect,
url_for,
session,
2022-03-28 18:33:48 +02:00
send_from_directory,
Blueprint,
2022-01-05 13:42:24 +01:00
)
from flask_login import (
logout_user,
login_required,
2022-01-05 15:10:18 +01:00
current_user,
2022-01-05 13:42:24 +01:00
)
from flask_mail import Mail
2022-01-05 13:42:24 +01:00
from flask_wtf.csrf import CSRFError
from app import create_app, login_manager
2022-03-27 12:46:08 +02:00
2022-01-05 13:42:24 +01:00
from usermodel import User
2022-03-28 18:33:48 +02:00
from distribusimodel import Distribusis
# Use upload form to populate filters
from forms.uploadform import UploadForm
# Interface! these are seperate files in main folder
2022-03-23 21:30:59 +01:00
from adminpage import AdminPage
2022-03-19 16:19:51 +01:00
from editor import Editor
from themeselector import ThemeSelector
from distribusiworkflow import DistribusiWorkflow
from distribusiselector import DistribusiSelector
from uploadpage import UploadPage
2022-03-04 19:08:55 +01:00
# UserPengguna
from statuspengguna.helper import UserHelper
from statuspengguna.loginuser import LoginUser
from statuspengguna.registeruser import RegisterUser
from statuspengguna.forgotpassword import ForgotPassword
2022-03-27 16:07:44 +02:00
from statuspengguna.resetpassword import ResetPassword
# Distribusi Information
2022-03-19 16:19:51 +01:00
from distribusisinfo import DistribusisInfo
2022-01-05 13:42:24 +01:00
APP = create_app()
stash_page = Blueprint("stash_page", __name__, static_folder="stash")
APP.register_blueprint(stash_page)
mail = Mail(APP)
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()
2022-03-27 12:46:08 +02:00
# http://localhost:5000/themes/publicthemes/RomeroTape/blueskies.css
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,
"term": distribusi.term,
"course": distribusi.course,
"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
years = uploadform.academicyear.choices
terms = uploadform.term.choices
courses = uploadform.course.choices
2022-03-08 22:58:05 +01:00
2022-03-20 11:24:33 +01:00
adminuser = isadminuser()
template = render_template(
"index.html",
2022-03-27 12:46:08 +02:00
distribusisindex=distribusisindex,
years=years,
terms=terms,
courses=courses,
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():
return render_template("help.html")
@APP.route("/distribusi", methods=["GET", "POST"])
2022-01-05 13:42:24 +01:00
@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():
2022-03-19 16:19:51 +01:00
return ThemeSelector()
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"
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"))
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.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)
2022-03-27 16:07:44 +02:00
@APP.route("/resetpassword/<path>", methods=["GET", "POST"])
def resetpassword(path):
return ResetPassword(path)
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)