from flask import redirect, render_template, request, url_for 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( '
  • ' + semester.label + "
  • " ) 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('' + page + "") 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(): setUserActiveSemester(request.args.get("semester_id")) ref = request.referrer red = ref if ref else url_for("index") return redirect(red)