251 lines
9.4 KiB
Python
251 lines
9.4 KiB
Python
from datetime import date
|
|
import time
|
|
from rest_framework.response import Response
|
|
from rest_framework.decorators import api_view
|
|
from rest_framework.views import APIView
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from pruefplan_viewer.models import *
|
|
from .serializer import *
|
|
|
|
from django.contrib.auth import authenticate, login, logout
|
|
from django.shortcuts import render
|
|
from api import serializer as api_serializer
|
|
from rest_framework_simplejwt.views import TokenObtainPairView
|
|
from django.contrib.auth.models import User
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.utils.encoding import force_str
|
|
|
|
|
|
# Create a REST API view class to interact with the custom token serializer class
|
|
class MyTokenObtainPairView(TokenObtainPairView):
|
|
# Setup the serializer -> using custom serializer
|
|
serializer_class = api_serializer.MyTokenObtainPairSerializer
|
|
|
|
# Func to handle a post request on this API endpoint
|
|
def post(self, request, *args, **kwargs):
|
|
username = request.data.get("username")
|
|
password = request.data.get("password")
|
|
|
|
# User authentication using LADP agains the TH-Server -> Setup LADP in settings.py
|
|
user = authenticate(username=username, password=password)
|
|
if not user:
|
|
return Response({"error": "Invalid credentials"}, status=400)
|
|
|
|
# Login the user for Backend authentication
|
|
login(request=request, user=user)
|
|
|
|
# Delegate the post request to the TokenObptainPairView class
|
|
# Executing the standart jwt creation workflow -> using the custom serializer
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
@api_view(["POST"])
|
|
def logoutUser(request):
|
|
logout(request)
|
|
return Response()
|
|
|
|
|
|
# Extract the location key and map it to a URL
|
|
def map_location(location_str):
|
|
URL_MAP = {
|
|
"B": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/b-standort-bahnhofstrasse/",
|
|
"F": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/f-standort-auf-aeg/",
|
|
"H": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/h-standort-hohfederstrasse/",
|
|
"K": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/k-standort-kesslerplatz/",
|
|
"NM": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/nm-standort-neumarkt-in-der-oberpfalz/",
|
|
"SC": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/sc-standort-innere-cramer-klett-strasse/",
|
|
"SG": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/sg-standort-karl-grillenberger-strasse/",
|
|
"SK": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/sk-standort-prof-ernst-nathan-strasse/",
|
|
"SP": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/sp-standort-kesslerstrasse/",
|
|
"SR": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/sr-standort-rednitzhembach/",
|
|
"W": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/w-standort-wassertorstrasse/",
|
|
"OL": "Online",
|
|
"MC": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/pruefungsstandort-mensa-insel-schuett/",
|
|
"Default": "https://www.th-nuernberg.de/wie-erreichen-sie-uns/anfahrt/",
|
|
}
|
|
if not location_str:
|
|
return URL_MAP["Default"] # Fallback URL if location is empty or None
|
|
|
|
location_key = location_str.split(".")[0] # Get the first part before '.'
|
|
|
|
# Find the matching key
|
|
for key in URL_MAP.keys():
|
|
if location_key.startswith(key):
|
|
return URL_MAP[key]
|
|
|
|
return URL_MAP["Default"] # If no match is found, return the default URL
|
|
|
|
|
|
def get_Subject(exam):
|
|
partialExam = PartialExam.objects.filter(identification=exam).first()
|
|
subjects = partialExam.subjectIds
|
|
result = [subject.name for subject in subjects]
|
|
pass
|
|
|
|
|
|
class PruefPlanUploader(APIView):
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
def post(self, request):
|
|
timeStart = time.perf_counter()
|
|
JsonParser().clearTable()
|
|
dataSets = [dataSet for dataSet in request.data]
|
|
|
|
serializers = [
|
|
Lecturer_JSON_Serializer(data=dataSets[0]),
|
|
Attendance_JSON_Serializer(data=dataSets[1]),
|
|
Subject_JSON_Serializer(data=dataSets[2]),
|
|
Exam_JSON_Serializer(data=dataSets[3]),
|
|
RoomAllocation_JSON_Serializer(data=dataSets[4]),
|
|
]
|
|
|
|
respondList = [
|
|
serializer.save() for serializer in serializers if serializer.is_valid()
|
|
]
|
|
|
|
partial_delta = generate_ExamDataSet_ForStudent()
|
|
timeEnd = time.perf_counter()
|
|
|
|
total_delta = timeEnd - timeStart
|
|
respondList.append({"Uploadtime_in_Sekonds": {round(total_delta, ndigits=2)}})
|
|
respondList.append({"ExamDataSettime_in_Sekonds": {round(partial_delta, ndigits=2)}})
|
|
|
|
return Response(respondList)
|
|
|
|
|
|
@api_view(["GET"])
|
|
def getAPI_ExamDataSet_ForStudent(request):
|
|
exam_data = API_ExamDataSet_ForStudent.objects.all()
|
|
|
|
# Serialize the data using the ModelSerializer
|
|
serializer = API_ExamDataSet_ForStudentSerializer(exam_data, many=True)
|
|
|
|
return Response(serializer.data) # This will return the data as JSON
|
|
|
|
|
|
@api_view(["POST"])
|
|
def pruefplanUpload(request):
|
|
|
|
timeStart = time.perf_counter()
|
|
dataSets = [dataSet for dataSet in request.data]
|
|
|
|
serializers = [
|
|
Lecturer_JSON_Serializer(data=dataSets[0]),
|
|
Attendance_JSON_Serializer(data=dataSets[1]),
|
|
Subject_JSON_Serializer(data=dataSets[2]),
|
|
Exam_JSON_Serializer(data=dataSets[3]),
|
|
RoomAllocation_JSON_Serializer(data=dataSets[4]),
|
|
]
|
|
respondList = [
|
|
serializer.save() for serializer in serializers if serializer.is_valid()
|
|
]
|
|
|
|
generate_ExamDataSet_ForStudent()
|
|
timeEnd = time.perf_counter()
|
|
|
|
delta = timeEnd - timeStart
|
|
print(f"Time for Upload: {round(delta)}s")
|
|
respondList.append(delta)
|
|
|
|
return Response(respondList)
|
|
|
|
|
|
def generate_ExamDataSet_ForStudent():
|
|
timeStart = time.perf_counter()
|
|
students = Student.objects.all()
|
|
|
|
for student in students:
|
|
for studentExam in student.exams.all():
|
|
|
|
examDataSet_ForStudent = API_ExamDataSet_ForStudent(
|
|
date=studentExam.date,
|
|
time=studentExam.time,
|
|
weekday=studentExam.weekday,
|
|
room=studentExam.location,
|
|
location=map_location(studentExam.location),
|
|
subjectIdent=list(
|
|
{
|
|
subject.name
|
|
for subject in studentExam.examExecution.partialExam.subjectIds.all()
|
|
}
|
|
)[0],
|
|
examIdent=studentExam.examIdentification,
|
|
examStudCount=studentExam.examExecution.partialExam.regStudCount,
|
|
supervisor=f"{studentExam.examExecution.lecturer.firstName} {studentExam.examExecution.lecturer.lastName}",
|
|
studMartNr=student.matrikel,
|
|
)
|
|
examDataSet_ForStudent.save()
|
|
|
|
timeEnd = time.perf_counter()
|
|
delta = timeEnd - timeStart
|
|
return delta
|
|
|
|
|
|
@api_view(["POST"])
|
|
def checkLoginStatus(request):
|
|
username = request.data["username"]
|
|
if User.objects.filter(username=username).exists():
|
|
usr = User.objects.get(username=username)
|
|
if usr.is_authenticated:
|
|
return Response(data="IsLoggedin")
|
|
else:
|
|
return Response(data="IsNotLoggedin")
|
|
else:
|
|
return Response(data="No user exist with this {} username.".format(username))
|
|
|
|
|
|
@api_view(["POST"])
|
|
def addLecturer(request):
|
|
serializer = Lecturer_JSON_Serializer(data=request.data)
|
|
if serializer.is_valid():
|
|
Lecturer_JASON_Serializer_Respond = (
|
|
serializer.save()
|
|
) # Hier wird der Rückgabewert von create() erfasst
|
|
# Erstelle eine Response basierend auf den erstellten Lecturer-Objekten
|
|
return Response(Lecturer_JASON_Serializer_Respond)
|
|
|
|
|
|
@api_view(["POST"])
|
|
def addAttendance(request):
|
|
serializer = Attendance_JSON_Serializer(data=request.data)
|
|
if serializer.is_valid():
|
|
Attendance_JSON_Serializer_Respond = (
|
|
serializer.save()
|
|
) # Hier wird der Rückgabewert von create() erfasst
|
|
# Erstelle eine Response basierend auf den erstellten Lecturer-Objekten
|
|
return Response(Attendance_JSON_Serializer_Respond)
|
|
|
|
|
|
@api_view(["POST"])
|
|
def addSubject(request):
|
|
serializer = Subject_JSON_Serializer(data=request.data)
|
|
if serializer.is_valid():
|
|
Subject_JASON_Serializer_Respond = (
|
|
serializer.save()
|
|
) # Hier wird der Rückgabewert von create() erfasst
|
|
# Erstelle eine Response basierend auf den erstellten Lecturer-Objekten
|
|
return Response(Subject_JASON_Serializer_Respond)
|
|
|
|
|
|
@api_view(["POST"])
|
|
def addExam(request):
|
|
serializer = Exam_JSON_Serializer(data=request.data)
|
|
if serializer.is_valid():
|
|
Exam_JASON_Serializer_Respond = (
|
|
serializer.save()
|
|
) # Hier wird der Rückgabewert von create() erfasst
|
|
# Erstelle eine Response basierend auf den erstellten Lecturer-Objekten
|
|
return Response(Exam_JASON_Serializer_Respond)
|
|
|
|
|
|
@api_view(["POST"])
|
|
def addRoomAllocation(request):
|
|
serializer = RoomAllocation_JSON_Serializer(data=request.data)
|
|
if serializer.is_valid():
|
|
RoomAllocation_JASON_Serializer_Respond = (
|
|
serializer.save()
|
|
) # Hier wird der Rückgabewert von create() erfasst
|
|
# Erstelle eine Response basierend auf den erstellten Lecturer-Objekten
|
|
return Response(RoomAllocation_JASON_Serializer_Respond)
|