diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index ee22906..c3b73d6 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -74,16 +74,14 @@ def semesterExperimentQueryFactory(): return SemesterExperiment.query.filter(SemesterExperiment.semester == userActiveSemester()) -def semesterFilterOptions(): - semesters = Semester.query.order_by(Semester.id.desc()) - return tuple((semester.id, semester.repr()) for semester in semesters) +class SemesterRowFilter(FilterEqual): + def get_options(self, view): + semesters = Semester.query.order_by(Semester.id.desc()) + return tuple((semester.id, semester.repr()) for semester in semesters) class UserView(SecureAdminModelView): - class SemesterFilter(FilterEqual): - def get_options(self, view): - return semesterFilterOptions() - + class SemesterFilter(SemesterRowFilter): def apply(self, query, value, alias=None): return query.filter(self.column.active_semester_id == int(value)) @@ -435,17 +433,15 @@ def groupQueryFactory(): return Group.query.filter(Group.semester == userActiveSemester()) -def partFilterOptions(): - if has_request_context(): - parts = Part.query.filter(Part.semester == userActiveSemester()) - return tuple((part.id, f"{part.program.repr()}{part.number}") for part in parts) +class PartRowFilter(FilterEqual): + def get_options(self, view): + if has_request_context(): + parts = Part.query.filter(Part.semester == userActiveSemester()) + return tuple((part.id, f"{part.program.repr()}{part.number}") for part in parts) class PartStudentView(SecureAdminModelView): - class PartFilter(FilterEqual): - def get_options(self, view): - return partFilterOptions() - + class PartFilter(PartRowFilter): def apply(self, query, value, alias=None): return query.filter(self.column.part_id == int(value)) @@ -537,16 +533,14 @@ def partStudentQueryFactory(): return PartStudent.query.filter(PartStudent.part.has(Part.semester == userActiveSemester())) -def programFilterOptions(): - programs = Program.query - return tuple((program.id, program.repr()) for program in programs) +class ProgramRowFilter(FilterEqual): + def get_options(self, view): + programs = Program.query + return tuple((program.id, program.repr()) for program in programs) class GroupView(SecureAdminModelView): - class ProgramFilter(FilterEqual): - def get_options(self, view): - return programFilterOptions() - + class ProgramFilter(ProgramRowFilter): def apply(self, query, value, alias=None): return query.filter(self.column.program_id == int(value)) @@ -608,10 +602,7 @@ class GroupView(SecureAdminModelView): class ExperimentView(SecureAdminModelView): - class ProgramFilter(FilterEqual): - def get_options(self, view): - return programFilterOptions() - + class ProgramFilter(ProgramRowFilter): def apply(self, query, value, alias=None): return query.filter(self.column.program_id == int(value)) @@ -670,10 +661,7 @@ def assistantQueryFactory(): class SemesterExperimentView(SecureAdminModelView): - class ProgramFilter(FilterEqual): - def get_options(self, view): - return programFilterOptions() - + class ProgramFilter(ProgramRowFilter): def apply(self, query, value, alias=None): return query.filter(self.column.experiment.has(Experiment.program_id == int(value))) @@ -872,22 +860,20 @@ class AdminView(SecureAdminModelView): assistantBlankText = "Auto assign if experiment has only one assistant" -def experimentFilterOptions(): - activeExperiments = Experiment.query.filter(Experiment.active == True) - return tuple( - ( - f"{activeExperiment.number},{activeExperiment.program_id}", - f"{activeExperiment.number} {activeExperiment.program.repr()}", +class ExperimentRowFilter(FilterEqual): + def get_options(self, view): + activeExperiments = Experiment.query.filter(Experiment.active == True) + return tuple( + ( + f"{activeExperiment.number},{activeExperiment.program_id}", + f"{activeExperiment.number} {activeExperiment.program.repr()}", + ) + for activeExperiment in activeExperiments ) - for activeExperiment in activeExperiments - ) class GroupExperimentView(SecureAdminModelView): - class ExperimentFilter(FilterEqual): - def get_options(self, view): - return experimentFilterOptions() - + class ExperimentFilter(ExperimentRowFilter): def apply(self, query, value, alias=None): values = value.split(",") experimentNumber = int(values[0]) @@ -1014,16 +1000,14 @@ def groupExperimentQueryFactory(): ) -def assistantFilterOptions(): - activeAssistants = assistantQueryFactory() - return tuple((assistant.id, assistant.repr()) for assistant in activeAssistants) +class AssistantRowFilter(FilterEqual): + def get_options(self, view): + activeAssistants = assistantQueryFactory() + return tuple((assistant.id, assistant.repr()) for assistant in activeAssistants) class AppointmentView(SecureAdminModelView): - class ExperimentFilter(FilterEqual): - def get_options(self, view): - return experimentFilterOptions() - + class ExperimentFilter(ExperimentRowFilter): def apply(self, query, value, alias=None): values = value.split(",") experimentNumber = int(values[0]) @@ -1042,10 +1026,7 @@ class AppointmentView(SecureAdminModelView): ), ) - class AssistantFilter(FilterEqual): - def get_options(self, view): - return assistantFilterOptions() - + class AssistantFilter(AssistantRowFilter): def apply(self, query, value, alias=None): return query.filter(self.column.assistant_id == int(value)) @@ -1144,10 +1125,7 @@ class ExperimentMarkView(SecureAdminModelView): def apply(self, query, value, alias=None): return query.filter(self.column.part_student.has(PartStudent.student_id == int(value))) - class AssistantFilter(FilterEqual): - def get_options(self, view): - return assistantFilterOptions() - + class AssistantFilter(AssistantRowFilter): def apply(self, query, value, alias=None): return query.filter(self.column.assistant_id == int(value)) @@ -1159,10 +1137,7 @@ class ExperimentMarkView(SecureAdminModelView): def apply(self, query, value, alias=None): return query.filter(self.column.admin_id == int(value)) - class ExperimentFilter(FilterEqual): - def get_options(self, view): - return experimentFilterOptions() - + class ExperimentFilter(ExperimentRowFilter): def apply(self, query, value, alias=None): values = value.split(",") experimentNumber = int(values[0]) @@ -1181,24 +1156,15 @@ class ExperimentMarkView(SecureAdminModelView): ), ) - class ProgramFilter(FilterEqual): - def get_options(self, view): - return programFilterOptions() - + class ProgramFilter(ProgramRowFilter): 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() - + class PartFilter(PartRowFilter): 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): - return semesterFilterOptions() - + class SemesterFilter(SemesterRowFilter): def apply(self, query, value, alias=None): return query.filter( self.column.group_experiment.has( @@ -1313,9 +1279,9 @@ class ExperimentMarkView(SecureAdminModelView): model.part_student.checkThenSetFinalPartMark() return ret - else: - # Nothing changed - return True + + # Nothing changed + return True class ProgramView(SecureAdminModelView):