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

Use get_first in database_import.py

This commit is contained in:
Mo 2022-05-16 22:28:09 +02:00
parent 5b6f0d76f2
commit 854abb093b

View file

@ -8,6 +8,7 @@ from sqlalchemy import select
from . import db
from .exceptions import DataBaseImportException
from .model_independent_funs import get_first
from .models import (
Appointment,
Assistant,
@ -145,13 +146,7 @@ def importFromFile(filePath):
semesterLabel = not_nullable(semesters["label"][0])
semesterYear = int(not_nullable(semesters["year"][0]))
dbSemester = (
db.session.execute(
select(Semester).where(Semester.label == semesterLabel, Semester.year == semesterYear).limit(1)
)
.scalars()
.first()
)
dbSemester = get_first(select(Semester).where(Semester.label == semesterLabel, Semester.year == semesterYear))
if dbSemester is None:
raise DataBaseImportException(
@ -166,8 +161,7 @@ def importFromFile(filePath):
id = int(not_nullable(id))
partNumber = int(not_nullable(parts["number"][i]))
partProgramLabel = not_nullable(parts["program_label"][i])
dbPart = (
db.session.execute(
dbPart = get_first(
select(Part)
.join(Program)
.where(
@ -175,10 +169,6 @@ def importFromFile(filePath):
Program.label == partProgramLabel,
Part.semester == dbSemester,
)
.limit(1)
)
.scalars()
.first()
)
if dbPart is None:
@ -194,11 +184,7 @@ def importFromFile(filePath):
dbStudents = {}
for i, studentNumber in enumerate(students["student_number"]):
studentNumber = int(not_nullable(studentNumber))
dbStudent = (
db.session.execute(select(Student).where(Student.student_number == studentNumber).limit(1))
.scalars()
.first()
)
dbStudent = get_first(select(Student).where(Student.student_number == studentNumber))
if dbStudent is None:
dbStudent = Student(
@ -226,13 +212,7 @@ def importFromFile(filePath):
dbGroups = {}
for i, id in enumerate(groups["id"]):
id = int(not_nullable(id))
program = (
db.session.execute(
select(Program).where(Program.label == not_nullable(groups["program_label"][i])).limit(1)
)
.scalars()
.first()
)
program = get_first(select(Program).where(Program.label == not_nullable(groups["program_label"][i])))
dbGroup = Group(
number=int(not_nullable(groups["number"][i])),
program=program,
@ -261,21 +241,11 @@ def importFromFile(filePath):
id = int(not_nullable(id))
experimentNumber = int(not_nullable(experiments["number"][i]))
experimentProgram = (
db.session.execute(
select(Program).where(Program.label == not_nullable(experiments["program_label"][i])).limit(1)
experimentProgram = get_first(
select(Program).where(Program.label == not_nullable(experiments["program_label"][i]))
)
.scalars()
.first()
)
dbExperiment = (
db.session.execute(
select(Experiment)
.where(Experiment.number == experimentNumber, Experiment.program == experimentProgram)
.limit(1)
)
.scalars()
.first()
dbExperiment = get_first(
select(Experiment).where(Experiment.number == experimentNumber, Experiment.program == experimentProgram)
)
if dbExperiment is None:
@ -284,14 +254,10 @@ def importFromFile(filePath):
f"Experiment with number {experimentNumber} and program {experimentProgram.repr()} does not exist in the database. Please make sure that experiments are created from the web interface."
)
dbSemesterExperiment = (
db.session.execute(
select(SemesterExperiment)
.where(SemesterExperiment.experiment == dbExperiment, SemesterExperiment.semester == dbSemester)
.limit(1)
dbSemesterExperiment = get_first(
select(SemesterExperiment).where(
SemesterExperiment.experiment == dbExperiment, SemesterExperiment.semester == dbSemester
)
.scalars()
.first()
)
if dbSemesterExperiment is None:
@ -320,11 +286,7 @@ def importFromFile(filePath):
for i, date in enumerate(appointments["date"]):
date = not_nullable(date)
assistantEmail = not_nullable(appointments["assistant_email"][i])
assistant = (
db.session.execute(select(Assistant).join(User).where(User.email == assistantEmail).limit(1))
.scalars()
.first()
)
assistant = get_first(select(Assistant).join(User).where(User.email == assistantEmail))
if assistant is None:
raise DataBaseImportException(