Ejemplo n.º 1
0
 def load_data(self):
     question_dict = self.questions[self.current_question_number - 1]
     context = {
         'num_questions': self.num_questions,
         'question': utils.dict_to_object(question_dict),
         'question_status':
         utils.dict_to_object(self.get_question_status()),
         'is_last_question':
         self.num_questions == self.current_question_number,
     }
     self.context.update(context)
     self.request.session[
         'current_question_number'] = self.current_question_number
Ejemplo n.º 2
0
    def check_quiz_eligibility(self):
        """
        1. # TODO: Check if user has unlocked the quiz
        2. Check max tries
        :return:
        """
        validity = {'message': None, 'code': 0, 'is_valid': False}

        match_reference = self.db.get_document_reference(
            f'users/{self.user_id}/matches/{self.mock_id}')
        match_details = match_reference.get().to_dict()
        max_attempts = match_details.get('maxAttempts')
        user_attempts = match_details.get('numAttempts')
        if max_attempts is None:
            mock_reference = self.db.get_document_reference(
                f'mockTests/{self.mock_id}')
            max_attempts = mock_reference.get().to_dict().get('maxAttempts')
            match_reference.update({'maxAttempts': max_attempts})
        if max_attempts is not None and user_attempts is not None and int(
                user_attempts) >= int(max_attempts):
            # Check if the actual mock test has the same number of max_attempts or not.
            mock_reference = self.db.get_document_reference(
                f'mockTests/{self.mock_id}')
            max_attempts_real = mock_reference.get().to_dict().get(
                'maxAttempts')
            if int(max_attempts) == int(max_attempts_real):
                validity[
                    'message'] = "Your attempts have been reached maximum."
                validity['code'] = 2
            else:
                match_reference.update({'maxAttempts': max_attempts_real})

        validity['is_valid'] = True

        return utils.dict_to_object(validity)
Ejemplo n.º 3
0
 def get_mock_tests(self):
     mock_tests_snapshots = self.db.list_mock_tests()
     mock_tests = []
     for mock_test in mock_tests_snapshots:
         test_dict = mock_test.to_dict()
         test_dict['id'] = mock_test.id
         mock_tests.append(dict_to_object(test_dict))
     return mock_tests
Ejemplo n.º 4
0
 def get_questions(self):
     questions = self.db.get_mock_questions(self.mock_id)
     questions_list = []
     for question in questions:
         question_dict = question.to_dict()
         question_dict['id'] = question.id
         questions_list.append(dict_to_object(question_dict))
         logger.info(f"Question obtained = {question_dict}")
     return questions_list
Ejemplo n.º 5
0
 def validate_session(self):
     status = {'is_valid': False, 'message': '404', 'code': 0}
     if self.request.session.get('mock_id') is None:
         status['message'] = 'Mock ID not found'
         status['code'] = 1
     elif self.get_time_diff() > self.time_limit:
         status['message'] = "Time's up!"
         status['code'] = 2
     elif self.request.session.get('quiz_state_id') is None:
         status['message'] = 'Quiz state ID not found'
         status['code'] = 1
     else:
         status['is_valid'] = True
     return utils.dict_to_object(status)
Ejemplo n.º 6
0
    def load_data(self):
        """
        1. Get the quiz state.
        :return:
        """
        quiz_state = self.db.get_mock_quiz_state(self.user_id, self.mock_id, self.quiz_id).to_dict()
        mock_test = self.db.get_mock_test(self.mock_id).to_dict()
        questions = []
        for i in range(len(quiz_state['answers'])):
            question = quiz_state['answers'][i]['questionId'].get().to_dict()
            questions.append(question)
            quiz_state['answers'][i]['question'] = question

            chosen = quiz_state['answers'][i].get('chosen', None)
            if chosen is not None:
                for choice in question['choices']:
                    if choice['index'] == chosen:
                        choice['isChosen'] = True

        # TODO: Modify elapsedTime into minutes:seconds
        # TODO: Modify startTime-endTime into a nice string.

        self.context['quiz'] = utils.dict_to_object(quiz_state)
        self.context['mock_quiz'] = utils.dict_to_object(mock_test)
Ejemplo n.º 7
0
 def get_mock_test_params(self):
     mock_test = self.db.get_mock_test(self.mock_id)
     return dict_to_object(mock_test.to_dict())
Ejemplo n.º 8
0
 def load_data(self):
     self.context['question'] = dict_to_object(
         self.db.get_mock_question(self.mock_id,
                                   self.question_id).to_dict())