текущ/заверш тестир-ия

This commit is contained in:
Cja committed 2025-01-28 09:31:19 +03:00
1 parent 3f4ea0cef9
commit e7548c60d7
1 file changed
+36 -18
+36 -18
View File
@@ -32,30 +32,48 @@ class TestDetail(generics.RetrieveUpdateDestroyAPIView):
random_questions = test.questions.order_by('?')[:number_of_questions]
test_serialized = TestSerializer(test).data
test_serialized['questions'] = TestQuestionSerializer(random_questions, many=True).data
data = prepare_response(test_serialized, request.user.username)
return Response(data)
def generate_key(user_login):
key = hashlib.sha256(user_login.encode()).digest()
return base64.urlsafe_b64encode(key)
def encrypt_value(value, user_login):
key = generate_key(user_login)
fernet = Fernet(key)
encrypted_value = fernet.encrypt(str(value).encode())
return encrypted_value
def prepare_response(data, user_login):
for question in data['questions']:
for answer in question['answers']:
answer['right_wrong'] = encrypt_value(answer['right_wrong'], user_login)
return data
return Response(test_serialized)
class TestListCreate(generics.ListCreateAPIView):
queryset = Test.objects.all()
serializer_class = TestSerializer
def get(self, request):
user = request.user
user = User.objects.get(username='9966-00-125')
now = datetime.now()
status = request.query_params.get('status')
tests = self.get_tests_by_status(status, now)
serialized_tests = TestSerializer(tests, many=True).data
questions = TestQuestion.objects.filter(test__in=tests)
questions_by_test_id = defaultdict(list)
for question in questions:
questions_by_test_id[question.test.id].append(question)
for test in serialized_tests:
test['is_passed'] = self.has_participated(user, test['id'])
related_questions = questions_by_test_id[test['id']]
test['files'] = [question.file.url for question in related_questions if question.file]
return Response(serialized_tests)
def get_tests_by_status(self, status, now):
print(status)
if status is None:
return Test.objects.all().order_by('dateend')
status_filters = {
'active': Test.objects.filter(dateend__gt=now),
'completed': Test.objects.filter(dateend__lt=now),
}
return status_filters.get(status, Test.objects.none()).order_by('dateend')
def has_participated(self, employee, test):
return TestResult.objects.filter(user=employee, answer__question__test=test).exists()
class GetTesting(APIView):
def get(self, request):