mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-20 23:41:20 +00:00
Added PartFilter for PartStudent, fixed set_semester
This commit is contained in:
parent
a5a63166b7
commit
c9023bdfc9
3 changed files with 36 additions and 6 deletions
|
@ -1,12 +1,13 @@
|
|||
from flask import url_for, render_template, flash, request
|
||||
from advlabdb.customClasses import SecureModelView
|
||||
from flask_admin.menu import MenuLink
|
||||
from flask_admin.contrib.sqla.filters import BaseSQLAFilter
|
||||
from flask_security import hash_password
|
||||
from wtforms import BooleanField, SelectField, TextField
|
||||
|
||||
from advlabdb import admin, app, user_datastore, db
|
||||
from advlabdb.models import *
|
||||
from advlabdb.utils import randomPassword, userActiveSemester
|
||||
from advlabdb.utils import randomPassword, userActiveSemester, partFromLabelInUserActiveSemester
|
||||
|
||||
|
||||
partsLabels = ["A/1", "A/2", "B/1", "B/2"]
|
||||
|
@ -61,6 +62,21 @@ class PartModelView(SecureModelView):
|
|||
form_columns = ["label", "semester"]
|
||||
|
||||
|
||||
class PartFilter(BaseSQLAFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
return query.filter(self.column == partFromLabelInUserActiveSemester(value).id)
|
||||
|
||||
def operation(self):
|
||||
return "equals"
|
||||
|
||||
def validate(self, value):
|
||||
if partFromLabelInUserActiveSemester(value):
|
||||
return True
|
||||
else:
|
||||
flash(f"Part {value} not found in your active semester {userActiveSemester()}!", "danger")
|
||||
return False
|
||||
|
||||
|
||||
class StudentModelView(SecureModelView):
|
||||
can_view_details = True
|
||||
column_list = ["student_number", "first_name", "last_name", "email", "part_students"]
|
||||
|
@ -81,7 +97,7 @@ class StudentModelView(SecureModelView):
|
|||
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:
|
||||
if partLabel != self.partChoices[0] and not partFromLabelInUserActiveSemester(partLabel):
|
||||
flash(f"Part {partLabel} is not created in {str(userActiveSemester())} yet!", "danger")
|
||||
return False
|
||||
if groupNumber != "":
|
||||
|
@ -102,7 +118,7 @@ class StudentModelView(SecureModelView):
|
|||
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()
|
||||
part = partFromLabelInUserActiveSemester(partLabel)
|
||||
group = Group.query.filter(Group.number == groupNumber, Group.part == part).first()
|
||||
|
||||
if group is None:
|
||||
|
@ -116,8 +132,11 @@ class StudentModelView(SecureModelView):
|
|||
flash("Added part student.", "success")
|
||||
|
||||
|
||||
class PartStudentModelView(SecureModelView):
|
||||
column_filters = [PartFilter(PartStudent.part_id, "Part", options=list(zip(partsLabels, partsLabels)))]
|
||||
|
||||
admin.add_view(StudentModelView(Student, db.session))
|
||||
admin.add_view(SecureModelView(PartStudent, db.session))
|
||||
admin.add_view(PartStudentModelView(PartStudent, db.session))
|
||||
admin.add_view(SecureModelView(Group, db.session))
|
||||
admin.add_view(SecureModelView(GroupExperiment, db.session))
|
||||
admin.add_view(SecureModelView(Experiment, db.session))
|
||||
|
|
|
@ -163,4 +163,5 @@ def deactivate_users():
|
|||
@auth_required()
|
||||
def set_semester():
|
||||
current_user.active_semester_id = int(request.args.get("semester_id"))
|
||||
return redirect(request.referrer)
|
||||
db.session.commit()
|
||||
return redirect(url_for("index"))
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from random import choice
|
||||
from string import digits, ascii_letters
|
||||
from flask_security import current_user
|
||||
from flask import flash
|
||||
|
||||
from advlabdb.models import Semester
|
||||
|
||||
|
@ -74,11 +75,20 @@ def userActiveSemester():
|
|||
lastSemesterId = Semester.query.all()[-1].id
|
||||
if current_user.active_semester_id is None:
|
||||
current_user.active_semester_id = lastSemesterId
|
||||
db.session.commit()
|
||||
elif current_user.active_semester_id != lastSemesterId:
|
||||
activeSemester = Semester.query.get(current_user.active_semester_id)
|
||||
if activeSemester:
|
||||
flash(f"You are in the old semester {activeSemester.label}!", "warning")
|
||||
else:
|
||||
flash(f"Semester changed!", "warning")
|
||||
current_user.active_semester_id = lastSemesterId
|
||||
db.session.commit()
|
||||
flash("Semester changed!", "warning")
|
||||
return Semester.query.get(current_user.active_semester_id)
|
||||
|
||||
|
||||
def partFromLabelInUserActiveSemester(partLabel):
|
||||
for part in userActiveSemester().parts:
|
||||
if part.label == partLabel:
|
||||
return part
|
||||
return False
|
||||
|
|
Loading…
Reference in a new issue