1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00
AdvLabDB/advlabdb/modelViews.py

135 lines
5.6 KiB
Python
Raw Normal View History

2021-04-26 22:26:11 +00:00
from flask import url_for, render_template, flash, request
from advlabdb.customClasses import SecureModelView
from flask_admin.menu import MenuLink
from flask_security import hash_password
2021-04-26 22:26:11 +00:00
from wtforms import BooleanField, SelectField, TextField
2021-04-24 11:38:03 +00:00
from advlabdb import admin, app, user_datastore, db
from advlabdb.models import *
2021-04-26 22:26:11 +00:00
from advlabdb.utils import randomPassword, userActiveSemester
partsLabels = ["A/1", "A/2", "B/1", "B/2"]
class UserModelView(SecureModelView):
column_list = ["email", "active", "roles", "assistant"]
column_searchable_list = ["email"]
column_filters = ["active"]
form_columns = ["email", "roles"]
def create_model(self, form):
password = randomPassword()
passwordHash = hash_password(password)
email = form.email.data.lower()
print(form.roles.data[0].name)
roles = [role.name for role in form.roles.data]
if "admin" in roles:
flash("You have registered a new admin!", "danger")
2021-04-24 11:38:03 +00:00
model = user_datastore.create_user(email=email, password=passwordHash, roles=roles)
db.session.commit()
flash(f"{email} registered with roles: {', '.join([role.name for role in form.roles.data])}.", category="success")
flash(f"Random password: {password}", category="warning")
2021-04-24 11:38:03 +00:00
return model
class RoleModelView(SecureModelView):
column_exclude_list = ["update_datetime"]
2021-04-24 11:38:03 +00:00
class SemesterModelView(SecureModelView):
column_list = ["label", "parts"]
form_columns = ["label", "create_parts"]
form_extra_fields = {
"create_parts": BooleanField("Create parts:" + ", ".join(partsLabels) + ".", default=True)
2021-04-24 11:38:03 +00:00
}
def after_model_change(self, form, model, is_created):
if is_created and form.create_parts.data:
2021-04-26 22:26:11 +00:00
for partLabel in partsLabels:
db.session.add(Part(label=partLabel, semester=model))
2021-04-24 11:38:03 +00:00
db.session.commit()
class PartModelView(SecureModelView):
can_view_details = True
column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"]
form_columns = ["label", "semester"]
class StudentModelView(SecureModelView):
can_view_details = True
column_list = ["student_number", "first_name", "last_name", "email", "part_students"]
2021-04-26 22:26:11 +00:00
column_details_list = column_list + ["bachelor_thesis", "bachelor_thesis_work_group", "note"]
column_searchable_list = ["student_number", "email", "first_name", "last_name"]
2021-04-26 22:26:11 +00:00
form_columns = column_details_list + ["new_part_student_part", "new_part_student_group_number"]
partChoices = ["-"] + partsLabels
form_extra_fields = {
"new_part_student_part": SelectField("Part", choices=list(zip(partChoices, partChoices)), default=partChoices[0]),
"new_part_student_group_number": TextField("Group number")
}
def validate_form(self, form):
if request.method == "POST":
partLabel = form.new_part_student_part.data
groupNumber = form.new_part_student_group_number.data
if (partLabel != self.partChoices[0] and groupNumber == "") or (partLabel == self.partChoices[0] and groupNumber != ""):
flash("You have to assign both part and group if you want to add a part student!", "danger")
return False
if partLabel != self.partChoices[0] and Part.query.filter(Part.label == partLabel, Part.semester == userActiveSemester()).first() is None:
flash(f"Part {partLabel} is not created in {str(userActiveSemester())} yet!", "danger")
return False
if groupNumber != "":
message = "The group number has to be an integer > 0 !"
try:
groupNumber = int(groupNumber)
except:
flash(message, "danger")
return False
if groupNumber < 1:
flash(message, "danger")
return False
return super().validate_form(form)
def after_model_change(self, form, model, is_created):
partLabel = form.new_part_student_part.data
print("\nLL\n")
if partLabel != self.partChoices[0]:
groupNumber = int(form.new_part_student_group_number.data)
part = Part.query.filter(Part.label == partLabel, Part.semester == userActiveSemester()).first()
group = Group.query.filter(Group.number == groupNumber, Group.part == part).first()
if group is None:
group = Group(number=groupNumber, part=part)
db.session.add(group)
flash(f"Added the new group with number {str(groupNumber)} in part {str(part)}.", "success")
partStudent = PartStudent(student=model, part=part, group=group)
db.session.add(partStudent)
db.session.commit()
flash("Added part student.", "success")
admin.add_view(StudentModelView(Student, db.session))
admin.add_view(SecureModelView(PartStudent, db.session))
admin.add_view(SecureModelView(Group, db.session))
admin.add_view(SecureModelView(GroupExperiment, db.session))
admin.add_view(SecureModelView(Experiment, db.session))
admin.add_view(SecureModelView(PartExperiment, db.session))
admin.add_view(SecureModelView(Assistant, db.session))
admin.add_view(SecureModelView(Appointment, db.session))
2021-04-24 11:38:03 +00:00
admin.add_view(PartModelView(Part, db.session))
admin.add_view(SemesterModelView(Semester, db.session))
admin.add_view(SecureModelView(ExperimentMark, db.session))
admin.add_view(UserModelView(User, db.session))
admin.add_view(RoleModelView(Role, db.session))
with app.app_context():
admin.add_link(MenuLink(name="Home", url=url_for("index"), category="Links"))