1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-11-08 21:21:06 +00:00

Moved get_query and get_count_query to parent classs

This commit is contained in:
Mo 2021-07-12 13:06:44 +02:00
parent 72b05beefc
commit 18257799c7
2 changed files with 36 additions and 81 deletions

View file

@ -29,9 +29,23 @@ class SecureModelView(ModelView):
create_template = "admin_create.html"
edit_template = "admin_edit.html"
queryFilter = None
def is_accessible(self):
return adminViewIsAccessible()
def inaccessible_callback(self, name, **kwargs):
# Redirect to login page if user doesn't have access
return redirect(url_for("security.login", next=request.url))
def get_query(self):
if self.queryFilter:
return super().get_query().filter(self.queryFilter())
else:
return super().get_query()
def get_count_query(self):
if self.queryFilter:
return super().get_count_query().filter(self.queryFilter())
else:
return super().get_count_query()

View file

@ -188,15 +188,7 @@ class PartView(SecureModelView):
column_details_list = ["label", "semester", "part_students", "groups"]
form_columns = ["label", "semester"]
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]))
)
queryFilter = lambda self: Part.id.in_([part.id for part in userActiveSemester().parts])
class StudentView(SecureModelView):
@ -225,18 +217,14 @@ class StudentView(SecureModelView):
]
def partQueryFactory():
return Part.query.filter(Part.id.in_([part.id for part in userActiveSemester().parts]))
partQueryFactory = lambda: Part.query.filter(Part.id.in_([part.id for part in userActiveSemester().parts]))
def groupQueryFactory():
return Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))
groupQueryFactory = lambda: Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))
class PartStudentView(SecureModelView):
class CreateForm(Form):
def studentQueryFactory():
return Student.query
studentQueryFactory = lambda: Student.query
student = QuerySelectField(
"Student", query_factory=studentQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
@ -255,6 +243,8 @@ class PartStudentView(SecureModelView):
column_filters = ["part", "student", "group"]
queryFilter = lambda self: PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])
partGroupPartMismatchException = "Student's part and group's part do not match!"
def create_form(self, obj=None):
@ -271,21 +261,12 @@ class PartStudentView(SecureModelView):
else:
return super().handle_view_exception(exc)
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 GroupView(SecureModelView):
class CreateForm(Form):
def partStudentsQueryFactory():
return PartStudent.query.filter(PartStudent.part_id.in_([part.id for part in userActiveSemester().parts]))
partStudentsQueryFactory = lambda: PartStudent.query.filter(
PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])
)
part = QuerySelectField(
"Part", query_factory=partQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
@ -300,6 +281,8 @@ class GroupView(SecureModelView):
column_list = ["number", "part", "part_students", "group_experiments"]
column_filters = ["number", "part"]
queryFilter = lambda self: Group.part_id.in_([part.id for part in userActiveSemester().parts])
partStudentPartPartMismatchException = "Group's part and student's part do not match!"
def create_model(self, form):
@ -339,16 +322,6 @@ class GroupView(SecureModelView):
form = self.CreateForm
return form(get_form_data(), obj=obj)
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]))
)
class ExperimentView(SecureModelView):
can_view_details = True
@ -372,15 +345,7 @@ class ExperimentView(SecureModelView):
class SemesterExperimentView(SecureModelView):
column_list = ["experiment", "semester", "assistants"]
def get_query(self):
return super().get_query().filter(SemesterExperiment.semester == userActiveSemester())
def get_count_query(self):
return (
self.session.query(func.count("*"))
.select_from(self.model)
.filter(SemesterExperiment.semester == userActiveSemester())
)
queryFilter = lambda self: SemesterExperiment.semester == userActiveSemester()
class AssistantView(SecureModelView):
@ -400,11 +365,11 @@ class AssistantView(SecureModelView):
class GroupExperimentView(SecureModelView):
class CreateForm(Form):
def semesterExperimentQueryFactory():
return SemesterExperiment.query.filter(SemesterExperiment.semester == userActiveSemester())
semesterExperimentQueryFactory = lambda: SemesterExperiment.query.filter(
SemesterExperiment.semester == userActiveSemester()
)
def assistantQueryFactory():
return Assistant.query.filter(
assistantQueryFactory = lambda: Assistant.query.filter(
Assistant.user_id.in_([user.id for user in User.query.filter(User.active == True)])
)
@ -445,6 +410,10 @@ class GroupExperimentView(SecureModelView):
column_list = ["group", "semester_experiment", "appointments", "experiment_marks"]
column_filters = ["group", "semester_experiment.experiment", "appointments"]
queryFilter = lambda self: GroupExperiment.group_id.in_(
[g.id for g in Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))]
)
def create_model(self, form):
try:
model = GroupExperiment.checkAndInit(
@ -492,34 +461,6 @@ class GroupExperimentView(SecureModelView):
self.after_model_change(form, model, True)
return model
def get_query(self):
return (
super()
.get_query()
.filter(
GroupExperiment.group_id.in_(
[
g.id
for g in Group.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(
GroupExperiment.group_id.in_(
[
g.id
for g in Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))
]
)
)
)
class AppointmentView(SecureModelView):
column_list = ["date", "special", "group_experiment", "assistant"]