1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +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 . import db
from .exceptions import DataBaseImportException from .exceptions import DataBaseImportException
from .model_independent_funs import get_first
from .models import ( from .models import (
Appointment, Appointment,
Assistant, Assistant,
@ -145,13 +146,7 @@ def importFromFile(filePath):
semesterLabel = not_nullable(semesters["label"][0]) semesterLabel = not_nullable(semesters["label"][0])
semesterYear = int(not_nullable(semesters["year"][0])) semesterYear = int(not_nullable(semesters["year"][0]))
dbSemester = ( dbSemester = get_first(select(Semester).where(Semester.label == semesterLabel, Semester.year == semesterYear))
db.session.execute(
select(Semester).where(Semester.label == semesterLabel, Semester.year == semesterYear).limit(1)
)
.scalars()
.first()
)
if dbSemester is None: if dbSemester is None:
raise DataBaseImportException( raise DataBaseImportException(
@ -166,8 +161,7 @@ def importFromFile(filePath):
id = int(not_nullable(id)) id = int(not_nullable(id))
partNumber = int(not_nullable(parts["number"][i])) partNumber = int(not_nullable(parts["number"][i]))
partProgramLabel = not_nullable(parts["program_label"][i]) partProgramLabel = not_nullable(parts["program_label"][i])
dbPart = ( dbPart = get_first(
db.session.execute(
select(Part) select(Part)
.join(Program) .join(Program)
.where( .where(
@ -175,10 +169,6 @@ def importFromFile(filePath):
Program.label == partProgramLabel, Program.label == partProgramLabel,
Part.semester == dbSemester, Part.semester == dbSemester,
) )
.limit(1)
)
.scalars()
.first()
) )
if dbPart is None: if dbPart is None:
@ -194,11 +184,7 @@ def importFromFile(filePath):
dbStudents = {} dbStudents = {}
for i, studentNumber in enumerate(students["student_number"]): for i, studentNumber in enumerate(students["student_number"]):
studentNumber = int(not_nullable(studentNumber)) studentNumber = int(not_nullable(studentNumber))
dbStudent = ( dbStudent = get_first(select(Student).where(Student.student_number == studentNumber))
db.session.execute(select(Student).where(Student.student_number == studentNumber).limit(1))
.scalars()
.first()
)
if dbStudent is None: if dbStudent is None:
dbStudent = Student( dbStudent = Student(
@ -226,13 +212,7 @@ def importFromFile(filePath):
dbGroups = {} dbGroups = {}
for i, id in enumerate(groups["id"]): for i, id in enumerate(groups["id"]):
id = int(not_nullable(id)) id = int(not_nullable(id))
program = ( program = get_first(select(Program).where(Program.label == not_nullable(groups["program_label"][i])))
db.session.execute(
select(Program).where(Program.label == not_nullable(groups["program_label"][i])).limit(1)
)
.scalars()
.first()
)
dbGroup = Group( dbGroup = Group(
number=int(not_nullable(groups["number"][i])), number=int(not_nullable(groups["number"][i])),
program=program, program=program,
@ -261,21 +241,11 @@ def importFromFile(filePath):
id = int(not_nullable(id)) id = int(not_nullable(id))
experimentNumber = int(not_nullable(experiments["number"][i])) experimentNumber = int(not_nullable(experiments["number"][i]))
experimentProgram = ( experimentProgram = get_first(
db.session.execute( select(Program).where(Program.label == not_nullable(experiments["program_label"][i]))
select(Program).where(Program.label == not_nullable(experiments["program_label"][i])).limit(1)
) )
.scalars() dbExperiment = get_first(
.first() select(Experiment).where(Experiment.number == experimentNumber, Experiment.program == experimentProgram)
)
dbExperiment = (
db.session.execute(
select(Experiment)
.where(Experiment.number == experimentNumber, Experiment.program == experimentProgram)
.limit(1)
)
.scalars()
.first()
) )
if dbExperiment is None: 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." 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 = ( dbSemesterExperiment = get_first(
db.session.execute( select(SemesterExperiment).where(
select(SemesterExperiment) SemesterExperiment.experiment == dbExperiment, SemesterExperiment.semester == dbSemester
.where(SemesterExperiment.experiment == dbExperiment, SemesterExperiment.semester == dbSemester)
.limit(1)
) )
.scalars()
.first()
) )
if dbSemesterExperiment is None: if dbSemesterExperiment is None:
@ -320,11 +286,7 @@ def importFromFile(filePath):
for i, date in enumerate(appointments["date"]): for i, date in enumerate(appointments["date"]):
date = not_nullable(date) date = not_nullable(date)
assistantEmail = not_nullable(appointments["assistant_email"][i]) assistantEmail = not_nullable(appointments["assistant_email"][i])
assistant = ( assistant = get_first(select(Assistant).join(User).where(User.email == assistantEmail))
db.session.execute(select(Assistant).join(User).where(User.email == assistantEmail).limit(1))
.scalars()
.first()
)
if assistant is None: if assistant is None:
raise DataBaseImportException( raise DataBaseImportException(