from advlabdb import app, bcrypt from flask import render_template, request, url_for, flash, redirect 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 pages = ["Index", "Students", "Assistants", "Experiments", "Appointments", "Groups", "Users", "Semesters"] def navbarItems(title): items = [] for page in pages[1:]: if page == title: active = " active" else: active = "" items.append('' + page + '') return items return dict(semesterDropDownItems=semesterDropDownItems, activeSemesterLabel=Semester.query.get(activeSemester_id).label, pages=pages, navbarItems=navbarItems) @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") 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") 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") 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") def appointments(): title = "Appointments" return render_template(titleToTemplate(title) + ".html", title=title) @app.route("/groups") 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") def users(): headerAndDataList = [["Email", "row.email"], ["Is admin", "row.is_admin"], ["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"]) 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") 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 = bcrypt.generate_password_hash(password).decode("utf-8") email = form.email.data.lower() admin = form.admin.data user = User(email=email, password_hash=passwordHash, is_admin=admin) db.session.add(user) 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)