1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Migrate database_import.py

This commit is contained in:
Mo 2022-05-16 19:14:55 +02:00
parent 659156b749
commit 0c5709d2ab

View file

@ -4,6 +4,7 @@ from pathlib import Path
from shutil import copy2
from flask import flash, has_request_context
from sqlalchemy import select
from . import db
from .exceptions import DataBaseImportException
@ -144,9 +145,15 @@ def importFromFile(filePath):
semesterLabel = not_nullable(semesters["label"][0])
semesterYear = int(not_nullable(semesters["year"][0]))
dbSemester = Semester.query.filter(Semester.label == semesterLabel, Semester.year == semesterYear).first()
dbSemester = (
db.session.execute(
select(Semester).where(Semester.label == semesterLabel, Semester.year == semesterYear).limit(1)
)
.scalars()
.first()
)
if not dbSemester:
if dbSemester is None:
raise DataBaseImportException(
f"{semesterLabel}{semesterYear} does not exist in the database! Please make sure that you create the semester from the web interface first."
)
@ -159,13 +166,22 @@ def importFromFile(filePath):
id = int(not_nullable(id))
partNumber = int(not_nullable(parts["number"][i]))
partProgramLabel = not_nullable(parts["program_label"][i])
dbPart = Part.query.filter(
dbPart = (
db.session.execute(
select(Part)
.join(Program)
.where(
Part.number == partNumber,
Part.program.has(Program.label == partProgramLabel),
Program.label == partProgramLabel,
Part.semester == dbSemester,
).first()
)
.limit(1)
)
.scalars()
.first()
)
if not dbPart:
if dbPart is None:
raise DataBaseImportException(
f"Part with number {partNumber} and program label {partProgramLabel} does not exist in the database! Please make sure that you create parts from the web interface first."
)
@ -178,9 +194,13 @@ def importFromFile(filePath):
dbStudents = {}
for i, studentNumber in enumerate(students["student_number"]):
studentNumber = int(not_nullable(studentNumber))
dbStudent = Student.query.filter(Student.student_number == studentNumber).first()
dbStudent = (
db.session.execute(select(Student).where(Student.student_number == studentNumber).limit(1))
.scalars()
.first()
)
if not dbStudent:
if dbStudent is None:
dbStudent = Student(
student_number=studentNumber,
first_name=not_nullable(students["first_name"][i]),
@ -206,9 +226,16 @@ 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()
)
dbGroup = Group(
number=int(not_nullable(groups["number"][i])),
program=Program.query.filter(Program.label == not_nullable(groups["program_label"][i])).first(),
program=program,
semester=dbSemester,
)
db.session.add(dbGroup)
@ -233,24 +260,41 @@ def importFromFile(filePath):
for i, id in enumerate(experiments["id"]):
id = int(not_nullable(id))
experimentNumber = int(not_nullable(experiments["number"][i]))
experimentProgram = Program.query.filter(
Program.label == not_nullable(experiments["program_label"][i])
).first()
dbExperiment = Experiment.query.filter(
Experiment.number == experimentNumber, Experiment.program == experimentProgram
).first()
if not dbExperiment:
experimentProgram = (
db.session.execute(
select(Program).where(Program.label == not_nullable(experiments["program_label"][i])).limit(1)
)
.scalars()
.first()
)
dbExperiment = (
db.session.execute(
select(Experiment)
.where(Experiment.number == experimentNumber, Experiment.program == experimentProgram)
.limit(1)
)
.scalars()
.first()
)
if dbExperiment is None:
# TODO: Check if experimentProgram is None
raise DataBaseImportException(
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 = SemesterExperiment.query.filter(
SemesterExperiment.experiment == dbExperiment, SemesterExperiment.semester == dbSemester
).first()
dbSemesterExperiment = (
db.session.execute(
select(SemesterExperiment)
.where(SemesterExperiment.experiment == dbExperiment, SemesterExperiment.semester == dbSemester)
.limit(1)
)
.scalars()
.first()
)
if not dbSemesterExperiment:
if dbSemesterExperiment is None:
raise DataBaseImportException(
f"No semester experiment exists in the database to the experiment with number {experimentNumber} and program {experimentProgram.repr()}. Please make sure that semester experiments are created in the web interface. The problem might be that the experiment was not active while creating a new semester"
)
@ -276,7 +320,11 @@ def importFromFile(filePath):
for i, date in enumerate(appointments["date"]):
date = not_nullable(date)
assistantEmail = not_nullable(appointments["assistant_email"][i])
assistant = Assistant.query.filter(Assistant.user.has(User.email == assistantEmail)).first()
assistant = (
db.session.execute(select(Assistant).join(User).where(User.email == assistantEmail).limit(1))
.scalars()
.first()
)
if assistant is None:
raise DataBaseImportException(