изменена структура бд
This commit is contained in:
1 parent
0fd243614c
commit
66531e5ac9
3 files changed
+35
-27
No files matched your search
+11
-10
@@ -56,8 +56,8 @@ class GetTests(generics.ListAPIView):
|
||||
|
||||
def set_attemps_number(self, user, tests):
|
||||
test_ids = [test.id for test in tests]
|
||||
attempt_counts = TestAttempt.objects.filter(user=user, test__id__in=test_ids).values('test').annotate(count=Count('test'))
|
||||
attempt_counts = {item['test']: item['count'] for item in attempt_counts}
|
||||
attempt_counts = TestAttempt.objects.filter(user=user, testing__test__id__in=test_ids).values('testing__test').annotate(count=Count('testing__test'))
|
||||
attempt_counts = {item['testing__test']: item['count'] for item in attempt_counts}
|
||||
|
||||
for test in tests:
|
||||
test.attemps_number = test.attemps_number - attempt_counts.get(test.id, 0)
|
||||
@@ -133,18 +133,18 @@ class TestResultCreate(generics.ListCreateAPIView):
|
||||
serializer_class = TestResultSerializer
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
user = request.user
|
||||
# user = User.objects.get(username = "9966-00-125")
|
||||
# user = request.user
|
||||
user = User.objects.get(username = "9966-00-125")
|
||||
test_id = request.GET.get('id')
|
||||
test = Testing.objects.filter(user=user, test__id=test_id).first().test
|
||||
last_attempt = TestAttempt.objects.filter(user=user, test=test).order_by('-completion_date').first()
|
||||
last_attempt = TestAttempt.objects.filter(user=user, testing__test=test).order_by('-completion_date').first()
|
||||
answer_ids = TestResult.objects.filter(attempt=last_attempt).values_list('answer__id', flat=True)
|
||||
test_info = self.get_test_info(answer_ids)
|
||||
return Response(data={'score': test_info['score'], 'answered_questions_total': test_info['answered_questions_total']})
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
user = request.user
|
||||
# user = User.objects.get(username = "9966-00-125")
|
||||
# user = request.user
|
||||
user = User.objects.get(username = "9966-00-125")
|
||||
answer_ids = [item['answer'] for item in request.data]
|
||||
test_info = self.get_test_info(answer_ids)
|
||||
test_attempt = self.create_test_attempt(user, test_info)
|
||||
@@ -160,13 +160,14 @@ class TestResultCreate(generics.ListCreateAPIView):
|
||||
test = test_info['test']
|
||||
score = test_info['score']
|
||||
attempt_count = get_attempt_count(user=user, test=test)+ 1
|
||||
test_attempt = TestAttempt.objects.create(user=user, test=test , score=score , attempt_number=attempt_count , completion_date= datetime.now())
|
||||
testing = Testing.objects.get(user=user, test=test)
|
||||
test_attempt = TestAttempt.objects.create(user=user, testing=testing , score=score , attempt_number=attempt_count , completion_date= datetime.now())
|
||||
return test_attempt
|
||||
|
||||
def update_testing(self, user, test_info):
|
||||
test = test_info['test']
|
||||
test_passed = test_info['is_passed']
|
||||
Testing.objects.update_or_create(user=user, test=test, defaults = {"completion_date": test.dateend,"test_passed":test_passed})
|
||||
Testing.objects.update_or_create(user=user, test=test, defaults = {"test_passed":test_passed})
|
||||
|
||||
def get_test_info(self, answer_ids):
|
||||
|
||||
@@ -195,4 +196,4 @@ class TestResultCreate(generics.ListCreateAPIView):
|
||||
return result
|
||||
|
||||
def get_attempt_count(user, test):
|
||||
return TestAttempt.objects.filter(user=user, test=test).count()
|
||||
return TestAttempt.objects.filter(user=user, testing__test=test).count()
|
||||
Reference in new issue
Block a user