Exemplo n.º 1
0
def wrong_rank(request):
    exam_id = request.GET['exam']
    count = request.GET.get('count', 5)

    # find questions
    question_ids = Exam.objects.get(id=exam_id).questions.split(",")
    if question_ids is None or len(question_ids) == 0:
        return http.wrap_ok_response(None)

    # find statistics
    data = QuestionStatistics.objects.filter(
        question_id__in=question_ids).order_by("-wrong_count")
    if data is None or len(data) == 0:
        return http.wrap_ok_response(None)

    # wrap
    map_question_number = {}
    i = 1
    for qid in question_ids:
        map_question_number[qid] = i
        i += 1

    result = []
    i = 0
    for stat in data:
        if i >= count:
            return http.wrap_ok_response(result)
        # get materials
        materials = []
        question = Question.objects.get(id=stat.question_id)
        explain_pics = [question.explain_image_link]
        material_ids = question.material_ids.split(",")
        if question.material_type == 1:
            materials = map(
                lambda each: each["image_link"],
                MaterialImage.objects.filter(
                    id__in=material_ids).values("image_link"))
        if question.material_type == 2:
            materials = map(
                lambda each: each["video_link"],
                MaterialVideo.objects.filter(
                    id__in=material_ids).values("video_link"))
        result.append({
            "order":
            i + 1,
            "wrong_count":
            stat.wrong_count,
            "material_type":
            question.material_type,
            "question_number":
            map_question_number[str(stat.question_id)],
            "materials":
            materials,
            "correct_answer":
            int(question.correct_answer),
            "explain_pics":
            explain_pics
        })
        i += 1
    return http.wrap_ok_response(result)
Exemplo n.º 2
0
def robot_get_progress(request):
    exam_id = request.GET['exam']
    robot_user = get_robot_user()
    robot_score = Score.objects.filter(exam_id=exam_id, user_id=robot_user.id)
    if len(robot_score) == 0:
        return http.wrap_ok_response({})
    else:
        robot_score = robot_score[0]
        answer = "{}"
        if robot_score.answer != '':
            answer = robot_score.answer
        return http.wrap_ok_response(json.loads(answer))
Exemplo n.º 3
0
def show_exam(request):
    exam_id = request.GET['exam']
    exam = Exam.objects.get(id=exam_id)
    question_ids = exam.questions.split(',')
    count = 1
    questions = []
    for question_id in question_ids:
        question = {}
        question_obj = Question.objects.get(id=question_id)
        question['question_number'] = count
        question['material_type'] = question_obj.material_type
        material_ids = question_obj.material_ids.split(',')
        material_links = []
        for material_id in material_ids:
            if question_obj.material_type == 1:
                material_links.append(
                    MaterialImage.objects.get(id=material_id).image_link)
            elif question_obj.material_type == 2:
                material_links.append(
                    MaterialVideo.objects.get(id=material_id).video_link)
        question['material_links'] = material_links
        questions.append(question)
        count += 1

    exam_data = {
        'exam_id': int(exam_id),
        'exam_title': exam.title,
        'questions': questions
    }
    return http.wrap_ok_response(exam_data)
Exemplo n.º 4
0
def team_tick_answer(request):
    exam_id = request.POST['exam']
    question_id = request.POST['question']
    this_answer = request.POST['answer']
    account = request.POST['account']

    answer_dic = {question_id: this_answer}
    answer = json.dumps(answer_dic)

    team_user = get_team_user(account)
    team_score = Score.objects.filter(exam_id=exam_id, user_id=team_user.id)
    if len(team_score) == 0:
        team_score = Score.objects.create(exam_id=exam_id,
                                          user_id=team_user.id,
                                          answer=answer)
    else:
        team_score = team_score[0]
        answer_new_dic = {}
        if team_score.answer != '':
            answer_new_dic.update(json.loads(team_score.answer))
        else:
            team_score.begin_at = timezone.now()
        answer_new_dic.update(answer_dic)
        team_score.answer = json.dumps(answer_new_dic)
        team_score.save()

    return http.wrap_ok_response(json.loads(team_score.answer))
Exemplo n.º 5
0
def audience_get_progress(request):
    exam_id = request.GET['exam']
    progress = {}
    for aud_type in AUDIENCE_TYPE:
        progress[AUDIENCE_KEY[aud_type]] = get_audience_progress(
            exam_id, aud_type)
    return http.wrap_ok_response(progress)
Exemplo n.º 6
0
def team_get_progress(request):
    exam_id = request.GET['exam']
    progress = []
    for account in get_pre_exam_winner(exam_id):
        each_progress = get_each_team_progress(exam_id, account)
        progress.append(each_progress)
    return http.wrap_ok_response(progress)
Exemplo n.º 7
0
def audience_get_rank(request):
    exam_id = request.GET['exam']
    count = request.GET.get('count', "10")
    rank = {}
    for aud_type in AUDIENCE_TYPE:
        rank[AUDIENCE_KEY[aud_type]] = get_audience_rank(
            exam_id, int(count), aud_type)
    return http.wrap_ok_response(rank)
Exemplo n.º 8
0
def rest_seconds(request):
    exam_id = request.GET['exam']
    map_exam_stage = {4: 1, 1: 3, 2: 5, 3: 7}
    time_limit = Exam.objects.get(id=exam_id).time_limit
    rest = 0
    if get_stage() == map_exam_stage[int(exam_id)]:
        rest = time_limit - (time.time() - get_stage_begin_timestamp())
        if rest < 0:
            rest = 0

    return http.wrap_ok_response({"rest": int(rest)})
Exemplo n.º 9
0
def team_get_rank(request):
    exam_id = request.GET['exam']
    rank = []
    # 先找机器人
    robot_user = get_robot_user()
    robot_score = Score.objects.filter(exam_id=exam_id, user_id=robot_user.id)
    if len(robot_score) != 0:
        robot_score = robot_score[0]
        rank.append({"name": "机器人", "order": 0, "score": robot_score.score})
    # 再找代表队
    for account in ACCOUNT_TEAMS:
        team_user = get_team_user(account)
        team_sore = Score.objects.filter(exam_id=exam_id, user_id=team_user.id)
        if len(team_sore) != 0:
            team_sore = team_sore[0]
            rank.append({
                "name": NAME_TEAMS[account],
                "order": 0,
                "score": team_sore.score
            })
        elif int(exam_id) == 3:
            # 决赛要返回所有代表队成绩
            team_sore = Score.objects.filter(exam_id=2, user_id=team_user.id)
            if len(team_sore) == 0:
                team_sore = Score.objects.filter(exam_id=1,
                                                 user_id=team_user.id)
            if len(team_sore) != 0:
                team_sore = team_sore[0]
                rank.append({
                    "name": NAME_TEAMS[account],
                    "order": 0,
                    "score": team_sore.score
                })

    # 排序
    rank = sorted(rank, key=operator.itemgetter('score'), reverse=True)
    for i in range(len(rank)):
        rank[i]['order'] = i + 1
    return http.wrap_ok_response(rank)
Exemplo n.º 10
0
def robot_submit_answer(request):
    exam_id = request.POST['exam']

    robot_user = get_robot_user()
    robot_score = Score.objects.filter(exam_id=exam_id, user_id=robot_user.id)
    if len(robot_score) == 0:
        return http.wrap_bad_response(-1, 'not found this exam')
    else:
        robot_score = robot_score[0]
        robot_score.submitted = True
        # count score
        point = get_pre_exam_score(exam_id, ACCOUNT_ROBOT, 0)
        if robot_score.answer == '':
            point += 0
        else:
            point += compute_score(exam_id, json.loads(robot_score.answer))

        robot_score.elapsed_seconds = int(
            (timezone.now() - robot_score.begin_at).total_seconds())
        robot_score.score = point
        robot_score.save()

    return http.wrap_ok_response({"score": point})
Exemplo n.º 11
0
def all_users_statistics(request):
    users = User.objects.all()
    data = {}
    stat = []
    for user in users:
        if user.user_type == 0:
            if user.account == 'robot':
                display = 'AI'
            else:
                display = NAME_TEAMS[user.account]
        else:
            display = user.phone

        # 统计分数和回答情况
        score_obj = None
        answer_stat = []
        score_obj_1 = Score.objects.filter(exam_id=1, user_id=user.id)
        if len(score_obj_1) > 0:
            score_obj = score_obj_1[0]
            if user.user_type == 0:
                answer_dic = json.loads(score_obj.answer)
            else:
                answer_dic = change_answer_list_to_dict(score_obj.answer)
            answer_stat += compute_answer(1, answer_dic)
        else:
            answer_stat += [0 for i in range(50)]

        score_obj_2 = Score.objects.filter(exam_id=2, user_id=user.id)
        if len(score_obj_2) > 0:
            score_obj = score_obj_2[0]
            if user.user_type == 0:
                answer_dic = json.loads(score_obj.answer)
            else:
                answer_dic = change_answer_list_to_dict(score_obj.answer)
            answer_stat += compute_answer(2, answer_dic)
        else:
            answer_stat += [0 for i in range(30)]

        score_obj_3 = Score.objects.filter(exam_id=3, user_id=user.id)
        if len(score_obj_3) > 0:
            score_obj = score_obj_3[0]
            if user.user_type == 0:
                answer_dic = json.loads(score_obj.answer)
            else:
                answer_dic = change_answer_list_to_dict(score_obj.answer)
            answer_stat += compute_answer(3, answer_dic)
        else:
            answer_stat += [0 for i in range(20)]

        if score_obj is not None:
            point = score_obj.score
        else:
            point = 0

        stat.append({
            "user_id": user.id,
            "user_type": user.user_type,
            "phone": display,
            "score": point,
            "answer": answer_stat,
            "user_info": model_to_dict(user)
        })

    # 排序
    stat = sorted(stat, key=operator.itemgetter('score'), reverse=True)
    for i in range(len(stat)):
        stat[i]['order'] = i + 1
    data['stat'] = stat
    return http.wrap_ok_response(data)
Exemplo n.º 12
0
def req_set_stage(request):
    stage_obj = request.GET['stage']
    set_stage(stage_obj)
    return http.wrap_ok_response(None)
Exemplo n.º 13
0
def req_get_stage(request):
    return http.wrap_ok_response({"stage": get_stage()})