mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +00:00
Replace all .has()
This commit is contained in:
parent
d7886eb773
commit
91826cc98d
1 changed files with 42 additions and 57 deletions
|
@ -78,7 +78,7 @@ class SemesterRowFilter(FilterEqual):
|
|||
if not has_request_context():
|
||||
return tuple()
|
||||
|
||||
semesters = Semester.query.order_by(Semester.id.desc())
|
||||
semesters = sortedSemestersStartingWithNewest()
|
||||
return tuple((semester.id, semester.repr()) for semester in semesters)
|
||||
|
||||
|
||||
|
@ -474,7 +474,7 @@ class PartRowFilter(FilterEqual):
|
|||
if not has_request_context():
|
||||
return tuple()
|
||||
|
||||
parts = Part.query.filter(Part.semester == current_user.active_semester)
|
||||
parts = db.execute(select(Part).where(Part.semester == current_user.active_semester)).scalars()
|
||||
return tuple((part.id, f"{part.program.repr()}{part.number}") for part in parts)
|
||||
|
||||
|
||||
|
@ -576,7 +576,7 @@ class ProgramRowFilter(FilterEqual):
|
|||
if not has_request_context():
|
||||
return tuple()
|
||||
|
||||
programs = Program.query
|
||||
programs = db.execute(select(Program)).scalars()
|
||||
return tuple((program.id, program.repr()) for program in programs)
|
||||
|
||||
|
||||
|
@ -596,7 +596,7 @@ class GroupView(SecureAdminModelView):
|
|||
def query_factory():
|
||||
return partStudentQueryFactory().where(
|
||||
or_(
|
||||
and_(PartStudent.group == None, PartStudent.part.has(Part.program == group.program)),
|
||||
and_(PartStudent.group == None, Part.program == group.program),
|
||||
PartStudent.group == group,
|
||||
)
|
||||
)
|
||||
|
@ -702,13 +702,13 @@ class ExperimentView(SecureAdminModelView):
|
|||
|
||||
|
||||
def assistantQueryFactory():
|
||||
return Assistant.query.filter(Assistant.user.has(User.active == True))
|
||||
return Assistant.query.join(User).where(User.active == True)
|
||||
|
||||
|
||||
class SemesterExperimentView(SecureAdminModelView):
|
||||
class ProgramFilter(ProgramRowFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
return query.filter(self.column.experiment.has(Experiment.program_id == int(value)))
|
||||
return query.join(Experiment).where(Experiment.program_id == int(value))
|
||||
|
||||
class ExperimentEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
|
||||
def customId(self, row):
|
||||
|
@ -823,10 +823,11 @@ class SemesterExperimentView(SecureAdminModelView):
|
|||
def userHasRoleFilterFactory(column, role_name):
|
||||
class UserHasRoleFilter(BooleanEqualFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
query = query.join(User)
|
||||
if bool(int(value)): # value is string "0" or "1"
|
||||
return query.filter(self.column.user.has(User.roles.any(Role.name == role_name)))
|
||||
return query.join(User.roles).where(Role.name == role_name)
|
||||
else:
|
||||
return query.filter(self.column.user.has(not_(User.roles.any(Role.name == role_name))))
|
||||
return query.where(not_(User.roles.any(Role.name == role_name)))
|
||||
|
||||
return UserHasRoleFilter(column, f"Currently has {role_name} role")
|
||||
|
||||
|
@ -930,7 +931,7 @@ class ExperimentRowFilter(FilterEqual):
|
|||
if not has_request_context():
|
||||
return tuple()
|
||||
|
||||
activeExperiments = Experiment.query.filter(Experiment.active == True)
|
||||
activeExperiments = db.execute(select(Experiment).where(Experiment.active == True)).scalars()
|
||||
return tuple(
|
||||
(
|
||||
f"{activeExperiment.number},{activeExperiment.program_id}",
|
||||
|
@ -939,21 +940,23 @@ class ExperimentRowFilter(FilterEqual):
|
|||
for activeExperiment in activeExperiments
|
||||
)
|
||||
|
||||
def get_values(value):
|
||||
values = value.split(",")
|
||||
experimentNumber = int(values[0])
|
||||
programId = int(values[1])
|
||||
|
||||
return (experimentNumber, programId)
|
||||
|
||||
|
||||
class GroupExperimentView(SecureAdminModelView):
|
||||
class ExperimentFilter(ExperimentRowFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
values = value.split(",")
|
||||
experimentNumber = int(values[0])
|
||||
programId = int(values[1])
|
||||
experimentNumber, programId = ExperimentRowFilter.get_values(value)
|
||||
|
||||
return query.filter(
|
||||
self.column.semester_experiment.has(
|
||||
SemesterExperiment.experiment.has(Experiment.program_id == programId)
|
||||
),
|
||||
self.column.semester_experiment.has(
|
||||
SemesterExperiment.experiment.has(Experiment.number == experimentNumber)
|
||||
),
|
||||
return (
|
||||
query.join(SemesterExperiment)
|
||||
.join(Experiment)
|
||||
.where(Experiment.program_id == programId, Experiment.number == experimentNumber)
|
||||
)
|
||||
|
||||
class GroupEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
|
||||
|
@ -1068,8 +1071,8 @@ class GroupExperimentView(SecureAdminModelView):
|
|||
|
||||
|
||||
def groupExperimentQueryFactory():
|
||||
return GroupExperiment.query.filter(
|
||||
GroupExperiment.semester_experiment.has(SemesterExperiment.semester == current_user.active_semester)
|
||||
return GroupExperiment.query.join(SemesterExperiment).where(
|
||||
SemesterExperiment.semester == current_user.active_semester
|
||||
)
|
||||
|
||||
|
||||
|
@ -1085,21 +1088,13 @@ class AssistantRowFilter(FilterEqual):
|
|||
class AppointmentView(SecureAdminModelView):
|
||||
class ExperimentFilter(ExperimentRowFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
values = value.split(",")
|
||||
experimentNumber = int(values[0])
|
||||
programId = int(values[1])
|
||||
experimentNumber, programId = ExperimentRowFilter.get_values(value)
|
||||
|
||||
return query.filter(
|
||||
self.column.group_experiment.has(
|
||||
GroupExperiment.semester_experiment.has(
|
||||
SemesterExperiment.experiment.has(Experiment.program_id == programId)
|
||||
)
|
||||
),
|
||||
self.column.group_experiment.has(
|
||||
GroupExperiment.semester_experiment.has(
|
||||
SemesterExperiment.experiment.has(Experiment.number == experimentNumber)
|
||||
)
|
||||
),
|
||||
return (
|
||||
query.join(GroupExperiment)
|
||||
.join(SemesterExperiment)
|
||||
.join(Experiment)
|
||||
.where(Experiment.program_id == programId, Experiment.number == experimentNumber)
|
||||
)
|
||||
|
||||
class AssistantFilter(AssistantRowFilter):
|
||||
|
@ -1205,7 +1200,7 @@ class ExperimentMarkView(SecureAdminModelView):
|
|||
return False
|
||||
|
||||
def apply(self, query, value, alias=None):
|
||||
return query.filter(self.column.part_student.has(PartStudent.student_id == int(value)))
|
||||
return query.join(PartStudent).where(PartStudent.student_id == int(value))
|
||||
|
||||
class AssistantFilter(AssistantRowFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
|
@ -1216,7 +1211,7 @@ class ExperimentMarkView(SecureAdminModelView):
|
|||
if not has_request_context():
|
||||
return tuple()
|
||||
|
||||
admins = Admin.query.filter(Admin.user.has(User.active == True))
|
||||
admins = db.execute(select(Admin).join(User).where(User.active == True)).scalars()
|
||||
return tuple((admin.id, admin.repr()) for admin in admins)
|
||||
|
||||
def apply(self, query, value, alias=None):
|
||||
|
@ -1224,37 +1219,27 @@ class ExperimentMarkView(SecureAdminModelView):
|
|||
|
||||
class ExperimentFilter(ExperimentRowFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
values = value.split(",")
|
||||
experimentNumber = int(values[0])
|
||||
programId = int(values[1])
|
||||
experimentNumber, programId = ExperimentRowFilter.get_values(value)
|
||||
|
||||
return query.filter(
|
||||
self.column.group_experiment.has(
|
||||
GroupExperiment.semester_experiment.has(
|
||||
SemesterExperiment.experiment.has(Experiment.program_id == programId)
|
||||
)
|
||||
),
|
||||
self.column.group_experiment.has(
|
||||
GroupExperiment.semester_experiment.has(
|
||||
SemesterExperiment.experiment.has(Experiment.number == experimentNumber)
|
||||
)
|
||||
),
|
||||
return (
|
||||
query.join(GroupExperiment)
|
||||
.join(SemesterExperiment)
|
||||
.join(Experiment)
|
||||
.where(Experiment.program_id == programId, Experiment.number == experimentNumber)
|
||||
)
|
||||
|
||||
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))))
|
||||
return query.join(PartStudent).join(Part).where(Part.program_id == int(value))
|
||||
|
||||
class PartFilter(PartRowFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
return query.filter(self.column.part_student.has(PartStudent.part_id == int(value)))
|
||||
return query.join(PartStudent).where(PartStudent.part_id == int(value))
|
||||
|
||||
class SemesterFilter(SemesterRowFilter):
|
||||
def apply(self, query, value, alias=None):
|
||||
return query.filter(
|
||||
self.column.group_experiment.has(
|
||||
GroupExperiment.semester_experiment.has(SemesterExperiment.semester_id == int(value))
|
||||
)
|
||||
return (
|
||||
query.join(GroupExperiment).join(SemesterExperiment).where(SemesterExperiment.semester_id == int(value))
|
||||
)
|
||||
|
||||
class PartStudentEndpointLinkRowAction(CustomIdEndpointLinkRowAction):
|
||||
|
|
Loading…
Reference in a new issue