2021-04-03 00:11:26 +00:00
|
|
|
from advlabdb import app, user_datastore
|
2021-04-16 16:14:22 +00:00
|
|
|
from flask import render_template, request, url_for, flash, redirect, session
|
2021-04-16 12:21:53 +00:00
|
|
|
from flask_security import auth_required, roles_required, hash_password, current_user
|
2021-04-03 00:11:26 +00:00
|
|
|
|
2021-03-18 22:20:59 +00:00
|
|
|
from advlabdb.utils import *
|
2021-03-19 12:41:52 +00:00
|
|
|
from advlabdb.models import *
|
2021-03-31 23:04:21 +00:00
|
|
|
from advlabdb.forms import *
|
2021-03-18 13:53:55 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-20 14:41:22 +00:00
|
|
|
@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
|
2021-04-01 12:08:42 +00:00
|
|
|
|
2021-04-16 16:14:22 +00:00
|
|
|
def activeSemesterLabel():
|
|
|
|
lastSemesterId = Semester.query.all()[-1].id
|
|
|
|
if "activeSemesterId" not in session:
|
|
|
|
session["activeSemesterId"] = lastSemesterId
|
|
|
|
elif session["activeSemesterId"] != lastSemesterId:
|
|
|
|
flash(f"Warning: You are in the old semester {Semester.query.get(session['activeSemesterId']).label}!",
|
|
|
|
"danger")
|
|
|
|
return Semester.query.get(session["activeSemesterId"]).label
|
|
|
|
|
2021-04-01 12:08:42 +00:00
|
|
|
def navbarItems(title):
|
2021-04-16 12:21:53 +00:00
|
|
|
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"]
|
|
|
|
|
2021-04-01 12:08:42 +00:00
|
|
|
items = []
|
2021-04-16 12:21:53 +00:00
|
|
|
for page in pages:
|
2021-04-01 12:08:42 +00:00
|
|
|
if page == title:
|
|
|
|
active = " active"
|
|
|
|
else:
|
|
|
|
active = ""
|
|
|
|
|
|
|
|
items.append('<a class="nav-link' + active + '" href="' + titleToTemplate(page) + '">' + page + '</a>')
|
|
|
|
return items
|
|
|
|
|
2021-03-20 14:41:22 +00:00
|
|
|
return dict(semesterDropDownItems=semesterDropDownItems,
|
2021-04-16 16:14:22 +00:00
|
|
|
activeSemesterLabel=activeSemesterLabel,
|
2021-04-16 12:21:53 +00:00
|
|
|
navbarItems=navbarItems,
|
|
|
|
current_user=current_user,
|
|
|
|
)
|
2021-03-20 14:41:22 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-18 13:53:55 +00:00
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Index"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
)
|
2021-03-18 22:20:59 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-18 22:20:59 +00:00
|
|
|
@app.route("/students")
|
2021-04-03 00:11:26 +00:00
|
|
|
@roles_required("admin")
|
2021-03-18 22:20:59 +00:00
|
|
|
def students():
|
2021-04-16 16:14:22 +00:00
|
|
|
semester = Semester.query.get(session["activeSemesterId"])
|
2021-03-19 17:09:07 +00:00
|
|
|
parts = semester.parts
|
2021-03-19 12:41:52 +00:00
|
|
|
tables = []
|
|
|
|
tablesLabels = []
|
|
|
|
|
2021-03-19 15:30:02 +00:00
|
|
|
headerAndDataList = [["Student number", "row.student.student_number"],
|
|
|
|
["First name", "row.student.first_name"],
|
|
|
|
["Last name", "row.student.last_name"],
|
|
|
|
["Email", "row.student.email"],
|
2021-03-19 20:15:43 +00:00
|
|
|
["GN", "row.group.number"],
|
2021-03-19 15:30:02 +00:00
|
|
|
["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"],
|
2021-03-19 20:15:43 +00:00
|
|
|
["Experiemt marks", "row.experiment_marks"],
|
|
|
|
["Parts", "[ps.part for ps in row.student.part_students]"]]
|
2021-03-19 12:41:52 +00:00
|
|
|
|
2021-03-19 15:30:02 +00:00
|
|
|
for part in parts:
|
|
|
|
tablesLabels.append("Part " + part.label + ":")
|
|
|
|
tables.append(makeTable(headerAndDataList=headerAndDataList,
|
|
|
|
rows=part.part_students))
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Students"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
tables=tables,
|
|
|
|
tablesLabels=tablesLabels,
|
|
|
|
)
|
2021-03-18 22:20:59 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-18 22:20:59 +00:00
|
|
|
@app.route("/assistants")
|
2021-04-03 00:11:26 +00:00
|
|
|
@roles_required("admin")
|
2021-03-18 22:20:59 +00:00
|
|
|
def assistants():
|
2021-03-19 17:09:07 +00:00
|
|
|
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())
|
|
|
|
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Assistants"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
table=table,
|
|
|
|
)
|
2021-03-18 22:20:59 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-18 22:20:59 +00:00
|
|
|
@app.route("/experiments")
|
2021-04-03 00:11:26 +00:00
|
|
|
@roles_required("admin")
|
2021-03-18 22:20:59 +00:00
|
|
|
def experiments():
|
2021-04-16 16:14:22 +00:00
|
|
|
semester = Semester.query.get(session["activeSemesterId"])
|
2021-03-19 20:15:43 +00:00
|
|
|
parts = semester.parts
|
|
|
|
tables = []
|
|
|
|
tablesLabels = []
|
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
headerAndDataList = [["Number", "row.experiment.number"],
|
|
|
|
["Name", "row.experiment.name"],
|
2021-03-19 20:15:43 +00:00
|
|
|
["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))
|
|
|
|
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Experiments"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
tables=tables,
|
|
|
|
tablesLabels=tablesLabels,
|
|
|
|
)
|
2021-03-18 22:20:59 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-18 22:20:59 +00:00
|
|
|
@app.route("/appointments")
|
2021-04-03 00:11:26 +00:00
|
|
|
@roles_required("admin")
|
2021-03-18 22:20:59 +00:00
|
|
|
def appointments():
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Appointments"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
)
|
2021-03-18 22:20:59 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-18 22:20:59 +00:00
|
|
|
@app.route("/groups")
|
2021-04-03 00:11:26 +00:00
|
|
|
@roles_required("admin")
|
2021-03-18 22:20:59 +00:00
|
|
|
def groups():
|
2021-04-16 16:14:22 +00:00
|
|
|
semester = Semester.query.get(session["activeSemesterId"])
|
2021-03-19 20:15:43 +00:00
|
|
|
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)",
|
2021-03-31 18:43:54 +00:00
|
|
|
"[str(gx.part_experiment.experiment.number) + ' (' + str([appointmentDate(a.date) for a in gx.appointments]) + ')' for gx in row.group_experiments]"]]
|
2021-03-19 20:15:43 +00:00
|
|
|
|
|
|
|
for part in parts:
|
|
|
|
tablesLabels.append("Part " + part.label + ":")
|
|
|
|
tables.append(makeTable(headerAndDataList=headerAndDataList,
|
|
|
|
rows=part.groups))
|
|
|
|
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Groups"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
tables=tables,
|
|
|
|
tablesLabels=tablesLabels,
|
|
|
|
)
|
2021-03-18 22:20:59 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-18 22:20:59 +00:00
|
|
|
@app.route("/users")
|
2021-04-03 00:11:26 +00:00
|
|
|
@roles_required("admin")
|
2021-03-18 22:20:59 +00:00
|
|
|
def users():
|
2021-03-19 20:40:40 +00:00
|
|
|
headerAndDataList = [["Email", "row.email"],
|
2021-04-03 00:11:26 +00:00
|
|
|
["Roles", "[role.name for role in row.roles]"],
|
2021-03-19 20:40:40 +00:00
|
|
|
["Assistant", "row.assistant"]]
|
|
|
|
|
|
|
|
table = makeTable(headerAndDataList=headerAndDataList,
|
|
|
|
rows=User.query.all())
|
|
|
|
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Users"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
table=table,
|
|
|
|
)
|
2021-03-20 14:41:22 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-20 14:41:22 +00:00
|
|
|
@app.route("/set_semester", methods=["GET"])
|
2021-04-03 00:11:26 +00:00
|
|
|
@auth_required()
|
2021-03-20 14:41:22 +00:00
|
|
|
def set_semester():
|
2021-04-16 16:14:22 +00:00
|
|
|
session["activeSemesterId"] = int(request.args.get("semester_id"))
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Index"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
)
|
2021-03-20 14:41:22 +00:00
|
|
|
|
2021-03-31 23:04:21 +00:00
|
|
|
|
2021-03-20 14:41:22 +00:00
|
|
|
@app.route("/semesters")
|
2021-04-03 00:11:26 +00:00
|
|
|
@roles_required("admin")
|
2021-03-20 14:41:22 +00:00
|
|
|
def semesters():
|
|
|
|
headerAndDataList = [["Label", "row.label"],
|
|
|
|
["Parts", "row.parts"]]
|
|
|
|
|
|
|
|
table = makeTable(headerAndDataList=headerAndDataList,
|
|
|
|
rows=Semester.query.all())
|
|
|
|
|
2021-04-01 12:08:42 +00:00
|
|
|
title = "Semesters"
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template(titleToTemplate(title) + ".html",
|
|
|
|
title=title,
|
|
|
|
table=table,
|
|
|
|
)
|
2021-03-31 23:04:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/register", methods=["GET", "POST"])
|
|
|
|
def register():
|
|
|
|
form = RegistrationForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
password = randomPassword()
|
2021-04-03 00:11:26 +00:00
|
|
|
passwordHash = hash_password(password)
|
2021-04-01 12:08:42 +00:00
|
|
|
|
2021-04-01 22:33:50 +00:00
|
|
|
email = form.email.data.lower()
|
2021-03-31 23:04:21 +00:00
|
|
|
admin = form.admin.data
|
2021-04-01 12:08:42 +00:00
|
|
|
|
2021-04-03 00:11:26 +00:00
|
|
|
roles = ["assistant"] if not admin else ["admin"]
|
|
|
|
|
|
|
|
user_datastore.create_user(email=email, password=passwordHash, roles=roles)
|
2021-03-31 23:04:21 +00:00
|
|
|
db.session.commit()
|
2021-04-16 12:21:53 +00:00
|
|
|
return render_template("registered.html",
|
|
|
|
title="Registered",
|
|
|
|
email=email,
|
|
|
|
password=password,
|
|
|
|
admin=admin,
|
|
|
|
)
|
|
|
|
return render_template("register.html",
|
|
|
|
title="Register",
|
|
|
|
form=form,
|
|
|
|
)
|