1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Polish filters

This commit is contained in:
Mo 2022-03-02 01:56:42 +01:00
parent 978dfd340f
commit 7b09842932
3 changed files with 194 additions and 64 deletions

View file

@ -1,6 +1,6 @@
from pathlib import Path from pathlib import Path
from flask import flash, redirect, request, url_for from flask import flash, redirect, request, url_for, has_request_context
from flask_admin import expose from flask_admin import expose
from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField
from flask_admin.contrib.sqla.filters import FilterEqual from flask_admin.contrib.sqla.filters import FilterEqual
@ -134,10 +134,10 @@ class UserView(SecureAdminModelView):
"last_name", "last_name",
"email", "email",
] ]
column_filters = [ column_filters = (
"active", "active",
"active_semester", "active_semester",
] )
column_editable_list = [ column_editable_list = [
"active", "active",
] ]
@ -292,14 +292,18 @@ class SemesterView(SecureAdminModelView):
) )
def addMenuLink(space, newSemester): def addMenuLink(space, newSemester):
space.add_link( categoryText = "Active semester"
MenuLink( link = MenuLink(
name=newSemester.repr(), name=newSemester.repr(),
url=url_for("set_semester") + "?semester_id=" + str(newSemester.id), url=url_for("set_semester") + "?semester_id=" + str(newSemester.id),
category="Active semester", category=categoryText,
)
) )
category = self._menu_categories.get(categoryText)
link.parent = category
category._children.insert(0, link)
def after_model_change(self, form, model, is_created): def after_model_change(self, form, model, is_created):
setUserActiveSemester(model.id) setUserActiveSemester(model.id)
@ -398,7 +402,28 @@ def groupQueryFactory():
return Group.query.filter(Group.semester == userActiveSemester()) return Group.query.filter(Group.semester == userActiveSemester())
def partFilterOptions():
if has_request_context():
parts = Part.query.filter(Part.semester == userActiveSemester())
return ((part.id, f"{part.program.repr()}{part.number}") for part in parts)
else:
return
class PartStudentView(SecureAdminModelView): class PartStudentView(SecureAdminModelView):
@expose("/")
def index_view(self):
# To update filter options
self._refresh_filters_cache()
return super().index_view()
class PartFilter(FilterEqual):
def get_options(self, view):
return partFilterOptions()
def apply(self, query, value, alias=None):
return query.filter(self.column.part_id == int(value))
class CreateForm(Form): class CreateForm(Form):
def studentQueryFactory(): def studentQueryFactory():
return Student.query return Student.query
@ -438,11 +463,14 @@ class PartStudentView(SecureAdminModelView):
"experiment_marks", "experiment_marks",
] ]
column_filters = [ column_filters = (
"part", PartFilter(PartStudent, "Part"),
"student", "student.student_number",
"group", "student.first_name",
] "student.last_name",
"group.number",
"experiment_marks",
)
def queryFilter(self): def queryFilter(self):
return PartStudent.part.has(Part.semester == userActiveSemester()) return PartStudent.part.has(Part.semester == userActiveSemester())
@ -460,6 +488,20 @@ def partStudentQueryFactory():
class GroupView(SecureAdminModelView): class GroupView(SecureAdminModelView):
@expose("/")
def index_view(self):
# To update filter options
self._refresh_filters_cache()
return super().index_view()
class ProgramFilter(FilterEqual):
def get_options(self, view):
programs = Program.query
return ((program.id, program.repr()) for program in programs)
def apply(self, query, value, alias=None):
return query.filter(self.column.program_id == int(value))
def formFactory(is_created, group): def formFactory(is_created, group):
if is_created: if is_created:
@ -488,15 +530,14 @@ class GroupView(SecureAdminModelView):
column_list = [ column_list = [
"number", "number",
"semester",
"program", "program",
"part_students", "part_students",
"group_experiments", "group_experiments",
] ]
column_filters = [ column_filters = (
ProgramFilter(Group, "Program"),
"number", "number",
"program", )
]
def queryFilter(self): def queryFilter(self):
return Group.semester == userActiveSemester() return Group.semester == userActiveSemester()
@ -516,9 +557,7 @@ class GroupView(SecureAdminModelView):
class ExperimentView(SecureAdminModelView): class ExperimentView(SecureAdminModelView):
can_view_details = True can_view_details = True
column_filters = [ column_filters = ("active",)
"active",
]
column_list = [ column_list = [
"number", "number",
"program", "program",
@ -611,9 +650,7 @@ class SemesterExperimentView(SecureAdminModelView):
"final_weighting", "final_weighting",
"group_experiments", "group_experiments",
] ]
column_filters = [ column_filters = ("experiment",)
"experiment",
]
def queryFilter(self): def queryFilter(self):
return SemesterExperiment.semester == userActiveSemester() return SemesterExperiment.semester == userActiveSemester()
@ -657,9 +694,7 @@ class AssistantView(SecureAdminModelView):
"user.last_name", "user.last_name",
"user.email", "user.email",
] ]
column_filters = [ column_filters = ("user.active",)
"user.active",
]
form_excluded_columns = [ form_excluded_columns = [
"experiment_marks", "experiment_marks",
"appointments", "appointments",
@ -688,6 +723,17 @@ class AssistantView(SecureAdminModelView):
assistantBlankText = "Auto assign if experiment has only one assistant" assistantBlankText = "Auto assign if experiment has only one assistant"
def experimentFilterOptions():
activeExperiments = Experiment.query.filter(Experiment.active == True)
return (
(
f"{activeExperiment.number},{activeExperiment.program.id}",
f"{activeExperiment.number} {activeExperiment.program.repr()}",
)
for activeExperiment in activeExperiments
)
class GroupExperimentView(SecureAdminModelView): class GroupExperimentView(SecureAdminModelView):
@expose("/") @expose("/")
def index_view(self): def index_view(self):
@ -697,14 +743,7 @@ class GroupExperimentView(SecureAdminModelView):
class ExperimentFilter(FilterEqual): class ExperimentFilter(FilterEqual):
def get_options(self, view): def get_options(self, view):
activeExperiments = Experiment.query.filter(Experiment.active == True).all() return experimentFilterOptions()
return (
(
f"{activeExperiment.number},{activeExperiment.program.id}",
f"{activeExperiment.number} {activeExperiment.program.repr()}",
)
for activeExperiment in activeExperiments
)
def apply(self, query, value, alias=None): def apply(self, query, value, alias=None):
values = value.split(",") values = value.split(",")
@ -770,11 +809,12 @@ class GroupExperimentView(SecureAdminModelView):
"appointments", "appointments",
"experiment_marks", "experiment_marks",
] ]
column_filters = [ column_filters = (
"group",
ExperimentFilter(GroupExperiment, "Experiment"), ExperimentFilter(GroupExperiment, "Experiment"),
"group.number",
"appointments", "appointments",
] "experiment_marks",
)
def queryFilter(self): def queryFilter(self):
return GroupExperiment.group.has(Group.semester == userActiveSemester()) return GroupExperiment.group.has(Group.semester == userActiveSemester())
@ -808,7 +848,47 @@ def groupExperimentQueryFactory():
) )
def assistantFilterOptions():
assistants = Assistant.query.filter(Assistant.user.has(User.active == True))
return ((assistant.id, assistant.repr()) for assistant in assistants)
class AppointmentView(SecureAdminModelView): class AppointmentView(SecureAdminModelView):
@expose("/")
def index_view(self):
# To update filter options
self._refresh_filters_cache()
return super().index_view()
class ExperimentFilter(FilterEqual):
def get_options(self, view):
return experimentFilterOptions()
def apply(self, query, value, alias=None):
values = value.split(",")
experimentNumber = int(values[0])
programId = int(values[1])
return query.filter(
self.column.group_experiment.has(
GroupExperiment.semester_experiment.has(
SemesterExperiment.experiment.has(Experiment.program_id == programId)
)
),
self.column.group_experiment.has(
GroupExperiment.semester_experiment.has(
SemesterExperiment.experiment.has(Experiment.number == experimentNumber)
)
),
)
class AssistantFilter(FilterEqual):
def get_options(self, view):
return assistantFilterOptions()
def apply(self, query, value, alias=None):
return query.filter(self.column.assistant_id == int(value))
class CreateForm(Form): class CreateForm(Form):
group_experiment = QuerySelectField( group_experiment = QuerySelectField(
"Group Experiment", "Group Experiment",
@ -828,13 +908,13 @@ class AppointmentView(SecureAdminModelView):
form = CreateForm form = CreateForm
column_filters = [ column_filters = (
ExperimentFilter(Appointment, "Experiment"),
AssistantFilter(Appointment, "Assistant"),
"group_experiment.group",
"date", "date",
"special", "special",
"group_experiment.group", )
"group_experiment.semester_experiment.experiment",
"assistant.user",
]
column_editable_list = [ column_editable_list = [
"date", "date",
"special", "special",
@ -886,21 +966,72 @@ class ExperimentMarkView(SecureAdminModelView):
else: else:
return False return False
def apply(self, query, value, alias=None):
return query.filter(self.column.part_student.has(PartStudent.student_id == int(value)))
class AssistantFilter(FilterEqual): class AssistantFilter(FilterEqual):
def get_options(self, view): def get_options(self, view):
assistants = Assistant.query.filter(Assistant.user.has(User.active == True)).all() return assistantFilterOptions()
return ((assistant.id, assistant.repr()) for assistant in assistants)
def apply(self, query, value, alias=None): def apply(self, query, value, alias=None):
return query.filter(self.column == int(value)) return query.filter(self.column.assistant_id == int(value))
class AdminFilter(FilterEqual): class AdminFilter(FilterEqual):
def get_options(self, view): def get_options(self, view):
admins = Admin.query.filter(Admin.user.has(User.active == True)).all() admins = Admin.query.filter(Admin.user.has(User.active == True))
return ((admin.id, admin.repr()) for admin in admins) return ((admin.id, admin.repr()) for admin in admins)
def apply(self, query, value, alias=None): def apply(self, query, value, alias=None):
return query.filter(self.column == int(value)) return query.filter(self.column.admin_id == int(value))
class ExperimentFilter(FilterEqual):
def get_options(self, view):
return experimentFilterOptions()
def apply(self, query, value, alias=None):
values = value.split(",")
experimentNumber = int(values[0])
programId = int(values[1])
return query.filter(
self.column.group_experiment.has(
GroupExperiment.semester_experiment.has(
SemesterExperiment.experiment.has(Experiment.program_id == programId)
)
),
self.column.group_experiment.has(
GroupExperiment.semester_experiment.has(
SemesterExperiment.experiment.has(Experiment.number == experimentNumber)
)
),
)
class ProgramFilter(FilterEqual):
def get_options(self, view):
programs = Program.query
return ((program.id, program.repr()) for program in programs)
def apply(self, query, value, alias=None):
return query.filter(self.column.part_student.has(PartStudent.part.has(Part.program_id == int(value))))
class PartFilter(FilterEqual):
def get_options(self, view):
return partFilterOptions()
def apply(self, query, value, alias=None):
return query.filter(self.column.part_student.has(PartStudent.part_id == int(value)))
class SemesterFilter(FilterEqual):
def get_options(self, view):
semesters = Semester.query.order_by(Semester.id.desc())
return ((semester.id, semester.repr()) for semester in semesters)
def apply(self, query, value, alias=None):
return query.filter(
self.column.group_experiment.has(
GroupExperiment.semester_experiment.has(SemesterExperiment.semester_id == int(value))
)
)
class CreateForm(Form): class CreateForm(Form):
part_student = QuerySelectField( part_student = QuerySelectField(
@ -940,20 +1071,19 @@ class ExperimentMarkView(SecureAdminModelView):
"admin": "The last admin who edited the mark", "admin": "The last admin who edited the mark",
} }
column_filters = [ column_filters = (
StudentFilter(PartStudent.id, "Student / ID"), StudentFilter(ExperimentMark, "Student / ID"),
AssistantFilter(ExperimentMark.assistant_id, "Assistant"), SemesterFilter(ExperimentMark, "Semester"),
AdminFilter(ExperimentMark.admin_id, "Admin"), AssistantFilter(ExperimentMark, "Assistant"),
"part_student.part.program", AdminFilter(ExperimentMark, "Admin"),
"part_student.part", ExperimentFilter(ExperimentMark, "Experiment"),
"part_student.student", ProgramFilter(ExperimentMark, "Program"),
PartFilter(ExperimentMark, "Part"),
"group_experiment.group", "group_experiment.group",
"group_experiment.semester_experiment.semester",
"group_experiment.semester_experiment.experiment",
"oral_mark", "oral_mark",
"protocol_mark", "protocol_mark",
"final_experiment_mark", "final_experiment_mark",
] )
column_default_sort = [("oral_mark", False), ("protocol_mark", False)] column_default_sort = [("oral_mark", False), ("protocol_mark", False)]
""" """

View file

@ -56,7 +56,7 @@ def setUserActiveSemester(semesterId):
def initActiveSemesterMenuLinks(space): def initActiveSemesterMenuLinks(space):
with app.app_context(): with app.app_context():
semesters = Semester.query.order_by(Semester.id) semesters = Semester.query.order_by(Semester.id.desc())
for semester in semesters: for semester in semesters:
space.add_link( space.add_link(
MenuLink( MenuLink(

View file

@ -95,13 +95,13 @@ with app.app_context():
db.session.add(gx1) db.session.add(gx1)
db.session.add(gx2) db.session.add(gx2)
user_datastore.create_role(name="admin") adminRole = user_datastore.create_role(name="admin")
user_datastore.create_role(name="assistant") assistantRole = user_datastore.create_role(name="assistant")
admin_user = user_datastore.create_user( admin_user = user_datastore.create_user(
email="admin@advlabdb.de", email="admin@advlabdb.de",
password=hash_password("admin"), password=hash_password("admin"),
roles=["admin"], roles=[adminRole],
first_name="Peter", first_name="Peter",
last_name="Blümler", last_name="Blümler",
) )
@ -113,7 +113,7 @@ with app.app_context():
us1 = user_datastore.create_user( us1 = user_datastore.create_user(
email="test@protonmail.com", email="test@protonmail.com",
password=hash_password("h1"), password=hash_password("h1"),
roles=["assistant"], roles=[assistantRole],
first_name="As1", first_name="As1",
last_name="l", last_name="l",
phone_number="012333212", phone_number="012333212",
@ -122,7 +122,7 @@ with app.app_context():
us2 = user_datastore.create_user( us2 = user_datastore.create_user(
email="test2@protonmail.com", email="test2@protonmail.com",
password=hash_password("h2"), password=hash_password("h2"),
roles=["assistant"], roles=[assistantRole],
first_name="As2", first_name="As2",
last_name="l", last_name="l",
) )