def get_quiz(request, userid, quizid ,format = None): """ Get a quiz. """ if verify_user_hash(userid, request.query_params.get('hash')): logger.info('under quiz.get_quiz '+str(quizid)) if quizid == 'all': try: quiz_list = Quiz.objects.filter(user=userid).order_by('id') serializer = QuizSerializer(quiz_list, many = True) except Quiz.DoesNotExist as e: logger.error('under quiz.get_quiz(all) - no quizzes '+str(e.args)) return Response({'errors': 'Quizzes not found'}, status=status.HTTP_404_NOT_FOUND) elif quizid.isnumeric(): try: quiz = Quiz.objects.get(id = quizid, user = userid) except Quiz.DoesNotExist as e: logger.error('under quiz.get_quiz '+str(e.args)) return Response({'errors': 'Quiz not found'}, status = status.HTTP_404_NOT_FOUND) serializer = QuizSerializer(quiz) # serializer.data['quiz_key'] = quiz.quiz_key else: logger.error('under quiz.get_quiz wrong url passed') return Response({'errors': 'Wrong URL passed.'}, status=status.HTTP_404_NOT_FOUND) return Response(serializer.data, status = status.HTTP_200_OK) logger.error('under quiz.get_quiz wrong hash') return Response({'errors': 'Corrupted User.'}, status=status.HTTP_404_NOT_FOUND)
def update_quiz(request, userid, quizid ,format = None): """ Update a quiz. """ if verify_user_hash(userid, request.data.get('hash')): logger.info('under quiz.update_quiz') try: quiz = Quiz.objects.get(id = quizid, user = userid) except Quiz.DoesNotExist as e: logger.error('under quiz.update_quiz '+str(e.args)) return Response({'errors': 'Quiz not found'}, status = status.HTTP_404_NOT_FOUND) serializer = QuizSerializer(quiz, data = request.data) if serializer.is_valid(): serializer.save() return Response({ 'updatedQuiz' : serializer.data }, status = status.HTTP_200_OK) else: logger.error('under quiz.update_quiz '+str(serializer.errors)) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) logger.error('under quiz.get_quiz wrong hash') return Response({'errors': 'Corrupted User.'}, status=status.HTTP_404_NOT_FOUND)
def get_quiz_acc_key(request): quiz_key = request.query_params.get('quiz_key') logger.info('under quiz.get_quiz_acc_key') if quiz_key: try: quiz = Quiz.objects.get(quiz_key = quiz_key) serializer = QuizSerializer(quiz) return Response(serializer.data, status = status.HTTP_200_OK) except Quiz.DoesNotExist as e: logger.error('under quiz.get_quiz_acc_key '+str(e.args)) return Response({'errors': 'Quiz not found.'}, status = status.HTTP_404_NOT_FOUND) else: logger.error('under quiz.get_quiz_acc_key quiz_key not given') return Response({'errors': 'Quiz Key not given.'}, status = status.HTTP_404_NOT_FOUND)
def create_quiz(request, format = None): """ Create a new quiz. """ if verify_user_hash(request.data.get('user'), request.data.get('hash')): logger.info('under quiz.create_quiz') serializer = QuizSerializer(data = request.data) if serializer.is_valid(): quiz = serializer.save() data = serializer.data data['allow_public_access'] = quiz.allow_public_access data['id'] = quiz.id data['quiz_key'] = quiz.quiz_key data['show_result_on_completion'] = quiz.show_result_on_completion data['total_duration'] = quiz.total_duration data['total_marks'] = quiz.total_marks data['total_questions'] = quiz.total_questions data['total_sections'] = quiz.total_sections data['url'] = quiz.url return Response(data, status = status.HTTP_200_OK) logger.error('under quiz.create_quiz '+str(serializer.errors)) return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST) logger.error('under quiz.mark_quiz_public wrong hash') return Response({'errors': 'Corrupted User.'}, status=status.HTTP_404_NOT_FOUND)
def create_live_test(request, live_key): logger.info('under quiz.create_live_test') print request.data quiz_obj = None is_quiz_new = False # Temp data LIVE_TEST_CREATION_DATA = request.data['LIVE_TEST_CREATION_DATA'] #Live test number should be start with 0 index. def create_quiz_stack(quiz_stack_data, quiz_obj, sub_category_obj, live_test_number): logger.info('under quiz.create_live_test.create_quiz_stack') questions_list = Question.objects.filter(sub_category = sub_category_obj, que_type = quiz_stack_data['que_type']) if len(questions_list)>=30*(live_test_number+1): questions_list = questions_list[30*live_test_number:30*(live_test_number+1)] else: questions_list = questions_list.order_by('?')[:30] qs = QuizStackSerializer(data = quiz_stack_data) if qs.is_valid(): print quiz_stack_data['que_type'],len(questions_list) qs = qs.save() qs.add_selected_questions([que.id for que in questions_list]) return qs else: logger.error('under quiz.create_live_test.create_quiz_stack error '+str(qs.errors)) return None try: user = User.objects.get(username = LIVE_TEST_CREATION_DATA['user_name'], email = LIVE_TEST_CREATION_DATA['user_email']) quiz_obj = Quiz.objects.get(user = user, title = LIVE_TEST_CREATION_DATA['title'], quiz_key = LIVE_TEST_CREATION_DATA['live_test_key']) except User.DoesNotExist as e: logger.error('under quiz.create_live_test '+str(e.args)) user = User.objects.create_user(username = LIVE_TEST_CREATION_DATA['user_name'], email = LIVE_TEST_CREATION_DATA['user_email'], password = '******') quiz_obj = Quiz.objects.get(user = user, quiz_key = LIVE_TEST_CREATION_DATA['live_test_key'], title = LIVE_TEST_CREATION_DATA['title']) except Quiz.DoesNotExist as e: logger.error('under quiz.create_live_test create a new quiz '+str(e.args)) is_quiz_new = True print 'Create a new quiz here' quiz_serializer = QuizSerializer(data = { 'user':user.id,'quiz_key':LIVE_TEST_CREATION_DATA['live_test_key'],'title':LIVE_TEST_CREATION_DATA['title'], 'no_of_attempt': LIVE_TEST_CREATION_DATA['allow_attempt'], 'passing_percent':LIVE_TEST_CREATION_DATA['passing_percent'],'success_text':'','fail_text':'', # 'total_marks':78, 'total_questions':LIVE_TEST_CREATION_DATA['total_questions'], 'total_sections':2 }) if quiz_serializer.is_valid(): '''Add some quiz stacks of 30''' quiz_obj = quiz_serializer.save() else: print quiz_serializer.errors logger.error('under quiz.create_live_test create a new quiz error '+str(quiz_serializer.errors)) print '>>>>>>>',quiz_obj if quiz_obj and is_quiz_new: logger.info('under quiz.create_live_test '+str(quiz_obj)+' a new quiz ') invited_user, is_new = InvitedUser.objects.get_or_create(user_name = user.username, user_email = user.email, defaults={}) invited_user.add_inviteduser(quiz_obj.id) for category_details in LIVE_TEST_CREATION_DATA['section_details']: for sub_category_name,sub_category_questions_details in category_details['included_sub_category_list'].items(): sub_category_data_list = sub_category_name.split(':#:') sub_category_obj = SubCategory.objects.get(sub_category_name = sub_category_data_list[0]) data = {'quiz':quiz_obj.id,'subcategory':'','section_name': category_details['section_name'], 'level':'', 'que_type':'', # 'duration':category_details['duration'], 'no_questions':0,'question_order':'random' } data['subcategory'] = sub_category_obj.id data['que_type'] = sub_category_data_list[1] for question_level, no_of_questions in sub_category_questions_details.items(): data['level'] = question_level data['no_questions'] = no_of_questions create_quiz_stack(data, quiz_obj, sub_category_obj, LIVE_TEST_CREATION_DATA['test_no']) return Response({'msg': 'Live quiz created successfully.', 'access_url':TEST_URL.format(quiz_key = LIVE_TEST_CREATION_DATA['live_test_key']), 'is_quiz_new':is_quiz_new}, status=status.HTTP_200_OK) else: print 'Quiz is pre existed' logger.info('under quiz.create_live_test '+str(quiz_obj)+' already existing quiz') return Response({'msg': 'Live quiz pre-created', 'access_url':TEST_URL.format(quiz_key = LIVE_TEST_CREATION_DATA['live_test_key']), 'is_quiz_new':is_quiz_new}, status=status.HTTP_200_OK)