2025-03-24 14:34:56 +01:00

455 lines
19 KiB
Python

import inspect
from django.db import models
from medinf.settings import (
PP_EXAM_FILE,
PP_ATTENDANCE_FILE,
PP_STUDENT_FILE,
PP_SUBJECT_FILE,
PP_TEACHER_FILE,
)
from pruefplan_viewer.models import (
API_ExamDataSet_ForStudent,
Attendance,
Subject,
Exam,
Lecturer,
PartialExam,
ExamExecution,
Student,
StudentExam,
)
import json
import datetime
class JsonParser(models.Model):
exceptionMessages = []
warningMessages = []
def clearTable(self):
JsonParser.exceptionMessages.clear()
JsonParser.warningMessages.clear()
Subject.objects.all().delete()
Exam.objects.all().delete()
Attendance.objects.all().delete()
Lecturer.objects.all().delete()
Student.objects.all().delete()
PartialExam.objects.all().delete()
ExamExecution.objects.all().delete()
StudentExam.objects.all().delete()
API_ExamDataSet_ForStudent.objects.all().delete()
def fillLecturerTable(self):
idKey = "kennung"
firstNameKey = "vorname"
lastNameKey = "nachname"
titleKey = "titel"
try:
with open(PP_TEACHER_FILE, encoding="utf-8") as json_file:
data = json.load(json_file)
revision = data["revision"] # TODO -> Do Something with Revision
for d in data["array"]:
entry = Lecturer(
identification=d[idKey],
firstName=d[firstNameKey],
lastName=d[lastNameKey],
title=d[titleKey],
) # Create new Lecturer entry
entry.save()
except FileNotFoundError as e:
JsonParser.exceptionMessages.append(
"Exception of type [FileNotFound]. Message: " + str(e)
)
except KeyError as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop.. Arguments: {3!r}. Message: [{4}] entry might be missing in {5!r}"
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e,
PP_TEACHER_FILE,
)
JsonParser.exceptionMessages.append(message)
except Exception as e:
calling_function = inspect.stack()[0][3]
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop. Arguments: {3!r}. Message: {4}."
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e.__str__(),
)
JsonParser.exceptionMessages.append(message)
def fillAttendanceTable(self):
idKey = "dozentenkennung"
yearKey = "jahr"
monthKey = "monat"
dayKey = "tag"
hourKey = "stunde"
minuteKey = "minute"
weekdayKey = "wochentag"
dateKey = "termin"
try:
with open(PP_ATTENDANCE_FILE, encoding="utf-8") as json_file:
data = json.load(json_file)
revision = data["revision"] # TODO -> Do Something with Revision
for d in data["array"]:
keyAvailable = "termin" in d
if (
not keyAvailable
): # These attendance entries were not assigned any examination date
template = "Warning. Location: {0!r}. Following Exam was not assigned any examination date ['termin']: [{1}]"
message = template.format(PP_EXAM_FILE, d[idKey])
JsonParser.warningMessages.append(message)
continue
date = datetime.date(
d[dateKey][yearKey], d[dateKey][monthKey], d[dateKey][dayKey]
)
time = datetime.time(d[dateKey][hourKey], d[dateKey][minuteKey])
entry = Attendance(
identification=d[idKey],
time=time,
date=date,
dateTimeText=d[dateKey]["datumText"],
weekday=d[dateKey]["wochentag"],
) # Create new Lecturer entry
entry.save()
try:
# Get the Lecturer with the "Dozentenkennung" of this attendance entry
lecturer = Lecturer.objects.get(identification=d[idKey])
# Link this attendance entry to the lecturer
lecturer.attendance_set.add(entry)
except Exception as e:
template = "Exception of type [{0}]. Arguments: {1!r}. Message: [{2}] entry might be missing in {3!r}"
message = template.format(
type(e).__name__, e.args, idKey, PP_TEACHER_FILE
)
JsonParser.exceptionMessages.append(message)
except FileNotFoundError as e:
JsonParser.exceptionMessages.append(
"Exception of type [FileNotFound]. Message: " + str(e)
)
except KeyError as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop.. Arguments: {3!r}. Message: [{4}] entry might be missing in {5!r}"
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e,
PP_ATTENDANCE_FILE,
)
JsonParser.exceptionMessages.append(message)
except Exception as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop. Arguments: {3!r}. Message: {4}."
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e.__str__(),
)
JsonParser.exceptionMessages.append(message)
def fillSubjectTable(self):
idKey = "kennung"
initialsKey = "kurztext"
nameKey = "langtext"
ohmIdKey = "fachkennungOhm"
try:
with open(PP_SUBJECT_FILE, encoding="utf-8") as json_file:
data = json.load(json_file)
revision = data["revision"] # TODO -> Do Something with Revision
for d in data["array"]:
entry = Subject(
identification=d[idKey],
initials=d[ohmIdKey][initialsKey],
name=d[ohmIdKey][nameKey],
) # Create new Subject entry
entry.save()
except FileNotFoundError as e:
JsonParser.exceptionMessages.append(
"Exception of type [FileNotFound]. Message:" + str(e)
)
except KeyError as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop.. Arguments: {3!r}. Message: [{4}] entry might be missing in {5!r}"
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e,
PP_SUBJECT_FILE,
)
JsonParser.exceptionMessages.append(message)
except Exception as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop. Arguments: {3!r}. Message: {4}."
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e.__str__(),
)
JsonParser.exceptionMessages.append(message)
def fillExamTable(self):
idKey = "kennung"
yearKey = "jahr"
monthKey = "monat"
dayKey = "tag"
hourKey = "stunde"
minuteKey = "minute"
weekdayKey = "wochentag"
dateKey = "termin"
partialExamsKey = "teilpruefungen"
partialExamIdKey = "kennung"
numRegStudKey = "anzahlAnmeldungen"
subjectIdsKey = "fachkennungen"
examExecutionsKey = "pruefungsdurchfuehrungen"
lecturerIdKey = "dozentenkennung"
supervisorTypeKey = "typ"
locationKey = "raum"
try:
with open(PP_EXAM_FILE, encoding="utf-8") as json_file:
data = json.load(json_file)
revision = data["revision"] # TODO -> Do Something with Revision
for d in data["array"]:
if (
d["pruefungsart"] == "LN_VORGEZOGEN"
): # These entries can be ignored
continue
keyAvailable = "termin" in d
if (
not keyAvailable
): # These exam entries were not assigned any examination date
template = "Warning. Location: {0!r}. Following Exam was not assigned any examination date ['termin']: [{1}]"
message = template.format(PP_EXAM_FILE, d[idKey])
JsonParser.warningMessages.append(message)
continue
date = datetime.date(
d[dateKey][yearKey], d[dateKey][monthKey], d[dateKey][dayKey]
)
time = datetime.time(d[dateKey][hourKey], d[dateKey][minuteKey])
examEntry = Exam(
identification=d[idKey],
date=date,
time=time,
weekday=d[dateKey][weekdayKey],
) # Create new Exam entry
examEntry.save()
for teilpruef in d[partialExamsKey]:
partialExamId = teilpruef[partialExamIdKey]
regStudCount = teilpruef[numRegStudKey]
partialExam = PartialExam(
identification=partialExamId, regStudCount=regStudCount
) # Create new Partialexam entry
partialExam.save()
examEntry.partialexam_set.add(
partialExam
) # Add connection Exam(One) to PartialExam(Many)
for subId in teilpruef[subjectIdsKey]:
# check if subject exists
try:
subject = Subject.objects.get(identification=subId)
partialExam.subjectIds.add(
subject
) # Add connection PartialExam(Many) to Subject(Many)
except Exception as e:
template = "Exception of type [{0}]. Arguments: {1!r}. Message: [{2}] entry might be missing in {3!r}"
message = template.format(
type(e).__name__, e.args, subId, PP_SUBJECT_FILE
)
JsonParser.exceptionMessages.append(message)
for pruefdurch in teilpruef[examExecutionsKey]:
lecId = pruefdurch[lecturerIdKey]
supervisorType = pruefdurch[supervisorTypeKey]
location = pruefdurch[locationKey]
examExecution = ExamExecution(
supervisorType=supervisorType, location=location
) # Create new ExamExecution entry
examExecution.save()
# check if lecturer exists
try:
lecturer = Lecturer.objects.get(pk=lecId)
lecturer.examexecution_set.add(
examExecution
) # Add connection Lecturer (One) to ExamExecution (Many)
except Exception as e:
template = "Exception of type [{0}]. Arguments: {1!r}. Message: [{2}] entry might be missing in {3!r}"
message = template.format(
type(e).__name__, e.args, lecId, PP_TEACHER_FILE
)
JsonParser.exceptionMessages.append(message)
for subId in pruefdurch[subjectIdsKey]:
# check if subject exists
try:
subject = Subject.objects.get(identification=subId)
examExecution.subjectIds.add(
subject
) # Add connection ExamExecution(Many) to Subject(Many)
except Exception as e:
template = "Exception of type [{0}]. Arguments: {1!r}. Message: [{2}] entry might be missing in {3!r}"
message = template.format(
type(e).__name__, e.args, subId, PP_SUBJECT_FILE
)
JsonParser.exceptionMessages.append(message)
partialExam.examexecution_set.add(
examExecution
) # Add connection PartialExam(One) to ExamExecution(Many)
except FileNotFoundError as e:
JsonParser.exceptionMessages.append(
"Exception of type [FileNotFound]. Message" + str(e)
)
except KeyError as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop.. Arguments: {3!r}. Message: [{4}] entry might be missing in {5!r}"
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e,
PP_EXAM_FILE,
)
JsonParser.exceptionMessages.append(message)
except Exception as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop. Arguments: {3!r}. Message: {4}."
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e.__str__(),
)
JsonParser.exceptionMessages.append(message)
def fillStudentExamTable(self):
idKey = "kennungPruefung"
yearKey = "jahr"
monthKey = "monat"
dayKey = "tag"
hourKey = "stunde"
minuteKey = "minute"
weekdayKey = "wochentag"
locationKey = "raum"
dateKey = "termin"
matrikelKey = "matrikel"
try:
with open(PP_STUDENT_FILE, encoding="utf-8") as json_file:
data = json.load(json_file)
revision = data["revision"] # TODO -> Do Something with Revision
for d in data["array"]:
date = datetime.date(
d[dateKey][yearKey], d[dateKey][monthKey], d[dateKey][dayKey]
)
time = datetime.time(d[dateKey][hourKey], d[dateKey][minuteKey])
exam = Exam.objects.filter(
identification=d[idKey], date=date, time=time
).first()
partialExams = exam.partialexam_set.all()
for partialExam in partialExams:
examExecutions = partialExam.examexecution_set.all()
for examExecution in examExecutions:
if examExecution.location == d[locationKey]:
studentExamEntry = StudentExam(
examIdentification=d[idKey],
date=date,
time=time,
weekday=d[dateKey][weekdayKey],
location=d[locationKey],
examExecution=examExecution,
) # Create new StudentExam entry
studentExamEntry.save()
for matrikel in d[matrikelKey]:
studentEntry = Student.objects.filter(
matrikel=matrikel
).exists()
if (
studentEntry is False
): # Create new Student entry if does not already exists
entry = Student(matrikel=matrikel)
entry.save()
else:
entry = (
Student.objects.all().filter(matrikel=matrikel).first()
)
entry.exams.add(studentExamEntry)
except FileNotFoundError as e:
JsonParser.exceptionMessages.append(
"Exception of type [FileNotFound]. Message:" + str(e)
)
except KeyError as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop.. Arguments: {3!r}. Message: [{4}] entry might be missing in {5!r}"
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e,
PP_STUDENT_FILE,
)
JsonParser.exceptionMessages.append(message)
except Exception as e:
calling_function = inspect.stack()[0][3]
iteration_number = data["array"].index(d)
template = "Exception of type [{0}] occurred in function {1} in iteration {2} of the loop. Arguments: {3!r}. Message: {4}."
message = template.format(
type(e).__name__,
calling_function,
iteration_number,
e.args,
e.__str__(),
)
JsonParser.exceptionMessages.append(message)