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

Replace get_count and execute().scalars()

This commit is contained in:
Mo 2022-09-22 20:26:22 +02:00
parent d533db66ab
commit 83f4726296
4 changed files with 14 additions and 27 deletions

View file

@ -542,7 +542,7 @@ class PartRowFilter(FilterEqual):
if not has_request_context(): if not has_request_context():
return tuple() return tuple()
parts = db.session.execute(select(Part).where(Part.semester == current_user.active_semester)).scalars() parts = db.session.scalars(select(Part).where(Part.semester == current_user.active_semester))
return tuple((part.id, part.str_without_semester()) for part in parts) return tuple((part.id, part.str_without_semester()) for part in parts)
@ -620,7 +620,7 @@ class ProgramRowFilter(FilterEqual):
if not has_request_context(): if not has_request_context():
return tuple() return tuple()
programs = db.session.execute(select(Program)).scalars() programs = db.session.scalars(select(Program))
return tuple((program.id, str(program)) for program in programs) return tuple((program.id, str(program)) for program in programs)
@ -934,7 +934,7 @@ class ExperimentRowFilter(FilterEqual):
if not has_request_context(): if not has_request_context():
return tuple() return tuple()
activeExperiments = db.session.execute(select(Experiment).where(Experiment.active == True)).scalars() activeExperiments = db.session.scalars(select(Experiment).where(Experiment.active == True))
return tuple( return tuple(
( (
f"{activeExperiment.number},{activeExperiment.program_id}", f"{activeExperiment.number},{activeExperiment.program_id}",
@ -1191,7 +1191,7 @@ class ExperimentMarkView(SecureAdminModelView):
if not has_request_context(): if not has_request_context():
return tuple() return tuple()
admins = db.session.execute(select(Admin).join(User).where(User.active == True)).scalars() admins = db.session.scalars(select(Admin).join(User).where(User.active == True))
return tuple((admin.id, str(admin)) for admin in admins) return tuple((admin.id, str(admin)) for admin in admins)
def apply(self, query, value, alias=None): def apply(self, query, value, alias=None):

View file

@ -8,14 +8,7 @@ from sqlalchemy import func, select
from .exceptions import DatabaseException, ModelViewException from .exceptions import DatabaseException, ModelViewException
from .model_independent_funs import reportBadAttempt from .model_independent_funs import reportBadAttempt
from .models import ( from .models import Assistant, ExperimentMark, GroupExperiment, SemesterExperiment, db
Assistant,
ExperimentMark,
GroupExperiment,
SemesterExperiment,
db,
get_count,
)
def adminViewIsAccessible(): def adminViewIsAccessible():
@ -71,7 +64,7 @@ class SecureAssistantIndexView(CustomIndexView):
@expose("/") @expose("/")
def index(self): def index(self):
active_semester_experiment_mark_ids_stmt = ( active_semester_experiment_mark_ids_stmt = (
select(ExperimentMark.final_experiment_mark) select(func.count(ExperimentMark.final_experiment_mark))
.join(GroupExperiment) .join(GroupExperiment)
.join(SemesterExperiment) .join(SemesterExperiment)
.where(SemesterExperiment.semester == current_user.active_semester) .where(SemesterExperiment.semester == current_user.active_semester)
@ -79,9 +72,9 @@ class SecureAssistantIndexView(CustomIndexView):
.where(Assistant.user == current_user) .where(Assistant.user == current_user)
) )
number_of_all_experiment_marks = get_count(active_semester_experiment_mark_ids_stmt) number_of_all_experiment_marks = db.session.scalar(active_semester_experiment_mark_ids_stmt)
number_of_missing_final_experiment_marks = get_count( number_of_missing_final_experiment_marks = db.session.scalar(
active_semester_experiment_mark_ids_stmt.where(ExperimentMark.final_experiment_mark == None) active_semester_experiment_mark_ids_stmt.where(ExperimentMark.final_experiment_mark == None)
) )

View file

@ -32,12 +32,8 @@ db = SQLAlchemy()
FsModels.set_db_info(db) FsModels.set_db_info(db)
def get_count(table):
return db.session.scalar(select(func.count()).select_from(table))
def get_first(table): def get_first(table):
return db.session.execute(table.limit(1)).scalars().first() return db.session.scalars(table.limit(1)).first()
def roundHalfUpToInt(number): def roundHalfUpToInt(number):
@ -546,7 +542,7 @@ class Semester(db.Model):
if transferParts: if transferParts:
semester.transferPartsFrom(oldSemester) semester.transferPartsFrom(oldSemester)
for experiment in db.session.execute(select(Experiment).where(Experiment.active == True)).scalars(): for experiment in db.session.scalars(select(Experiment).where(Experiment.active == True)):
newSemesterExperiment = SemesterExperiment(experiment=experiment, semester=semester) newSemesterExperiment = SemesterExperiment(experiment=experiment, semester=semester)
if transferAssistants: if transferAssistants:
@ -571,7 +567,7 @@ class Semester(db.Model):
if limit > 0: if limit > 0:
stmt = stmt.limit(limit) stmt = stmt.limit(limit)
return db.session.execute(stmt).scalars() return db.session.scalars(stmt)
@staticmethod @staticmethod
def lastSemester(): def lastSemester():
@ -589,9 +585,7 @@ class Semester(db.Model):
if set_next_semester: if set_next_semester:
# Set active_semester to next_semester # Set active_semester to next_semester
users_in_semester_done = db.session.execute( users_in_semester_done = db.session.scalars(select(User).where(User.active_semester == semester))
select(User).where(User.active_semester == semester)
).scalars()
for user in users_in_semester_done: for user in users_in_semester_done:
user.active_semester = next_semester user.active_semester = next_semester

View file

@ -14,12 +14,12 @@ def _reset_admin_password(manage):
with app.app_context(): with app.app_context():
with db.session.begin(): with db.session.begin():
admins = db.session.execute(select(Admin).join(User).where(User.active == True)).scalars().all() admins = db.session.scalars(select(Admin).join(User).where(User.active == True)).all()
activate_user = False activate_user = False
if len(admins) == 0: if len(admins) == 0:
click.echo("There is no admin with an active user. The user of the chosen admin will be activated.") click.echo("There is no admin with an active user. The user of the chosen admin will be activated.")
admins = db.session.execute(select(Admin)).scalars().all() admins = db.session.scalars(select(Admin)).all()
activate_user = True activate_user = True
num_admins = len(admins) num_admins = len(admins)