mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-20 23:41:20 +00:00
Added persistent semester filters
This commit is contained in:
parent
b8da4fa8a8
commit
c2917aa3fe
4 changed files with 41 additions and 4 deletions
|
@ -4,6 +4,7 @@ from flask_admin.menu import MenuLink
|
||||||
from flask_security import current_user, hash_password
|
from flask_security import current_user, hash_password
|
||||||
from wtforms import BooleanField, SelectField, TextField
|
from wtforms import BooleanField, SelectField, TextField
|
||||||
from wtforms.validators import DataRequired, Email
|
from wtforms.validators import DataRequired, Email
|
||||||
|
from sqlalchemy import func
|
||||||
|
|
||||||
from advlabdb import admin, app, db, user_datastore
|
from advlabdb import admin, app, db, user_datastore
|
||||||
from advlabdb.configUtils import getConfig
|
from advlabdb.configUtils import getConfig
|
||||||
|
@ -146,6 +147,16 @@ class PartModelView(SecureModelView):
|
||||||
partLabels = getConfig("partLabels")
|
partLabels = getConfig("partLabels")
|
||||||
form_choices = {"label": list(zip(partLabels, partLabels))}
|
form_choices = {"label": list(zip(partLabels, partLabels))}
|
||||||
|
|
||||||
|
def get_query(self):
|
||||||
|
return super().get_query().filter(Part.id.in_([part.id for part in userActiveSemester().parts]))
|
||||||
|
|
||||||
|
def get_count_query(self):
|
||||||
|
return (
|
||||||
|
self.session.query(func.count("*"))
|
||||||
|
.select_from(self.model)
|
||||||
|
.filter(Part.id.in_([part.id for part in userActiveSemester().parts]))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class StudentModelView(SecureModelView):
|
class StudentModelView(SecureModelView):
|
||||||
can_view_details = True
|
can_view_details = True
|
||||||
|
@ -171,24 +182,30 @@ class StudentModelView(SecureModelView):
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
partLabel = form.new_part_student_part.data
|
partLabel = form.new_part_student_part.data
|
||||||
groupNumber = form.new_part_student_group_number.data
|
groupNumber = form.new_part_student_group_number.data
|
||||||
|
|
||||||
if (partLabel != self.partChoices[0] and groupNumber == "") or (
|
if (partLabel != self.partChoices[0] and groupNumber == "") or (
|
||||||
partLabel == self.partChoices[0] and groupNumber != ""
|
partLabel == self.partChoices[0] and groupNumber != ""
|
||||||
):
|
):
|
||||||
flash("You have to assign both part and group if you want to add a part student!", "danger")
|
flash("You have to assign both part and group if you want to add a part student!", "danger")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if partLabel != self.partChoices[0] and not partFromLabelInUserActiveSemester(partLabel):
|
if partLabel != self.partChoices[0] and not partFromLabelInUserActiveSemester(partLabel):
|
||||||
flash(f"Part {partLabel} is not created in {str(userActiveSemester())} yet!", "danger")
|
flash(f"Part {partLabel} is not created in {str(userActiveSemester())} yet!", "danger")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if groupNumber != "":
|
if groupNumber != "":
|
||||||
message = "The group number has to be an integer > 0 !"
|
message = "The group number has to be an integer > 0 !"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
groupNumber = int(groupNumber)
|
groupNumber = int(groupNumber)
|
||||||
except:
|
except Exception:
|
||||||
flash(message, "danger")
|
flash(message, "danger")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if groupNumber < 1:
|
if groupNumber < 1:
|
||||||
flash(message, "danger")
|
flash(message, "danger")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return super().validate_form(form)
|
return super().validate_form(form)
|
||||||
|
|
||||||
def after_model_change(self, form, model, is_created):
|
def after_model_change(self, form, model, is_created):
|
||||||
|
@ -246,6 +263,16 @@ class PartStudentModelView(SecureModelView):
|
||||||
|
|
||||||
form_excluded_columns = ["experiment_marks"]
|
form_excluded_columns = ["experiment_marks"]
|
||||||
|
|
||||||
|
def get_query(self):
|
||||||
|
return super().get_query().filter(PartStudent.part_id.in_([part.id for part in userActiveSemester().parts]))
|
||||||
|
|
||||||
|
def get_count_query(self):
|
||||||
|
return (
|
||||||
|
self.session.query(func.count("*"))
|
||||||
|
.select_from(self.model)
|
||||||
|
.filter(PartStudent.part_id.in_([part.id for part in userActiveSemester().parts]))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class GroupModelView(SecureModelView):
|
class GroupModelView(SecureModelView):
|
||||||
partLabels = getConfig("partLabels")
|
partLabels = getConfig("partLabels")
|
||||||
|
@ -264,6 +291,16 @@ class GroupModelView(SecureModelView):
|
||||||
return False
|
return False
|
||||||
return super().validate_form(form)
|
return super().validate_form(form)
|
||||||
|
|
||||||
|
def get_query(self):
|
||||||
|
return super().get_query().filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))
|
||||||
|
|
||||||
|
def get_count_query(self):
|
||||||
|
return (
|
||||||
|
self.session.query(func.count("*"))
|
||||||
|
.select_from(self.model)
|
||||||
|
.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
admin.add_view(StudentModelView(Student, db.session))
|
admin.add_view(StudentModelView(Student, db.session))
|
||||||
admin.add_view(PartStudentModelView(PartStudent, db.session))
|
admin.add_view(PartStudentModelView(PartStudent, db.session))
|
||||||
|
|
|
@ -7,6 +7,7 @@ https://flask-sqlalchemy.palletsprojects.com/en/2.x/models/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Imports
|
# Imports
|
||||||
|
from flask import flash
|
||||||
from flask_security.models.fsqla_v2 import FsRoleMixin, FsUserMixin
|
from flask_security.models.fsqla_v2 import FsRoleMixin, FsUserMixin
|
||||||
|
|
||||||
# Importing the database instance
|
# Importing the database instance
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from flask import flash, redirect, render_template, request, url_for
|
from flask import redirect, render_template, request, url_for
|
||||||
from flask_security import auth_required, current_user
|
from flask_security import auth_required, current_user
|
||||||
|
|
||||||
from advlabdb import app, db
|
from advlabdb import app
|
||||||
from advlabdb.models import Semester
|
from advlabdb.models import Semester
|
||||||
from advlabdb.utils import setActiveSemester, titleToTemplate, userActiveSemester
|
from advlabdb.utils import setActiveSemester, titleToTemplate, userActiveSemester
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,6 @@ def partFromLabelInUserActiveSemester(partLabel):
|
||||||
def setActiveSemester(semesterId):
|
def setActiveSemester(semesterId):
|
||||||
try:
|
try:
|
||||||
current_user.active_semester_id = Semester.query.get(int(semesterId)).id
|
current_user.active_semester_id = Semester.query.get(int(semesterId)).id
|
||||||
print(Semester.query.get(int(semesterId)))
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
flash(str(err))
|
flash(str(err))
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Reference in a new issue