diff --git a/src/back/tests/views.py b/src/back/tests/views.py index 8bf8ad16c..71dbee719 100644 --- a/src/back/tests/views.py +++ b/src/back/tests/views.py @@ -69,12 +69,14 @@ class GetTests(generics.ListAPIView): status_filters = { 'active' : Test.objects.filter( testing__user=user, - testing__completion_date__gte=now + testing__completion_date__gte=now, + testing__is_active=True ).distinct(), 'completed' : Test.objects.filter( testing__user=user, - testing__completion_date__lt=now + testing__completion_date__lt=now, + testing__is_active=True ).distinct() } return status_filters.get(status, Test.objects.none()) @@ -82,7 +84,8 @@ class GetTests(generics.ListAPIView): def set_attemps_number(self, user, tests): """Updates the number of attempts remaining for each test for the user.""" attempt_counts = TestAttempt.objects.filter(testing__user=user, - testing__test__in=tests + testing__test__in=tests, + testing__is_active=True ).values('testing__test' ).annotate(count=Count('testing__test')) attempt_counts = {item['testing__test']: item['count'] for item in attempt_counts} @@ -362,12 +365,12 @@ class TestResultView(generics.ListCreateAPIView): answer_ids = [item['answer'] for item in request.data] test_info = self.get_test_info(answer_ids) if status == 'testing': - test_attempt = self.create_test_attempt(user, test_info) + test_attempt, testing = self.create_test_attempt(user, test_info) serializer = self.get_serializer(data=request.data, many=True) if serializer.is_valid(): serializer.save(attempt=test_attempt) - self.update_testing(user, test_info) + self.update_testing(user, test_info, testing) return Response(data={'score': test_info['score'], 'answered_questions_total': test_info['answered_questions_total'], @@ -377,21 +380,23 @@ class TestResultView(generics.ListCreateAPIView): """Creates a new test attempt for the user.""" test = test_info['test'] score = test_info['score'] - testing = Testing.objects.get(user=user, test=test) + testing = Testing.objects.get(user=user, test=test, is_active=True) attempt_count = get_attempt_count(testing)+ 1 test_attempt = TestAttempt.objects.create(testing=testing, score=score, attempt_number=attempt_count, completion_date= datetime.now()) - return test_attempt + return test_attempt, testing - def update_testing(self, user, test_info): + def update_testing(self, user, test_info, testing): """Updates or creates a Testing record with a test passing flag.""" test = test_info['test'] test_passed = test_info['is_passed'] - Testing.objects.update_or_create(user=user, - test=test, - defaults = {"test_passed":test_passed}) + testing.test_passed = test_passed + testing.save() + # Testing.objects.update_or_create(user=user, + # test=test, + # defaults = {"test_passed":test_passed}) def get_test_info(self, answer_ids): """Calculates test results from a list of answer IDs."""