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
2021-04-01 01:04:21 +02:00

180 lines
6.7 KiB
Python

from advlabdb import app
from flask import render_template, request, url_for, flash, redirect
from werkzeug.security import check_password_hash, generate_password_hash
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('<li><a class="dropdown-item" href="' + url_for("set_semester") + "?semester_id=" +
str(semester.id) + '">' + semester.label + '</a></li>')
return items
return dict(semesterDropDownItems=semesterDropDownItems,
activeSemesterLabel=Semester.query.get(activeSemester_id).label)
@app.route("/")
def index():
global activeSemester_id
activeSemester_id = Semester.query.all()[-1].id
page = "index"
return render_template(page + ".html", navbarItems=navbarItems(page))
@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))
page = "students"
return render_template(page + ".html", navbarItems=navbarItems(page),
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())
page = "assistants"
return render_template(page + ".html", navbarItems=navbarItems(page),
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))
page = "experiments"
return render_template(page + ".html", navbarItems=navbarItems(page),
tables=tables, tablesLabels=tablesLabels)
@app.route("/appointments")
def appointments():
page = "appointments"
return render_template(page + ".html", navbarItems=navbarItems(page))
@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))
page = "groups"
return render_template(page + ".html", navbarItems=navbarItems(page),
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())
page = "users"
return render_template(page + ".html", navbarItems=navbarItems(page),
table=table)
@app.route("/set_semester", methods=["GET"])
def set_semester():
global activeSemester_id
activeSemester_id = int(request.args.get("semester_id"))
page = "index"
return render_template(page + ".html", navbarItems=navbarItems(page))
@app.route("/semesters")
def semesters():
headerAndDataList = [["Label", "row.label"],
["Parts", "row.parts"]]
table = makeTable(headerAndDataList=headerAndDataList,
rows=Semester.query.all())
page = "semesters"
return render_template(page + ".html", navbarItems=navbarItems(page),
table=table)
@app.route("/register", methods=["GET", "POST"])
def register():
form = RegistrationForm()
if form.validate_on_submit():
password = randomPassword()
passwordHash = generate_password_hash(password).decode("utf-8")
email = form.email.data
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)