Merge remote-tracking branch 'origin/сja' into Alex
This commit is contained in:
commit
17343eaf45
2 files changed
+35
-30
No files matched your search
@@ -4,8 +4,9 @@ from .views import *
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('testdetail/<int:id>/', TestDetail.as_view(),name='TestDetail'),
|
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('gettesting/', GetTesting.as_view(), name="GetTesting"),
|
||||||
path('testresultcreate/', TestResultCreate.as_view(), name="TestResultCreate"),
|
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 cryptography.fernet import Fernet
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
|
|
||||||
|
class TestList(generics.ListAPIView):
|
||||||
|
queryset = Test.objects.all()
|
||||||
|
serializer_class = TestSerializer
|
||||||
|
|
||||||
class TestDetail(generics.RetrieveUpdateDestroyAPIView):
|
class TestDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||||
queryset = Test.objects.all()
|
queryset = Test.objects.all()
|
||||||
serializer_class = TestSerializer
|
serializer_class = TestSerializer
|
||||||
@@ -35,43 +39,43 @@ class TestDetail(generics.RetrieveUpdateDestroyAPIView):
|
|||||||
|
|
||||||
return Response(test_serialized)
|
return Response(test_serialized)
|
||||||
|
|
||||||
class TestListCreate(generics.ListCreateAPIView):
|
# class TestListCreate(generics.ListCreateAPIView):
|
||||||
queryset = Test.objects.all()
|
# queryset = Test.objects.all()
|
||||||
serializer_class = TestSerializer
|
# serializer_class = TestSerializer
|
||||||
|
|
||||||
def get(self, request):
|
# def get(self, request):
|
||||||
user = request.user
|
# user = request.user
|
||||||
now = datetime.now()
|
# now = datetime.now()
|
||||||
status = request.query_params.get('status')
|
# status = request.query_params.get('status')
|
||||||
tests = self.get_tests_by_status(status, now)
|
# tests = self.get_tests_by_status(status, now)
|
||||||
serialized_tests = TestSerializer(tests, many=True).data
|
# serialized_tests = TestSerializer(tests, many=True).data
|
||||||
questions = TestQuestion.objects.filter(test__in=tests)
|
# questions = TestQuestion.objects.filter(test__in=tests)
|
||||||
|
|
||||||
questions_by_test_id = defaultdict(list)
|
# questions_by_test_id = defaultdict(list)
|
||||||
for question in questions:
|
# for question in questions:
|
||||||
questions_by_test_id[question.test.id].append(question)
|
# questions_by_test_id[question.test.id].append(question)
|
||||||
|
|
||||||
for test in serialized_tests:
|
# for test in serialized_tests:
|
||||||
test['is_passed'] = self.has_participated(user, test['id'])
|
# test['is_passed'] = self.has_participated(user, test['id'])
|
||||||
related_questions = questions_by_test_id[test['id']]
|
# related_questions = questions_by_test_id[test['id']]
|
||||||
test['files'] = [question.file.url for question in related_questions if question.file]
|
# 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):
|
# def get_tests_by_status(self, status, now):
|
||||||
print(status)
|
# print(status)
|
||||||
if status is None:
|
# if status is None:
|
||||||
return Test.objects.all().order_by('dateend')
|
# return Test.objects.all().order_by('dateend')
|
||||||
|
|
||||||
status_filters = {
|
# status_filters = {
|
||||||
'active': Test.objects.filter(dateend__gt=now),
|
# 'active': Test.objects.filter(dateend__gt=now),
|
||||||
'completed': Test.objects.filter(dateend__lt=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):
|
# def has_participated(self, employee, test):
|
||||||
return TestResult.objects.filter(user=employee, answer__question__test=test).exists()
|
# return TestResult.objects.filter(user=employee, answer__question__test=test).exists()
|
||||||
|
|
||||||
class GetTesting(APIView):
|
class GetTesting(APIView):
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user