from advlabdb import app, user_datastore from flask import render_template, request, url_for, flash, redirect from flask_security import auth_required, roles_required, hash_password, current_user from advlabdb.utils import * from advlabdb.models import * from advlabdb.forms import * activeSemester_id = 0 @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 = ["Students", "Assistants", "Experiments", "Appointments", "Groups", "Users", "Semesters"] assistantPages = ["Students", "Assistants", "Experiments", "Appointments", "Groups"] 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, activeSemesterLabel=Semester.query.get(activeSemester_id).label, navbarItems=navbarItems, current_user=current_user, ) @app.route("/") def index(): global activeSemester_id activeSemester_id = Semester.query.all()[-1].id title = "Index" return render_template(titleToTemplate(title) + ".html", title=title, ) @app.route("/students") @roles_required("admin") def students(): semester = Semester.query.get(activeSemester_id) parts = semester.parts tables = [] tablesLabels = [] headerAndDataList = [["Student number", "row.student.student_number"], ["First name", "row.student.first_name"], ["Last name", "row.student.last_name"], ["Email", "row.student.email"], ["GN", "row.group.number"], ["Bachelor thesis", "row.student.bachelor_thesis"], ["BT WG", "row.student.bachelor_thesis_work_group"], ["Note", "row.student.note"], ["Final part mark", "row.final_part_mark"], ["Experiemt marks", "row.experiment_marks"], ["Parts", "[ps.part for ps in row.student.part_students]"]] for part in parts: tablesLabels.append("Part " + part.label + ":") tables.append(makeTable(headerAndDataList=headerAndDataList, rows=part.part_students)) title = "Students" return render_template(titleToTemplate(title) + ".html", title=title, tables=tables, tablesLabels=tablesLabels, ) @app.route("/assistants") @roles_required("admin") def assistants(): headerAndDataList = [["First name", "row.first_name"], ["Last name", "row.last_name"], ["Email", "row.email"], ["Phone number", "row.phone_number"], ["Mobile phone number", "row.mobile_phone_number"], ["Room", "row.room"], ["Building", "row.building"], ["User", "row.user"], ["Appointments", "row.appointments"], ["Experiemt marks", "row.experiment_marks"]] table = makeTable(headerAndDataList=headerAndDataList, rows=Assistant.query.filter(Assistant.user_id != None).all()) title = "Assistants" return render_template(titleToTemplate(title) + ".html", title=title, table=table, ) @app.route("/experiments") @roles_required("admin") def experiments(): semester = Semester.query.get(activeSemester_id) parts = semester.parts tables = [] tablesLabels = [] headerAndDataList = [["Number", "row.experiment.number"], ["Name", "row.experiment.name"], ["Assistants", "row.assistants"], ["Groups with this ex.", "[gEx.group.number for gEx in row.group_experiments]"]] for part in parts: tablesLabels.append("Part " + part.label + ":") tables.append(makeTable(headerAndDataList=headerAndDataList, rows=part.part_experiments)) title = "Experiments" return render_template(titleToTemplate(title) + ".html", title=title, tables=tables, tablesLabels=tablesLabels, ) @app.route("/appointments") @roles_required("admin") def appointments(): title = "Appointments" return render_template(titleToTemplate(title) + ".html", title=title, ) @app.route("/groups") @roles_required("admin") def groups(): semester = Semester.query.get(activeSemester_id) parts = semester.parts tables = [] tablesLabels = [] headerAndDataList = [["Number", "row.number"], ["Students", "[ps.student.first_name + ' ' + ps.student.last_name for ps in row.part_students]"], ["Experiments (appointments)", "[str(gx.part_experiment.experiment.number) + ' (' + str([appointmentDate(a.date) for a in gx.appointments]) + ')' for gx in row.group_experiments]"]] for part in parts: tablesLabels.append("Part " + part.label + ":") tables.append(makeTable(headerAndDataList=headerAndDataList, rows=part.groups)) title = "Groups" return render_template(titleToTemplate(title) + ".html", title=title, tables=tables, tablesLabels=tablesLabels, ) @app.route("/users") @roles_required("admin") def users(): headerAndDataList = [["Email", "row.email"], ["Roles", "[role.name for role in row.roles]"], ["Assistant", "row.assistant"]] table = makeTable(headerAndDataList=headerAndDataList, rows=User.query.all()) title = "Users" return render_template(titleToTemplate(title) + ".html", title=title, table=table, ) @app.route("/set_semester", methods=["GET"]) @auth_required() def set_semester(): global activeSemester_id activeSemester_id = int(request.args.get("semester_id")) title = "Index" return render_template(titleToTemplate(title) + ".html", title=title, ) @app.route("/semesters") @roles_required("admin") def semesters(): headerAndDataList = [["Label", "row.label"], ["Parts", "row.parts"]] table = makeTable(headerAndDataList=headerAndDataList, rows=Semester.query.all()) title = "Semesters" return render_template(titleToTemplate(title) + ".html", title=title, table=table, ) @app.route("/register", methods=["GET", "POST"]) def register(): form = RegistrationForm() if form.validate_on_submit(): password = randomPassword() passwordHash = hash_password(password) email = form.email.data.lower() admin = form.admin.data roles = ["assistant"] if not admin else ["admin"] user_datastore.create_user(email=email, password=passwordHash, roles=roles) db.session.commit() return render_template("registered.html", title="Registered", email=email, password=password, admin=admin, ) return render_template("register.html", title="Register", form=form, )