1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-12 21:40:41 +00:00
AdvLabDB/advlabdb/routes.py

181 lines
6 KiB
Python
Raw Normal View History

2021-06-02 21:43:41 +00:00
from flask import flash, redirect, render_template, request, url_for
2021-05-17 20:36:24 +00:00
from flask_security import auth_required, current_user
2021-04-03 00:11:26 +00:00
2021-06-02 21:43:41 +00:00
from advlabdb import app, db
2021-05-17 20:36:24 +00:00
from advlabdb.models import Semester
2021-06-02 21:43:41 +00:00
from advlabdb.utils import setActiveSemester, titleToTemplate, userActiveSemester
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:
2021-06-02 21:43:41 +00:00
items.append(
'<li><a class="dropdown-item" href="'
+ url_for("set_semester")
+ "?semester_id="
+ str(semester.id)
+ '">'
+ semester.label
+ "</a></li>"
)
2021-03-20 14:41:22 +00:00
return items
2021-04-01 12:08:42 +00:00
def navbarItems(title):
2021-05-02 16:11:08 +00:00
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"]
2021-04-01 12:08:42 +00:00
items = []
for page in pages:
2021-04-01 12:08:42 +00:00
if page == title:
active = " active"
else:
active = ""
2021-06-02 21:43:41 +00:00
items.append('<a class="nav-link' + active + '" href="' + titleToTemplate(page) + '">' + page + "</a>")
2021-04-01 12:08:42 +00:00
return items
2021-06-02 21:43:41 +00:00
return dict(
semesterDropDownItems=semesterDropDownItems,
userActiveSemester=userActiveSemester,
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-05-17 20:15:50 +00:00
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")
2021-05-17 20:36:24 +00:00
2021-05-02 16:11:08 +00:00
"""
@app.route("/assistants")
2021-04-03 00:11:26 +00:00
@roles_required("admin")
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"],
2021-05-18 13:20:03 +00:00
["Experiment marks", "row.experiment_marks"]]
2021-03-19 17:09:07 +00:00
table = makeTable(headerAndDataList=headerAndDataList,
rows=Assistant.query.filter(Assistant.user_id != None).all())
return render_template("assistants.html",
table=table,
)
2021-03-31 23:04:21 +00:00
@app.route("/experiments")
2021-04-03 00:11:26 +00:00
@roles_required("admin")
def experiments():
semester = Semester.query.get(current_user.active_semester_id)
parts = semester.parts
tables = []
tablesLabels = []
2021-03-31 23:04:21 +00:00
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))
return render_template("experiments.html",
tables=tables,
tablesLabels=tablesLabels,
)
2021-03-31 23:04:21 +00:00
@app.route("/appointments")
2021-04-03 00:11:26 +00:00
@roles_required("admin")
def appointments():
return render_template("appointments.html",
)
2021-03-31 23:04:21 +00:00
@app.route("/groups")
2021-04-03 00:11:26 +00:00
@roles_required("admin")
def groups():
semester = Semester.query.get(current_user.active_semester_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))
return render_template("groups.html",
tables=tables,
tablesLabels=tablesLabels,
)
2021-03-31 23:04:21 +00:00
@app.route("/deactivate_users", methods=["GET"])
@roles_required("admin")
def deactivate_users():
2021-04-17 15:14:23 +00:00
usersEmails = json.loads(request.args.get("json"))
deactivatedUsersEmails = []
2021-04-17 15:14:23 +00:00
for email in usersEmails:
user = User.query.filter(User.email == email).first()
if user == current_user:
flash("You have tried to deactivate yourself as user!", "danger")
continue
if user_datastore.deactivate_user(user):
deactivatedUsersEmails.append(email)
db.session.commit()
2021-04-17 15:14:23 +00:00
deactivatedUsersEmailsLen = len(deactivatedUsersEmails)
if deactivatedUsersEmailsLen == 0:
flash(f"No users deactivated!", "warning")
else:
2021-04-17 15:14:23 +00:00
flash(f"{deactivatedUsersEmailsLen} Users with emails {deactivatedUsersEmails} deactivated!", "success")
return redirect(url_for("users"))
2021-05-02 16:11:08 +00:00
"""
2021-03-31 23:04:21 +00:00
2021-05-17 20:36:24 +00:00
@app.route("/set_semester", methods=["GET"])
@auth_required()
def set_semester():
setActiveSemester(request.args.get("semester_id"))
ref = request.referrer
red = ref if ref else url_for("index")
return redirect(red)