From f989cea8799a6535dff693e790c65f4f201be0ee Mon Sep 17 00:00:00 2001 From: Mo8it Date: Fri, 30 Jul 2021 02:50:49 +0200 Subject: [PATCH] Added AssistantGroupExperimentView --- advlabdb/__init__.py | 2 +- .../{modelViews.py => adminModelViews.py} | 0 advlabdb/assistantModelViews.py | 60 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) rename advlabdb/{modelViews.py => adminModelViews.py} (100%) create mode 100644 advlabdb/assistantModelViews.py diff --git a/advlabdb/__init__.py b/advlabdb/__init__.py index 30f83c7..7ca549e 100644 --- a/advlabdb/__init__.py +++ b/advlabdb/__init__.py @@ -54,7 +54,7 @@ user_datastore = SQLAlchemyUserDatastore(db, models.User, models.Role) Security(app, user_datastore) try: - from advlabdb import routes, modelViews + from advlabdb import routes, adminModelViews, assistantModelViews except Exception as ex: print( "\nYou are probably initializing the database with a script. If not, then you have to worry about not being able to import in __init__.py!\n" diff --git a/advlabdb/modelViews.py b/advlabdb/adminModelViews.py similarity index 100% rename from advlabdb/modelViews.py rename to advlabdb/adminModelViews.py diff --git a/advlabdb/assistantModelViews.py b/advlabdb/assistantModelViews.py new file mode 100644 index 0000000..55f2e2d --- /dev/null +++ b/advlabdb/assistantModelViews.py @@ -0,0 +1,60 @@ +from flask_security import current_user + +from advlabdb import assistantSpace, db +from advlabdb.customClasses import SecureAssistantModelView +from advlabdb.models import ( + Appointment, + Assistant, + Experiment, + ExperimentMark, + Group, + GroupExperiment, + Part, + SemesterExperiment, + PartStudent, + Role, + Semester, + Student, + User, + Program, +) +from advlabdb.utils import userActiveSemester +from advlabdb.exceptions import ModelViewException, DataBaseException + + +class AssistantGroupExperimentView(SecureAssistantModelView): + can_create = False + can_edit = False + can_delete = False + column_display_actions = False + + column_list = ["group", "semester_experiment", "appointments", "experiment_marks"] + 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())] + ) + + 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) + ) + ] + ) + + def get_query(self): + return super().get_query().filter(self.queryFilter1()).filter(self.queryFilter2()) + + def get_count_query(self): + return super().get_count_query().filter(self.queryFilter1()).filter(self.queryFilter2()) + + +assistantSpace.add_view( + AssistantGroupExperimentView( + GroupExperiment, db.session, endpoint="assistant_groupexperiment", url="groupexperiment" + ) +)