возврат каталога(все тесты)
This commit is contained in:
1 parent
aa156ce216
commit
de682a9542
2 files changed
+35
-30
No files matched your search
@@ -4,8 +4,9 @@ from .views import *
|
||||
|
||||
urlpatterns = [
|
||||
path('testdetail/<int:id>/', TestDetail.as_view(),name='TestDetail'),
|
||||
path('testlistcreate/', TestListCreate.as_view(), name="TestListCreate"),
|
||||
# path('testlistcreate/', TestListCreate.as_view(), name="TestListCreate"),
|
||||
path('gettesting/', GetTesting.as_view(), name="GetTesting"),
|
||||
path('testresultcreate/', TestResultCreate.as_view(), name="TestResultCreate"),
|
||||
path('testlist/', TestList.as_view(), name="TestList"),
|
||||
]
|
||||
|
||||
+33
-29
@@ -20,6 +20,10 @@ import hashlib
|
||||
from cryptography.fernet import Fernet
|
||||
from django.db.models import F
|
||||
|
||||
class TestList(generics.ListAPIView):
|
||||
queryset = Test.objects.all()
|
||||
serializer_class = TestSerializer
|
||||
|
||||
class TestDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
queryset = Test.objects.all()
|
||||
serializer_class = TestSerializer
|
||||
@@ -35,43 +39,43 @@ class TestDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
|
||||
return Response(test_serialized)
|
||||
|
||||
class TestListCreate(generics.ListCreateAPIView):
|
||||
queryset = Test.objects.all()
|
||||
serializer_class = TestSerializer
|
||||
# class TestListCreate(generics.ListCreateAPIView):
|
||||
# queryset = Test.objects.all()
|
||||
# serializer_class = TestSerializer
|
||||
|
||||
def get(self, request):
|
||||
user = request.user
|
||||
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)
|
||||
# def get(self, request):
|
||||
# user = request.user
|
||||
# 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)
|
||||
# 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]
|
||||
# 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)
|
||||
# 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')
|
||||
# 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),
|
||||
}
|
||||
# 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')
|
||||
# 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()
|
||||
# def has_participated(self, employee, test):
|
||||
# return TestResult.objects.filter(user=employee, answer__question__test=test).exists()
|
||||
|
||||
class GetTesting(APIView):
|
||||
|
||||
|
||||
Reference in new issue
Block a user