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

Add part student in StudentModelView

This commit is contained in:
Mo 2021-04-27 00:26:11 +02:00
parent 48fda40c2c
commit 07dec709f7

View file

@ -1,12 +1,12 @@
from flask import url_for, render_template, flash
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
from wtforms import BooleanField
from wtforms import BooleanField, SelectField, TextField
from advlabdb import admin, app, user_datastore, db
from advlabdb.models import *
from advlabdb.utils import randomPassword
from advlabdb.utils import randomPassword, userActiveSemester
partsLabels = ["A/1", "A/2", "B/1", "B/2"]
@ -50,8 +50,8 @@ class SemesterModelView(SecureModelView):
def after_model_change(self, form, model, is_created):
if is_created and form.create_parts.data:
for part in self.parts:
db.session.add(Part(label=part, semester=model))
for partLabel in partsLabels:
db.session.add(Part(label=partLabel, semester=model))
db.session.commit()
@ -64,9 +64,56 @@ class PartModelView(SecureModelView):
class StudentModelView(SecureModelView):
can_view_details = True
column_list = ["student_number", "first_name", "last_name", "email", "part_students"]
column_details_list = column_list + ["bachelor_thesis", "bachelor_thesis_work_group"]
column_details_list = column_list + ["bachelor_thesis", "bachelor_thesis_work_group", "note"]
column_searchable_list = ["student_number", "email", "first_name", "last_name"]
form_columns = column_list
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))