TestCreate

This commit is contained in:
cja committed 2025-05-15 16:39:13 +03:00
1 parent b64cdb89cd
commit ec0a3d37f8
2 files changed
+90 -3

No files matched your search

+2 -2
View File
@@ -3,9 +3,9 @@ from .views import *
urlpatterns = [ urlpatterns = [
path('testdetail/<int:id>/', TestDetail.as_view(),name='TestDetail'),
path('gettests/', GetTests.as_view(), name="GetTests"), path('gettests/', GetTests.as_view(), name="GetTests"),
path('testdetail/<int:id>/', TestDetail.as_view(),name='TestDetail'),
path('testcreate/', TestCreate.as_view(),name='TestCreate'),
# path('gettesting/', GetTesting.as_view(), name="GetTesting"), # path('gettesting/', GetTesting.as_view(), name="GetTesting"),
path('testresultview/', TestResultView.as_view(), name="TestResultView"), path('testresultview/', TestResultView.as_view(), name="TestResultView"),
] ]
+88 -1
View File
@@ -8,6 +8,7 @@ from .serializers import *
from datetime import datetime from datetime import datetime
from rest_framework import generics from rest_framework import generics
from navigation.models import News from navigation.models import News
from django.db import DatabaseError
import json import json
from collections import defaultdict from collections import defaultdict
import os import os
@@ -96,6 +97,92 @@ class TestDetail(generics.RetrieveUpdateDestroyAPIView):
return Response(test_serialized) return Response(test_serialized)
class TestCreate(APIView):
"""
create test
"""
def post(self, request):
"""
create test
"""
try:
test = self.create_test(request)
return Response(
{"status": "success", "message": "Test created successfully."},
status=200,
)
except ValueError as e:
return Response({"status": "error", "message": str(e)}, status=400)
except DatabaseError as e:
print(e)
return Response({"status": "error", "message": str(e)}, status=400)
def create_test(self, request):
"""
create test
"""
user = request.user
test_image = (
request.data.get["image"]["name"]
if "image" in request.data and request.data["image"] is not None
else None
)
test = Test.objects.create(
file=test_image,
name=request.data["name"],
description=request.data["description"],
datestart=request.data["datestart"],
dateend=request.data["dateend"],
author=user,
pass_perc=request.data["pass_perc"],
time=request.data["time"],
question_count=request.data["question_count"],
attemps_number=request.data["attemps_number"],
)
questions_files = {
file.name: file for file in request.FILES.getlist("questionsfiles")
}
answers_files = {
file.name: file for file in request.FILES.getlist("answersfiles")
}
questions = json.loads(request.data["questions"])
for count, question_data in enumerate(questions, start=1):
question_image = (
questions_files.get(question_data["image"]["name"])
if "image" in question_data and question_data["image"] is not None
else None
)
question = TestQuestion.objects.create(
name=question_data["text"],
test=test,
file=question_image,
numb=count,
settings=question_data.get("settings", {}),
)
for answer in question_data["answers"]:
answer_image = (
answers_files.get(answer["image"]["name"])
if "image" in answer and answer["image"] is not None
else None
)
TestAnswer.objects.create(
name=answer["text"],
file=answer_image,
question=question,
right_wrong=answer["right_wrong"],
)
return test
# class GetTesting(APIView): # class GetTesting(APIView):
# def get(self, request): # def get(self, request):
@@ -107,7 +194,7 @@ class TestDetail(generics.RetrieveUpdateDestroyAPIView):
# if test['id'] in test_names: # if test['id'] in test_names:
# test['test_name'] = test_names[test['id']] # test['test_name'] = test_names[test['id']]
# return Response(serializer) # return Response(serializer)
class TestResultView(generics.ListCreateAPIView): class TestResultView(generics.ListCreateAPIView):
queryset = TestResult.objects.all() queryset = TestResult.objects.all()