2022-01-05 13:42:24 +01:00
|
|
|
"""This is the main flask distribusi page"""
|
2022-01-05 18:14:45 +01:00
|
|
|
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,
|
2022-03-25 11:24:49 +01:00
|
|
|
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
|
|
|
)
|
2022-03-25 15:37:00 +01:00
|
|
|
from flask_mail import Mail
|
2022-01-05 13:42:24 +01:00
|
|
|
from flask_wtf.csrf import CSRFError
|
2022-03-21 19:54:55 +01:00
|
|
|
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
|
2022-01-07 16:55:40 +01:00
|
|
|
|
2022-03-23 21:57:28 +01:00
|
|
|
# Use upload form to populate filters
|
2022-01-09 17:22:38 +01:00
|
|
|
from forms.uploadform import UploadForm
|
2022-01-05 18:14:45 +01:00
|
|
|
|
2022-03-23 21:57:28 +01:00
|
|
|
# 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
|
2022-03-21 19:54:55 +01:00
|
|
|
from distribusiworkflow import DistribusiWorkflow
|
2022-03-23 21:57:28 +01:00
|
|
|
from distribusiselector import DistribusiSelector
|
|
|
|
from uploadpage import UploadPage
|
2022-03-04 16:41:14 +01:00
|
|
|
|
2022-03-04 19:08:55 +01:00
|
|
|
# UserPengguna
|
2022-03-28 20:16:34 +02:00
|
|
|
from statuspengguna.helper import UserHelper
|
2022-02-13 22:24:56 +01:00
|
|
|
from statuspengguna.loginuser import LoginUser
|
|
|
|
from statuspengguna.registeruser import RegisterUser
|
2022-03-25 15:37:00 +01:00
|
|
|
from statuspengguna.forgotpassword import ForgotPassword
|
2022-03-27 16:07:44 +02:00
|
|
|
from statuspengguna.resetpassword import ResetPassword
|
2022-03-28 20:16:34 +02:00
|
|
|
|
|
|
|
# Distribusi Information
|
2022-03-19 16:19:51 +01:00
|
|
|
from distribusisinfo import DistribusisInfo
|
2022-02-12 19:44:44 +01:00
|
|
|
|
2022-01-05 13:42:24 +01:00
|
|
|
APP = create_app()
|
2022-03-25 11:24:49 +01:00
|
|
|
stash_page = Blueprint("stash_page", __name__, static_folder="stash")
|
|
|
|
APP.register_blueprint(stash_page)
|
2022-03-25 15:37:00 +01:00
|
|
|
mail = Mail(APP)
|
2022-01-05 13:42:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.before_request
|
|
|
|
def session_handler():
|
|
|
|
session.permanent = True
|
2022-02-12 19:44:44 +01:00
|
|
|
APP.permanent_session_lifetime = timedelta(minutes=30)
|
2022-01-05 13:42:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/")
|
|
|
|
def index():
|
2022-03-28 20:16:34 +02:00
|
|
|
UserHelper.reset_user_state()
|
2022-03-27 12:46:08 +02:00
|
|
|
# http://localhost:5000/themes/publicthemes/RomeroTape/blueskies.css
|
2022-03-06 23:15:38 +01:00
|
|
|
uploadform = UploadForm()
|
2022-03-20 19:23:39 +01:00
|
|
|
distribusis = DistribusisInfo.visibledistribusis()
|
2022-03-27 12:46:08 +02:00
|
|
|
distribusisindex = {}
|
2022-02-13 21:49:07 +01:00
|
|
|
for distribusi in distribusis:
|
|
|
|
user = User.query.filter_by(id=distribusi.userid).first()
|
2022-03-06 23:15:38 +01:00
|
|
|
singledistribusi = {
|
2022-03-18 14:58:30 +01:00
|
|
|
"username": user.username,
|
2022-03-27 12:46:08 +02:00
|
|
|
"publictheme": distribusi.publictheme,
|
2022-03-06 23:15:38 +01:00
|
|
|
"term": distribusi.term,
|
|
|
|
"course": distribusi.course,
|
|
|
|
"year": distribusi.year,
|
2022-03-18 18:05:42 +01:00
|
|
|
"tags": distribusi.tags.split(","),
|
2022-03-06 23:15:38 +01:00
|
|
|
}
|
2022-03-27 12:46:08 +02:00
|
|
|
distribusisindex[distribusi.distribusiname] = singledistribusi
|
2022-03-06 23:15:38 +01:00
|
|
|
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()
|
2022-03-06 23:15:38 +01:00
|
|
|
template = render_template(
|
|
|
|
"index.html",
|
2022-03-27 12:46:08 +02:00
|
|
|
distribusisindex=distribusisindex,
|
2022-03-06 23:15:38 +01:00
|
|
|
years=years,
|
|
|
|
terms=terms,
|
|
|
|
courses=courses,
|
2022-03-20 11:24:33 +01:00
|
|
|
adminuser=adminuser,
|
2022-03-06 23:15:38 +01:00
|
|
|
)
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
2022-01-07 16:55:40 +01:00
|
|
|
@APP.route("/distribusi", methods=["GET", "POST"])
|
2022-01-05 13:42:24 +01:00
|
|
|
@login_required
|
|
|
|
def distribusi():
|
2022-03-21 19:54:55 +01:00
|
|
|
return DistribusiWorkflow()
|
2022-01-05 18:14:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/upload", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
def upload():
|
2022-03-23 21:57:28 +01:00
|
|
|
uploadfolder = APP.config["UPLOAD_FOLDER"]
|
|
|
|
return UploadPage(uploadfolder)
|
2022-01-09 17:22:38 +01:00
|
|
|
|
|
|
|
|
2022-01-14 18:11:01 +01:00
|
|
|
@APP.route("/theme", methods=["GET", "POST"])
|
2022-01-09 17:22:38 +01:00
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2022-01-14 18:11:01 +01:00
|
|
|
@APP.route("/editor", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def editor():
|
2022-03-19 14:24:12 +01:00
|
|
|
return Editor()
|
2022-01-14 18:11:01 +01:00
|
|
|
|
|
|
|
|
2022-02-18 12:41:20 +01:00
|
|
|
@APP.route("/selector", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def selector():
|
2022-03-23 21:57:28 +01:00
|
|
|
return DistribusiSelector()
|
2022-02-18 12:41:20 +01:00
|
|
|
|
2022-03-04 16:41:14 +01:00
|
|
|
|
2022-03-25 11:24:49 +01:00
|
|
|
@APP.route("/stash")
|
|
|
|
def shortstashurl():
|
|
|
|
return redirect(url_for("index"))
|
2022-01-07 18:09:29 +01:00
|
|
|
|
|
|
|
|
2022-03-20 19:23:39 +01:00
|
|
|
@APP.route("/admin", methods=["GET", "POST"])
|
2022-01-05 13:42:24 +01:00
|
|
|
@login_required
|
|
|
|
def admin():
|
2022-03-23 22:45:32 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
2022-02-13 22:24:56 +01:00
|
|
|
@APP.route("/login", methods=["GET", "POST"])
|
|
|
|
def login():
|
2022-03-25 15:37:00 +01:00
|
|
|
return LoginUser()
|
2022-02-13 22:24:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/register", methods=["GET", "POST"])
|
|
|
|
def register():
|
2022-03-25 15:37:00 +01:00
|
|
|
return RegisterUser()
|
|
|
|
|
|
|
|
|
|
|
|
@APP.route("/forgotpassword", methods=["GET", "POST"])
|
|
|
|
def forgotpassword():
|
|
|
|
return ForgotPassword(mail)
|
2022-02-13 22:24:56 +01:00
|
|
|
|
|
|
|
|
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)
|