distribusi-verse: medium-tech web app content management system for the web
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.0 KiB

"""This is the main flask distribusi page"""
from datetime import timedelta
from flask import (
render_template,
redirect,
url_for,
session,
send_from_directory,
)
from flask_login import (
logout_user,
login_required,
current_user,
)
from flask_wtf.csrf import CSRFError
from app import create_app, login_manager
from usermodel import User
# Forms!
from forms.uploadform import UploadForm
from forms.distribusiform import DistribusiForm
from forms.themeform import ThemeForm
from forms.publicthemeform import PublicThemeForm
from forms.selectorform import SelectorForm
# Interface!
from adminpage import AdminPage
from editor import Editor
from themeselector import ThemeSelector
from distribusiworkflow import DistribusiWorkflow
from distribusiselector import SelectorPage, SelectorVisible
from upload import UploadNewDistribusi, UploadUpdatedFiles
# UserPengguna
from statuspengguna.helper import (
IsZipUploaded,
IsCssSelected,
CurrentDistribusi,
HasDistribusi,
IsDistribusiLive,
ResetUserState,
DistribusiLimitReached,
)
from statuspengguna.loginuser import LoginUser
from statuspengguna.registeruser import RegisterUser
from distribusisinfo import DistribusisInfo
APP = create_app()
@APP.before_request
def session_handler():
session.permanent = True
APP.permanent_session_lifetime = timedelta(minutes=30)
@APP.route("/")
def index():
ResetUserState()
uploadform = UploadForm()
distribusis = DistribusisInfo.visibledistribusis()
distribusies = {}
for distribusi in distribusis:
user = User.query.filter_by(id=distribusi.userid).first()
singledistribusi = {
"username": user.username,
"term": distribusi.term,
"course": distribusi.course,
"year": distribusi.year,
"tags": distribusi.tags.split(","),
}
distribusies[distribusi.distribusiname] = singledistribusi
years = uploadform.academicyear.choices
terms = uploadform.term.choices
courses = uploadform.course.choices
adminuser = isadminuser()
template = render_template(
"index.html",
distribusies=distribusies,
years=years,
terms=terms,
courses=courses,
adminuser=adminuser,
)
return template
@APP.route("/distribusi", methods=["GET", "POST"])
@login_required
def distribusi():
return DistribusiWorkflow()
@APP.route("/upload", methods=["POST"])
@login_required
def upload():
distribusiform = DistribusiForm()
themeform = ThemeForm()
publicthemeform = PublicThemeForm()
publicthemeform.publicthemes.choices = DistribusisInfo.publicthemes()
selectorform = SelectorForm()
selectorform.distribusis.choices = DistribusisInfo.userdistribusinames()
selectorvisible = SelectorVisible()
current_distribusi = CurrentDistribusi()
if current_distribusi == "new" or HasDistribusi() is False:
uploadform = UploadNewDistribusi(APP.config["UPLOAD_FOLDER"])
else:
uploadform = UploadUpdatedFiles(APP.config["UPLOAD_FOLDER"])
files_uploaded = IsZipUploaded(uploadform.sitename.data)
distribusi_live = IsDistribusiLive(current_distribusi)
css_selected = IsCssSelected(current_distribusi)
limit_reached = DistribusiLimitReached()
template = render_template(
"distribusi.html",
uploadform=uploadform,
distribusiform=distribusiform,
themeform=themeform,
publicthemeform=publicthemeform,
selectorform=selectorform,
files_uploaded=files_uploaded,
distribusi_live=distribusi_live,
css_selected=css_selected,
selectorvisible=selectorvisible,
limit_reached=limit_reached,
)
return template
@APP.route("/theme", methods=["GET", "POST"])
@login_required
def theme():
return ThemeSelector()
@APP.route("/editor", methods=["GET", "POST"])
@login_required
def editor():
return Editor()
@APP.route("/selector", methods=["GET", "POST"])
@login_required
def selector():
return SelectorPage()
@APP.route("/stash/<path:path>")
def distribusistash(path):
return send_from_directory("stash", path)
@APP.route("/admin", methods=["GET", "POST"])
@login_required
def admin():
return AdminPage()
@APP.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for("index"))
@APP.route("/login", methods=["GET", "POST"])
def login():
result = LoginUser()
return result
@APP.route("/register", methods=["GET", "POST"])
def register():
result = RegisterUser()
return result
@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)