diff --git a/advlabdb/database_import.py b/advlabdb/database_import.py index 1a01d4d..55237a3 100644 --- a/advlabdb/database_import.py +++ b/advlabdb/database_import.py @@ -174,29 +174,61 @@ def importFromFile(filePath: Path): flash("Student...") dbStudents = {} - for i, studentNumber in enumerate(students["student_number"]): - studentNumber = int(not_nullable(studentNumber)) - dbStudent = get_first(select(Student).where(Student.student_number == studentNumber)) + for i, student_number in enumerate(students["student_number"]): + student_number = int(not_nullable(student_number)) + 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: dbStudent = 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]), + student_number=student_number, + first_name=first_name, + last_name=last_name, + uni_email=uni_email, + contact_email=contact_email, + bachelor_thesis=bachelor_thesis, + bachelor_thesis_work_group=bachelor_thesis_work_group, + note=note, ) db.session.add(dbStudent) else: - dbStudent.contact_email = nullable(students["contact_email"][i]) - dbStudent.bachelor_thesis = nullable(students["bachelor_thesis"][i]) - dbStudent.bachelor_thesis_work_group = nullable(students["bachelor_thesis_work_group"][i]) - dbStudent.note = nullable(students["note"][i]) + # Check if columns that should not change match + if dbStudent.first_name != first_name: + raise DataBaseImportException( + 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 flash("Group...") @@ -216,10 +248,10 @@ def importFromFile(filePath: Path): # PartStudent flash("PartStudent...") - for i, studentNumber in enumerate(partStudents["student_number"]): - studentNumber = int(not_nullable(studentNumber)) + for i, student_number in enumerate(partStudents["student_number"]): + student_number = int(not_nullable(student_number)) dbPartStudent = PartStudent( - student=dbStudents[studentNumber], + student=dbStudents[student_number], part=dbParts[int(not_nullable(partStudents["part_id"][i]))], group=dbGroups[int(not_nullable(partStudents["group_id"][i]))], )