Beispiel #1
0
def create_exam(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {
                'exam_name', 'exam_duration', 'group_id', 'template_id',
                'show_result', 'result_timestamp', 'is_enable',
                'exam_enable_time', 'auth_key'
        }.issubset(data.keys()) and authenticate(data['auth_key']):
            try:
                group = Master_Groups.objects.get(id=data['group_id'],
                                                  is_available=True)
                template = Master_Template.objects.get(id=data['template_id'],
                                                       is_available=True)
                exam = Master_Exam.objects.create(
                    exam_name=data['exam_name'],
                    exam_duration=data['exam_duration'],
                    group=group,
                    template=template,
                    show_result_immediately=data['show_result'],
                    result_timestamp=data['result_timestamp'],
                    is_enable=data['is_enable'],
                    exam_enable_time=data['exam_enable_time'])
                exam.save()
                resp = Response(200, 'exam created successfully')
                return JsonResponse(resp, status=200)
            except Master_Template.DoesNotExist:
                resp = Response(203, 'Template doesnot exists')
                return JsonResponse(resp, status=200)
            except Master_Groups.DoesNotExist:
                resp = Response(203, 'Group doesnot exists')
                return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #2
0
def add_question(request):
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        if {
                'auth_key', 'question_type', "question_marks", "question_text",
                "difficulty", "subtopic", "options", "correct_options"
        }.issubset(data.keys()) and authenticate(data['auth_key']):
            subtopic_obj = Master_SubTopic.objects.get(id=data['subtopic'])
            question_obj = Master_Question.objects.create(
                question_type=data['question_type'],
                question_text=data['question_text'],
                question_marks=data['question_marks'],
                difficulty=data['difficulty'],
                subtopic=subtopic_obj)
            question_obj.save()
            options_arr = data['options']
            correct_arr = data['correct_options']

            for i in range(0, len(options_arr)):
                option_obj = Master_Option.objects.create(
                    option_text=options_arr[i], question=question_obj)
                option_obj.save()
                for j in range(0, len(correct_arr)):
                    print("correct ans: ", correct_arr[j])
                    if (i == int(correct_arr[j])):
                        correct_option = Master_Correct_Option.objects.create(
                            option=option_obj, question=question_obj)
                        correct_option.save()

            resp = Response(200, "Question added successfully")
            return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #3
0
def get_all_exams_user(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'user_id', 'auth_key'}.issubset(data.keys()) and authenticate(
                data['auth_key']):
            try:
                user = Master_Users.objects.get(id=data['user_id'])
                group_mappings = User_Group_Mapping.objects.filter(user=user)
            except Master_Users.DoesNotExist:
                resp = Response(204, 'User doesnot exist ')
                return JsonResponse(resp, status=200)
            except User_Group_Mapping.DoesNotExist:
                resp = Response(204, 'user and group do not match')
                return JsonResponse(resp, status=200)
            exams_arr = []
            exams_dict = {'data': exams_arr}
            for group_mapping in group_mappings:
                try:
                    group = group_mapping.group
                    exams = Master_Exam.objects.filter(group=group)
                except Master_Exam.DoesNotExist:
                    pass
                for exam in exams:
                    temp_dict = get_exam_dict(exam)
                    try:
                        user_test = User_Test_Status.objects.get(exam=exam,
                                                                 user=user)
                        temp_dict['status'] = user_test.status
                    except User_Test_Status.DoesNotExist:
                        temp_dict['status'] = 1
                    exams_arr.append(temp_dict)
            return JsonResponse(exams_dict, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #4
0
def activate_question(request):
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        if ({'question_id', 'auth_key'}.issubset(
                data.keys())) and authenticate(data['auth_key']):
            try:
                question_obj = Master_Question.objects.get(
                    id=data['question_id'], is_available=False)
                question_obj.is_available = True
                question_obj.save()
                option_obj = Master_Option.objects.filter(
                    question=question_obj, is_available=False)
                for option in option_obj:
                    option.is_available = True
                    option.save()
                correct_option_obj = Master_Correct_Option.objects.filter(
                    question=question_obj, is_available=False)
                for correct_option in correct_option_obj:
                    correct_option.is_available = True
                    correct_option.save()
                resp = Response(200, "Question activated successfully")
                return JsonResponse(resp, status=200)
            except:
                resp = Response(203, 'Question or Options doesnot exists')
                return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #5
0
def scroll_through_exam(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {
                'question_assigned_id', 'option_id', 'marked',
                'next_question_id', 'user_id', 'exam_id', 'auth_key',
                'duration'
        }.issubset(data.keys()) and authenticate(data['auth_key']):
            try:
                #response saved
                user_response = User_Question_Response.objects.get(
                    section_question=data['question_assigned_id'])
                option_obj = Master_Option.objects.get(id=data['option_id'])
                user_response.option = option_obj
            except Master_Option.DoesNotExist:
                user_response.option = None
            except User_Question_Response.DoesNotExist:
                resp = Response(203, 'Response doesnot exists')
                return JsonResponse(resp, status=200)
            except Master_Question.DoesNotExist:
                resp = Response(
                    203, 'Wrong Next Question id and it doesnot exists')
                return JsonResponse(resp, status=200)
            user_response.marked = data['marked']
            user_response.save()
            #question fetched
            if data['next_question_id'] != 0:
                question = Master_Question.objects.get(
                    id=data['next_question_id'])
            else:
                #no question to be fetched, final question
                question = None

            #fetch exam and user
            try:
                exam_obj = Master_Exam.objects.get(id=data['exam_id'])
                user_obj = Master_Users.objects.get(id=data['user_id'])
            except Master_Exam.DoesNotExist:
                resp = Response(203, 'Exam doesnot exists')
                return JsonResponse(resp, status=200)
            except Master_Users.DoesNotExist:
                resp = Response(203, 'User doesnot exists')
                return JsonResponse(resp, status=200)

            user_question_assigned_arr = User_Question_Assigned.objects.filter(
                exam=exam_obj, user=user_obj)
            return JsonResponse(soul(exam_obj, user_obj, question,
                                     user_question_assigned_arr,
                                     data['duration']),
                                status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #6
0
def get_exam(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'id', 'auth_key'}.issubset(data.keys()) and authenticate(
                data['auth_key']):
            try:
                exam = Master_Exam.objects.get(id=data['id'])
                return JsonResponse(get_exam_dict(exam), status=200)
            except Master_Exam.DoesNotExist:
                resp = Response(203, 'Exam doesnot exists')
                return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #7
0
def delete_subtopic_perman(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'id','auth_key'}.issubset(data.keys()) and authenticate(data['auth_key']):
            try:
                subtopic = Master_SubTopic.objects.get(id = data['id'],is_available = True)
                subtopic.delete()
                resp = Response(200, "Deleted permanently")
                return JsonResponse(resp, status = 200)
            except Master_SubTopic.DoesNotExist:
                resp = Response(203,"Subtopic doesnot exist")
                return JsonResponse(resp,status  = 200)
        
    resp = Response(405,'Bad Request!!')
    return JsonResponse(resp,status = 405)
Beispiel #8
0
def delete_user_test(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'id', 'auth_key'}.issubset(data.keys()) and authenticate(
                data['auth_key']):
            try:
                user_test_status = User_Test_Status.objects.get(id=data['id'])
                user_test_status.delete()
                resp = Response(200, 'user_test_status deleted successfully')
                return JsonResponse(resp, status=200)
            except User_Test_Status.DoesNotExist:
                resp = Response(203, 'Exam doesnot exists')
                return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #9
0
def create_topic(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'text','auth_key'}.issubset(data.keys()) and authenticate(data['auth_key']):
            try:
                topic = Master_Topic.objects.get(topic_text = data['text'])
                resp = Response(203,"Topic already exists")
                return JsonResponse(resp, status  = 200)
            except Master_Topic.DoesNotExist:
                topic = Master_Topic.objects.create(topic_text = data['text'])
                topic.save()
                resp = Response(200,"Created successfully")
                return JsonResponse(resp,status = 200)
    
    resp = Response(405,'Bad Request!!')
    return JsonResponse(resp,status = 405)
Beispiel #10
0
def get_question(request):
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        if {'auth_key', 'question_id'}.issubset(data.keys()) and authenticate(
                data['auth_key']):
            question_obj = Master_Question.objects.get(id=data['question_id'])
            #question_data = {'data':get_single_question(question_obj)}
            try:
                return JsonResponse(get_single_question(question_obj),
                                    status=200)
            except:
                resp = Response('Something went wrong', Exception)
                return JsonResponse(resp, status=203)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #11
0
def soul(exam, user, question_to_be_fetched, user_question_assigned_arr,
         duration):

    try:
        user_test_status = User_Test_Status.objects.get(user=user, exam=exam)
        if question_to_be_fetched == None:
            #final question
            user_test_status.status = 3
            user_test_status.save()
            resp = Response(200, "Exam completed successfully !")
            return resp
        if duration == -1:
            print('if')
            user_test_status.attempts -= 1
            pass
        else:
            user_test_status.duration = duration
            print('else')
        user_test_status.save()

    except User_Test_Status.DoesNotExist:
        print('except')
        user_test_status = User_Test_Status.objects.create(exam=exam,
                                                           user=user,
                                                           status=2,
                                                           duration=duration)
        user_test_status.save()

    user_question_response_arr = []

    for user_question_assigned_obj in user_question_assigned_arr:
        user_response = User_Question_Response.objects.get(
            section_question=user_question_assigned_obj)
        user_question_response_arr.append(user_response)

    response_option_id = 0
    main_arr = []
    main_dict = {"data": main_arr}
    for i in range(0, len(user_question_assigned_arr)):
        question = user_question_assigned_arr[i].question
        temp_obj = {
            'question_id': question.id,
            'marked': user_question_response_arr[i].marked,
            'user_question_assigned_id': user_question_assigned_arr[i].id
        }
        if user_question_response_arr[i].option == None:
            temp_obj['response'] = False
        else:
            temp_obj['response'] = True
            if question == question_to_be_fetched:
                response_option_id = User_Question_Response.objects.get(
                    section_question=user_question_assigned_arr[i]).option.id

        main_arr.append(temp_obj)

    main_dict['first_question'] = get_single_question(question_to_be_fetched)
    main_dict['response_option_id'] = response_option_id
    main_dict['duration'] = user_test_status.duration
    main_dict['status'] = 200
    return main_dict
Beispiel #12
0
def activate_topic(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'id','auth_key'}.issubset(data.keys()) and authenticate(data['auth_key']):
            try:
                topic = Master_Topic.objects.get(id = data['id'],is_available = False)
                topic.is_available = True
                topic.save()
                resp = Response(200, "Activated successfully")
                return JsonResponse(resp, status = 200)
            except Master_SubTopic.DoesNotExist:
                resp = Response(203,"topic doesnot exist")
                return JsonResponse(resp,status  = 200)
    
    resp = Response(405,'Bad Request!!')
    return JsonResponse(resp,status = 405)
Beispiel #13
0
def delete_question_perman(request):
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        if ({'question_id', 'auth_key'}.issubset(
                data.keys())) and authenticate(data['auth_key']):
            try:
                question_obj = Master_Question.objects.get(
                    id=data['question_id'], is_available=True)
                question_obj.delete()
                resp = Response(200, "Question deleted successfully")
                return JsonResponse(resp, status=200)
            except:
                resp = Response(203, 'Question doesnot exists')
                return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #14
0
def delete_exam_perman(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'id', 'auth_key'}.issubset(data.keys()) and authenticate(
                data['auth_key']):
            try:
                exam = Master_Exam.objects.get(id=data['id'],
                                               is_available=True)
                exam.delete()
                resp = Response(200, 'exam deleted successfully')
                return JsonResponse(resp, status=200)
            except Master_Exam.DoesNotExist:
                resp = Response(203, 'Exam doesnot exists')
                return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #15
0
def update_question(request):
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        if {
                'auth_key', 'question_id', 'question_type', "question_marks",
                "question_text", "difficulty", "subtopic", "options",
                "correct_options"
        }.issubset(data.keys()) and authenticate(data['auth_key']):
            subtopic_obj = Master_SubTopic.objects.get(id=data['subtopic'])
            question_obj = Master_Question.objects.get(id=data['question_id'])
            question_obj.question_type = data['question_type']
            question_obj.question_text = data['question_text']
            question_obj.question_marks = data['question_marks']
            question_obj.difficulty = data['difficulty']
            question_obj.subtopic = subtopic_obj
            question_obj.save()
            options_arr = data['options']
            correct_arr = data['correct_options']
            correct_objs = Master_Correct_Option.objects.filter(
                question=int(data['question_id']))
            print(correct_objs)
            #deleting past correct options
            for correct_obj in correct_objs:
                correct_obj.is_available = False
                correct_obj.save()
            #deleting past options
            option_objs = Master_Option.objects.filter(
                question=int(data['question_id']))
            for option_obj in option_objs:
                option_obj.is_available = False
                option_obj.save()
            for i in range(0, len(options_arr)):
                option_obj = Master_Option.objects.create(
                    option_text=options_arr[i], question=question_obj)
                option_obj.save()
                for j in range(0, len(correct_arr)):
                    if (i == int(correct_arr[j])):
                        correct_option = Master_Correct_Option.objects.create(
                            option=option_obj, question=question_obj)
                        correct_option.save()
            resp = Response(200, "Question modified successfully")
            return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #16
0
def get_all_questions(request):
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        if {'auth_key'}.issubset(data.keys()) and authenticate(
                data['auth_key']):
            questions = Master_Question.objects.all()
            arr_dict = []
            questions_data = {'data': arr_dict}
            try:
                for question in questions:
                    arr_dict.append(get_single_question(question))
                return JsonResponse(questions_data, status=200)
            except:
                resp = Response('Something went wrong GEN 1', Exception)
                return JsonResponse(resp, status=477)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #17
0
def get_all_exams(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'auth_key'}.issubset(data.keys()) and authenticate(
                data['auth_key']):
            exams_arr = []
            exams_dict = {'data': exams_arr}
            try:
                exams = Master_Exam.objects.all()
                for exam in exams:
                    exams_arr.append(get_exam_dict(exam))
                return JsonResponse(exams_dict, status=200)
            except Master_Exam.DoesNotExist:
                resp = Response(203, 'Exam doesnot exists')
                return JsonResponse(resp, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #18
0
def get_topic(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'id','auth_key'}.issubset(data.keys())  and authenticate(data['auth_key']):
            try:
                topic = Master_Topic.objects.get(id = data['id'])
                topic_dict = {
                    'id':topic.id,
                    'text':topic.topic_text,
                    'is_available':topic.is_available
                }
                return JsonResponse(topic_dict,status = 200)
            except Master_SubTopic.DoesNotExist:
                resp = Response(203,"topic doesnot exist")
                return JsonResponse(resp,status  = 200)
    
    resp = Response(405,'Bad Request!!')
    return JsonResponse(resp,status = 405)
Beispiel #19
0
def get_all_questions_subtopic(request):
    if request.method == "POST":
        data = json.loads(request.body.decode('utf-8'))
        if {'subtopic_id', 'auth_key'}.issubset(data.keys()) and authenticate(
                data['auth_key']):
            subtopic = Master_SubTopic.objects.get(id=data['subtopic_id'])
            questions = Master_Question.objects.filter(subtopic=subtopic,
                                                       is_available=True)
            arr_dict = []
            questions_data = {'data': arr_dict}
            try:
                for question in questions:
                    arr_dict.append(get_single_question(question))
                return JsonResponse(questions_data, status=200)
            except:
                resp = Response('Something went wrong GEN 1', Exception)
                return JsonResponse(resp, status=477)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #20
0
def update_subtopic(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'auth_key','id','text','topic_id'}.issubset(data.keys()) and authenticate(data['auth_key']):
            try:
                topic = Master_Topic.objects.get(id = data['topic_id'])
                subtopic = Master_SubTopic.objects.get(id = data['id'])
                subtopic.subtopic_text = data['text']
                subtopic.topic = topic
                subtopic.save()
                resp = Response(200, "Updated successfully")
                return JsonResponse(resp, status = 200)
            except Master_SubTopic.DoesNotExist:
                resp = Response(203,"Subtopic doesnot exist")
                return JsonResponse(resp,status  = 200)
            except Master_Topic.DoesNotExist:
                resp = Response(203,"topic doesnot exist")
                return JsonResponse(resp,status  = 200)
    
    resp = Response(405,'Bad Request!!')
    return JsonResponse(resp,status = 405)
Beispiel #21
0
def create_key(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        token = data['token']
        if match_token(token):
            global AUTH_KEY
            AUTH_KEY = str(random.randint(100000000000,999999999999))
            dict = {
                'auth_key':AUTH_KEY
            }
            return JsonResponse(dict,status = 200)
    resp = Response(400,'Bad Request')
    return JsonResponse(resp)
Beispiel #22
0
def get_all_topics(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'auth_key'}.issubset(data.keys()) and authenticate(data['auth_key']):
            topics_arr = []
            topics_dict = {'data':topics_arr}
            topics = Master_Topic.objects.all()
            for topic in topics:
                temp = {
                    'id':topic.id,
                    'text':topic.topic_text,
                    'is_available':topic.is_available
                }
                topics_arr.append(temp)
            return JsonResponse(topics_dict,status = 200)

    resp = Response(405,'Bad Request!!')
    return JsonResponse(resp,status = 405)
Beispiel #23
0
def add_questions(request):
    print('first stage')
    print('subtopic 1')
    try:
        options_arr = ['option_a', 'option_b', 'option_c', 'option_d']
        correct_arr = [0]
        subtopic_1 = Master_SubTopic.objects.get(id=3)
        subtopic_2 = Master_SubTopic.objects.get(id=4)
        subtopic_3 = Master_SubTopic.objects.get(id=5)
    except:
        print('in try 1 maybe a subtopic error ')
        resp = Response(204, "Wrong key value pair in try part 0")
        return JsonResponse(resp, status=200)

    try:
        for i in range(33):
            question_obj = Master_Question.objects.create(
                question_text='question' + str(i),
                question_marks=1,
                difficulty=1,
                question_type=1,
                subtopic=subtopic_1)
            question_obj.save()
            for i in range(0, len(options_arr)):
                option_obj = Master_Option.objects.create(
                    option_text=options_arr[i], question=question_obj)
                option_obj.save()
                for j in range(0, len(correct_arr)):
                    if (i == int(correct_arr[j])):
                        correct_option = Master_Correct_Option.objects.create(
                            option=option_obj, question=question_obj)
                        correct_option.save()

    except Exception as e:
        print('error in for looop 1')
        print(e)
        resp = Response(204, "Wrong key value pair in try part 1")
        return JsonResponse(resp, status=200)

    try:
        for i in range(33):
            question_obj = Master_Question.objects.create(
                question_text='question' + str(i + 33),
                question_marks=1,
                difficulty=1,
                question_type=1,
                subtopic=subtopic_2)
            question_obj.save()
            for i in range(0, len(options_arr)):
                option_obj = Master_Option.objects.create(
                    option_text=options_arr[i], question=question_obj)
                option_obj.save()
                for j in range(0, len(correct_arr)):
                    if (i == int(correct_arr[j])):
                        correct_option = Master_Correct_Option.objects.create(
                            option=option_obj, question=question_obj)
                        correct_option.save()
    except:
        print('error in for loop 2')
        resp = Response(204, "Wrong key value pair in try part 2")
        return JsonResponse(resp, status=200)

    try:
        for i in range(33):
            question_obj = Master_Question.objects.create(
                question_text='question' + str(i + 66),
                question_marks=1,
                difficulty=1,
                question_type=1,
                subtopic=subtopic_3)
            question_obj.save()
            for i in range(0, len(options_arr)):
                option_obj = Master_Option.objects.create(
                    option_text=options_arr[i], question=question_obj)
                option_obj.save()
                for j in range(0, len(correct_arr)):
                    if (i == int(correct_arr[j])):
                        correct_option = Master_Correct_Option.objects.create(
                            option=option_obj, question=question_obj)
                        correct_option.save()
    except:
        print('error in for loop 3')
        resp = Response(204, "Wrong key value pair in try part 3")
        return JsonResponse(resp, status=200)

    resp = Response(200, "Questionssss added successfully")
    return JsonResponse(resp, status=200)
Beispiel #24
0
def assign_questions_to_exam(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'user_id', 'exam_id', 'auth_key'}.issubset(
                data.keys()) and authenticate(data['auth_key']):
            template_section_arr_obj = []
            #get a template by using exam_id
            try:
                exam_obj = Master_Exam.objects.get(id=data['exam_id'],
                                                   is_available=True)
                user_obj = Master_Users.objects.get(id=data['user_id'],
                                                    is_available=True)
            except Master_Exam.DoesNotExist:
                resp = Response(203, 'Exam doesnot exists')
                return JsonResponse(resp, status=200)
            except Master_Users.DoesNotExist:
                resp = Response(203, 'User doesnot exists')
                return JsonResponse(resp, status=200)

            #CHECKING if question is already assigned
            try:
                user_question_assigned_arr = User_Question_Assigned.objects.filter(
                    exam=exam_obj, user=user_obj)
                if len(user_question_assigned_arr) > 0:
                    question = user_question_assigned_arr[0].question
                    user_test_status = User_Test_Status.objects.get(
                        user=user_obj, exam=exam_obj)
                    if user_test_status.attempts <= 0:
                        resp = Response(203, 'Attempts have been finished')
                        return JsonResponse(resp, status=200)
                    return JsonResponse(soul(exam_obj, user_obj, question,
                                             user_question_assigned_arr, -1),
                                        status=200)
            except User_Question_Assigned.DoesNotExist:
                pass
            try:
                template_obj = exam_obj.template
                section_arr_obj = Master_Section.objects.filter(
                    template=template_obj, is_available=True)
            except Master_Section.DoesNotExist:
                print('in try block 2 error is with filter of Master sections')
                resp = Response(203, 'Master Section error persists')
                return JsonResponse(resp, status=200)

            #fetch all template sections based on sections
            try:
                for section_obj in section_arr_obj:
                    temp_temp_section_arr = Template_Section.objects.filter(
                        section=section_obj)
                    for temp_section in temp_temp_section_arr:
                        template_section_arr_obj.append(temp_section)
            except Template_Section.DoesNotExist:
                print(
                    'in try block 3 error is with template_Section or with appending the objects'
                )
                resp = Response(203, 'Template Section error persists')
                return JsonResponse(resp, status=200)

            try:
                for temp_section_obj in template_section_arr_obj:
                    subtopic_obj = temp_section_obj.subtopic
                    section_obj = temp_section_obj.section
                    questions = Master_Question.objects.filter(
                        subtopic=subtopic_obj,
                        is_available=True,
                        difficulty=temp_section_obj.difficulty_id)
                    questions = list(questions)
                    for i in range(0, temp_section_obj.no_questions):
                        rand_index = random.randint(0, len(questions) - 1)
                        question = questions[rand_index]
                        user_question_assigned_obj = User_Question_Assigned.objects.create(
                            question=question,
                            exam=exam_obj,
                            user=user_obj,
                            section=section_obj)
                        user_question_assigned_obj.save()
                        user_question_response_obj = User_Question_Response.objects.create(
                            section_question=user_question_assigned_obj,
                            option=None)
                        user_question_response_obj.save()
                        questions.remove(question)
                        print(i)
                        print(question)

            except Exception as e:
                print(e)
                resp = Response(203, 'Question Subtopic error persists')
                return JsonResponse(resp, status=200)
            #FINAL RESULT TO SEND
            user_question_assigned_arr = User_Question_Assigned.objects.filter(
                exam=exam_obj, user=user_obj)
            question = user_question_assigned_arr[0].question
            return JsonResponse(soul(exam_obj, user_obj, question,
                                     user_question_assigned_arr,
                                     template_obj.template_duration * 60),
                                status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)
Beispiel #25
0
def get_result(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf-8'))
        if {'user_id', 'exam_id', 'auth_key'}.issubset(
                data.keys()) and authenticate(data['auth_key']):
            try:
                exam_obj = Master_Exam.objects.get(id=data['exam_id'])
                user_obj = Master_Users.objects.get(id=data['user_id'])
                exam_status = User_Test_Status.objects.get(user=user_obj,
                                                           exam=exam_obj)
                if exam_status.status != 3:
                    resp = Response(203, 'Exam has not been completed yet')
                    return JsonResponse(resp, status=200)
                user_question_assigned_arr = User_Question_Assigned.objects.filter(
                    exam=exam_obj, user=user_obj)
            except User_Test_Status.DoesNotExist:
                resp = Response(203, 'User_Test_Status doesnot exists')
                return JsonResponse(resp, status=200)
            except Master_Exam.DoesNotExist:
                resp = Response(203, 'Exam doesnot exists')
                return JsonResponse(resp, status=200)
            except Master_Users.DoesNotExist:
                resp = Response(203, 'User doesnot exists')
                return JsonResponse(resp, status=200)
            except User_Question_Assigned.DoesNotExist:
                resp = Response(203, 'User has not been assigned questions')
                return JsonResponse(resp, status=200)
            template = exam_obj.template

            total_marks = template.template_marks
            marks_obtained = 0
            question_list = []
            for user_question_assigned in user_question_assigned_arr:
                question_assigned = user_question_assigned.question
                negative_marks = user_question_assigned.section.negative_marks
                #question dict to send and attempted option list to send
                question_dict = get_single_question(question_assigned)
                attempt_option_list = []
                temp_dict = {
                    'question': question_dict,
                    'attempted_option': attempt_option_list
                }
                try:
                    user_response_arr = User_Question_Response.objects.filter(
                        section_question=user_question_assigned)
                    correct_options_set = set()
                    correct_options = Master_Correct_Option.objects.filter(
                        question=question_assigned, is_available=True)
                    for correct_option in correct_options:
                        correct_options_set.add(correct_option.option)

                    attempted_options_set = set()
                    for user_response in user_response_arr:
                        attempted_options_set.add(user_response.option)
                        attempt_option_list.append(user_response.option.id)
                    print(attempted_options_set, correct_options_set)
                    if attempted_options_set == correct_options_set:
                        marks_obtained = marks_obtained + (
                            question_assigned.question_marks *
                            question_assigned.difficulty)
                    else:
                        marks_obtained = marks_obtained - negative_marks

                except User_Question_Response.DoesNotExist:
                    pass
                question_list.append(temp_dict)
        marks_dict = {
            'exam_info': get_exam_dict(exam_obj),
            'marks_obtained': marks_obtained,
            'total_marks': total_marks,
            'questions_set': question_list,
        }
        return JsonResponse(marks_dict, status=200)

    resp = Response(405, 'Bad Request!!')
    return JsonResponse(resp, status=405)