2022-01-05 13:42:24 +01:00
|
|
|
"""This is the main flask distribusi page"""
|
2024-04-21 20:44:08 +02:00
|
|
|
|
2022-01-05 18:14:45 +01:00
|
|
|
from datetime import timedelta
|
2024-04-21 20:44:08 +02:00
|
|
|
|
2024-04-28 13:15:23 +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-28 13:15:23 +02:00
|
|
|
|
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
|
2022-03-28 20:16:34 +02:00
|
|
|
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-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")
|
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)
|
2022-03-25 11:24:49 +01:00
|
|
|
APP.register_blueprint(stash_page)
|
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-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,
|
2024-04-21 21:28:44 +02:00
|
|
|
"category": distribusi.category,
|
2022-03-06 23:15:38 +01:00
|
|
|
"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
|
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()
|
2022-03-06 23:15:38 +01:00
|
|
|
template = render_template(
|
2024-04-28 13:04:07 +02:00
|
|
|
"base/index.html",
|
2022-03-27 12:46:08 +02:00
|
|
|
distribusisindex=distribusisindex,
|
2022-03-06 23:15:38 +01:00
|
|
|
years=years,
|
2024-04-19 22:55:07 +02:00
|
|
|
categories=categories,
|
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():
|
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)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@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)
|