1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +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" create_template = "admin_create.html"
edit_template = "admin_edit.html" edit_template = "admin_edit.html"
queryFilter = None
def is_accessible(self): def is_accessible(self):
return adminViewIsAccessible() return adminViewIsAccessible()
def inaccessible_callback(self, name, **kwargs): def inaccessible_callback(self, name, **kwargs):
# Redirect to login page if user doesn't have access # Redirect to login page if user doesn't have access
return redirect(url_for("security.login", next=request.url)) 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"] column_details_list = ["label", "semester", "part_students", "groups"]
form_columns = ["label", "semester"] form_columns = ["label", "semester"]
def get_query(self): queryFilter = lambda self: Part.id.in_([part.id for part in userActiveSemester().parts])
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]))
)
class StudentView(SecureModelView): class StudentView(SecureModelView):
@ -225,18 +217,14 @@ class StudentView(SecureModelView):
] ]
def partQueryFactory(): partQueryFactory = lambda: Part.query.filter(Part.id.in_([part.id for part in userActiveSemester().parts]))
return Part.query.filter(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]))
def groupQueryFactory():
return Group.query.filter(Group.part_id.in_([part.id for part in userActiveSemester().parts]))
class PartStudentView(SecureModelView): class PartStudentView(SecureModelView):
class CreateForm(Form): class CreateForm(Form):
def studentQueryFactory(): studentQueryFactory = lambda: Student.query
return Student.query
student = QuerySelectField( student = QuerySelectField(
"Student", query_factory=studentQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-" "Student", query_factory=studentQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
@ -255,6 +243,8 @@ class PartStudentView(SecureModelView):
column_filters = ["part", "student", "group"] 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!" partGroupPartMismatchException = "Student's part and group's part do not match!"
def create_form(self, obj=None): def create_form(self, obj=None):
@ -271,21 +261,12 @@ class PartStudentView(SecureModelView):
else: else:
return super().handle_view_exception(exc) 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 GroupView(SecureModelView):
class CreateForm(Form): class CreateForm(Form):
def partStudentsQueryFactory(): partStudentsQueryFactory = lambda: PartStudent.query.filter(
return PartStudent.query.filter(PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])) PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])
)
part = QuerySelectField( part = QuerySelectField(
"Part", query_factory=partQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-" "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_list = ["number", "part", "part_students", "group_experiments"]
column_filters = ["number", "part"] 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!" partStudentPartPartMismatchException = "Group's part and student's part do not match!"
def create_model(self, form): def create_model(self, form):
@ -339,16 +322,6 @@ class GroupView(SecureModelView):
form = self.CreateForm form = self.CreateForm
return form(get_form_data(), obj=obj) 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): class ExperimentView(SecureModelView):
can_view_details = True can_view_details = True
@ -372,15 +345,7 @@ class ExperimentView(SecureModelView):
class SemesterExperimentView(SecureModelView): class SemesterExperimentView(SecureModelView):
column_list = ["experiment", "semester", "assistants"] column_list = ["experiment", "semester", "assistants"]
def get_query(self): queryFilter = lambda self: SemesterExperiment.semester == userActiveSemester()
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())
)
class AssistantView(SecureModelView): class AssistantView(SecureModelView):
@ -400,13 +365,13 @@ class AssistantView(SecureModelView):
class GroupExperimentView(SecureModelView): class GroupExperimentView(SecureModelView):
class CreateForm(Form): class CreateForm(Form):
def semesterExperimentQueryFactory(): semesterExperimentQueryFactory = lambda: SemesterExperiment.query.filter(
return SemesterExperiment.query.filter(SemesterExperiment.semester == userActiveSemester()) SemesterExperiment.semester == userActiveSemester()
)
def assistantQueryFactory(): assistantQueryFactory = lambda: Assistant.query.filter(
return Assistant.query.filter( Assistant.user_id.in_([user.id for user in User.query.filter(User.active == True)])
Assistant.user_id.in_([user.id for user in User.query.filter(User.active == True)]) )
)
group = QuerySelectField( group = QuerySelectField(
"Group", query_factory=groupQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-" "Group", query_factory=groupQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
@ -445,6 +410,10 @@ class GroupExperimentView(SecureModelView):
column_list = ["group", "semester_experiment", "appointments", "experiment_marks"] column_list = ["group", "semester_experiment", "appointments", "experiment_marks"]
column_filters = ["group", "semester_experiment.experiment", "appointments"] 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): def create_model(self, form):
try: try:
model = GroupExperiment.checkAndInit( model = GroupExperiment.checkAndInit(
@ -492,34 +461,6 @@ class GroupExperimentView(SecureModelView):
self.after_model_change(form, model, True) self.after_model_change(form, model, True)
return model 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): class AppointmentView(SecureModelView):
column_list = ["date", "special", "group_experiment", "assistant"] column_list = ["date", "special", "group_experiment", "assistant"]