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

Apply rules to import

This commit is contained in:
Mo 2022-09-08 01:37:06 +02:00
parent 93fef08b4c
commit 5d3a8023df

View file

@ -174,29 +174,61 @@ def importFromFile(filePath: Path):
flash("Student...") flash("Student...")
dbStudents = {} dbStudents = {}
for i, studentNumber in enumerate(students["student_number"]): for i, student_number in enumerate(students["student_number"]):
studentNumber = int(not_nullable(studentNumber)) student_number = int(not_nullable(student_number))
dbStudent = get_first(select(Student).where(Student.student_number == studentNumber)) first_name = not_nullable(students["first_name"][i])
last_name = not_nullable(students["last_name"][i])
uni_email = not_nullable(students["uni_email"][i])
contact_email = nullable(students["contact_email"][i])
bachelor_thesis = nullable(students["bachelor_thesis"][i])
bachelor_thesis_work_group = nullable(students["bachelor_thesis_work_group"][i])
note = nullable(students["note"][i])
dbStudent = get_first(select(Student).where(Student.student_number == student_number))
if dbStudent is None: if dbStudent is None:
dbStudent = Student( dbStudent = Student(
student_number=studentNumber, student_number=student_number,
first_name=not_nullable(students["first_name"][i]), first_name=first_name,
last_name=not_nullable(students["last_name"][i]), last_name=last_name,
uni_email=not_nullable(students["uni_email"][i]), uni_email=uni_email,
contact_email=nullable(students["contact_email"][i]), contact_email=contact_email,
bachelor_thesis=nullable(students["bachelor_thesis"][i]), bachelor_thesis=bachelor_thesis,
bachelor_thesis_work_group=nullable(students["bachelor_thesis_work_group"][i]), bachelor_thesis_work_group=bachelor_thesis_work_group,
note=nullable(students["note"][i]), note=note,
) )
db.session.add(dbStudent) db.session.add(dbStudent)
else: else:
dbStudent.contact_email = nullable(students["contact_email"][i]) # Check if columns that should not change match
dbStudent.bachelor_thesis = nullable(students["bachelor_thesis"][i]) if dbStudent.first_name != first_name:
dbStudent.bachelor_thesis_work_group = nullable(students["bachelor_thesis_work_group"][i]) raise DataBaseImportException(
dbStudent.note = nullable(students["note"][i]) f"First name {dbStudent.first_name} in the database does not match with the first name {first_name} provided in the import file for the student number {student_number}. In case the name is changed, please change it manually using the web interface first."
)
if dbStudent.last_name != last_name:
raise DataBaseImportException(
f"Last name {dbStudent.last_name} in the database does not match with the last name {last_name} provided in the import file for the student number {student_number}. In case the name is changed, please change it manually using the web interface first."
)
if dbStudent.uni_email != uni_email:
raise DataBaseImportException(
f"University email {dbStudent.uni_email} in the database does not match with the university email {last_name} provided in the import file for the student number {student_number}. In case the email is changed, please change it manually using the web interface first."
)
dbStudents[studentNumber] = dbStudent dbStudent.contact_email = contact_email
# Only overwrite if set
if bachelor_thesis is not None:
dbStudent.bachelor_thesis = bachelor_thesis
if bachelor_thesis_work_group is not None:
dbStudent.bachelor_thesis_work_group = bachelor_thesis_work_group
# Append to note instead of overwriting
if note is not None:
if dbStudent.note is None:
dbStudent.note = note
dbStudent.note += "\n" + note
dbStudents[student_number] = dbStudent
# Group # Group
flash("Group...") flash("Group...")
@ -216,10 +248,10 @@ def importFromFile(filePath: Path):
# PartStudent # PartStudent
flash("PartStudent...") flash("PartStudent...")
for i, studentNumber in enumerate(partStudents["student_number"]): for i, student_number in enumerate(partStudents["student_number"]):
studentNumber = int(not_nullable(studentNumber)) student_number = int(not_nullable(student_number))
dbPartStudent = PartStudent( dbPartStudent = PartStudent(
student=dbStudents[studentNumber], student=dbStudents[student_number],
part=dbParts[int(not_nullable(partStudents["part_id"][i]))], part=dbParts[int(not_nullable(partStudents["part_id"][i]))],
group=dbGroups[int(not_nullable(partStudents["group_id"][i]))], group=dbGroups[int(not_nullable(partStudents["group_id"][i]))],
) )