From 8333da3c918ed85d382f351d0d69c4480b7acdf0 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Fri, 30 Jul 2021 15:14:35 +0200 Subject: [PATCH] Improved filters --- advlabdb/adminModelViews.py | 31 +++++++++++-------------------- advlabdb/assistantModelViews.py | 13 +++---------- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index ad11c87..723dd4a 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -118,7 +118,7 @@ class UserView(SecureAdminModelView): if model == current_user and not form.active.data: raise ModelViewException("Tried to deactiavte yourself as user!") - if form.generate_new_password.data: + if hasattr(form, "generate_new_password") and form.generate_new_password.data: password = randomPassword() UserView.flashPassword(password) @@ -248,7 +248,7 @@ class StudentView(SecureAdminModelView): def partQueryFactory(): - return Part.query.filter(Part.id.in_([part.id for part in userActiveSemester().parts])) + return Part.query.filter(Part.semester == userActiveSemester()) def groupQueryFactory(): @@ -294,7 +294,7 @@ class PartStudentView(SecureAdminModelView): column_filters = ["part", "student", "group"] def queryFilter(self): - return PartStudent.part_id.in_([part.id for part in userActiveSemester().parts]) + return PartStudent.part.has(Part.semester == userActiveSemester()) def create_form(self, obj=None): form = self.CreateForm @@ -311,7 +311,7 @@ class PartStudentView(SecureAdminModelView): def partStudentQueryFactory(): - return PartStudent.query.filter(PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])) + return PartStudent.query.filter(PartStudent.part.has(Part.semester == userActiveSemester())) class GroupView(SecureAdminModelView): @@ -389,7 +389,7 @@ class ExperimentView(SecureAdminModelView): def assistantQueryFactory(): - return Assistant.query.filter(Assistant.user_id.in_([user.id for user in User.query.filter(User.active == True)])) + return Assistant.query.filter(Assistant.user.has(User.active == True)) class SemesterExperimentView(SecureAdminModelView): @@ -503,9 +503,7 @@ class GroupExperimentView(SecureAdminModelView): column_filters = ["group", "semester_experiment.experiment", "appointments"] def queryFilter(self): - return GroupExperiment.group_id.in_( - [group.id for group in Group.query.filter(Group.semester == userActiveSemester())] - ) + return GroupExperiment.group.has(Group.semester == userActiveSemester()) def customCreateModel(self, form): return GroupExperiment.customInit(semester_experiment=form.semester_experiment.data, group=form.group.data) @@ -530,14 +528,7 @@ class GroupExperimentView(SecureAdminModelView): def groupExperimentQueryFactory(): return GroupExperiment.query.filter( - GroupExperiment.semester_experiment_id.in_( - [ - semesterExperiment.id - for semesterExperiment in SemesterExperiment.query.filter( - SemesterExperiment.semester == userActiveSemester() - ) - ] - ) + GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester()) ) @@ -570,8 +561,8 @@ class AppointmentView(SecureAdminModelView): ] def queryFilter(self): - return Appointment.group_experiment_id.in_( - groupExperiment.id for groupExperiment in groupExperimentQueryFactory() + return Appointment.group_experiment.has( + GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester()) ) def customCreateModel(self, form): @@ -641,8 +632,8 @@ class ExperimentMarkView(SecureAdminModelView): ] def queryFilter(self=None): - return ExperimentMark.group_experiment_id.in_( - groupExperiment.id for groupExperiment in groupExperimentQueryFactory() + return ExperimentMark.group_experiment.has( + GroupExperiment.semester_experiment.has(SemesterExperiment.semester == userActiveSemester()) ) def checkForm(form): diff --git a/advlabdb/assistantModelViews.py b/advlabdb/assistantModelViews.py index b056e40..d9b941b 100644 --- a/advlabdb/assistantModelViews.py +++ b/advlabdb/assistantModelViews.py @@ -32,18 +32,11 @@ class AssistantGroupExperimentView(SecureAssistantModelView): column_filters = ["group", "semester_experiment.experiment", "appointments"] def queryFilter1(self): - return GroupExperiment.group_id.in_( - [group.id for group in Group.query.filter(Group.semester == userActiveSemester())] - ) + return GroupExperiment.group.has(Group.semester == userActiveSemester()) def queryFilter2(self): - return GroupExperiment.semester_experiment_id.in_( - [ - semesterExperiment.id - for semesterExperiment in SemesterExperiment.query.filter( - SemesterExperiment.assistants.any(Assistant.id == current_user.assistant.id) - ) - ] + return GroupExperiment.semester_experiment.has( + SemesterExperiment.assistants.any(Assistant.user == current_user) ) def get_query(self):