1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00
AdvLabDB/advlabdb/routes.py

84 lines
2.4 KiB
Python

from flask import redirect, render_template, request, url_for, flash
from flask_security import auth_required, current_user
from advlabdb import app
from advlabdb.models import Semester
from advlabdb.utils import setUserActiveSemester, titleToTemplate, userActiveSemester
@app.context_processor
def util_processor():
def semesterDropDownItems():
semesters = Semester.query.all()
items = []
for semester in semesters:
items.append(
'<li><a class="dropdown-item" href="'
+ url_for("set_semester")
+ "?semester_id="
+ str(semester.id)
+ '">'
+ semester.label
+ "</a></li>"
)
return items
def navbarItems(title):
adminPages = []
assistantPages = []
if current_user.is_authenticated:
roleNames = [role.name for role in current_user.roles]
if "assistant" in roleNames:
pages = assistantPages
if "admin" in roleNames:
for p in adminPages:
if p not in pages:
pages.append(p)
elif "admin" in roleNames:
pages = adminPages
pages.append("Logout")
else:
pages = ["Login"]
items = []
for page in pages:
if page == title:
active = " active"
else:
active = ""
items.append('<a class="nav-link' + active + '" href="' + titleToTemplate(page) + '">' + page + "</a>")
return items
return dict(
semesterDropDownItems=semesterDropDownItems,
userActiveSemester=userActiveSemester,
navbarItems=navbarItems,
current_user=current_user,
)
@app.route("/")
def index():
if current_user.has_role("admin"):
return redirect(url_for("index") + "admin")
elif current_user.has_role("assistant"):
return redirect(url_for("index") + "assistant")
else:
return render_template("index.html")
@app.route("/set_semester")
@auth_required()
def set_semester():
try:
semesterId = int(request.args.get("semester_id"))
except Exception as ex:
flash(str(ex), "error")
else:
setUserActiveSemester(semesterId)
red = request.referrer or url_for("index")
return redirect(red)