Esempio n. 1
0
def http_get_all_types(request):
    if request.method == 'GET':
        type_list = get_type_list()
        data = {
            'type_list': type_list,
        }
        return response.Response(constant.to_response(data))
Esempio n. 2
0
def http_get_radar_data(request):
    from common_auth.models import User
    radar_data = []
    user_id = request.GET.get('id')
    user = User.objects.filter(id=user_id).first()
    for category_name in PRACTICE_TYPE_EXCRISE_CATEGORY:
        radar_data.append(
            get_solved_ratio(category_name, user)
            if get_solved_ratio(category_name, user) > 0.2 else 0.2)

    return response.Response(constant.to_response(radar_data),
                             status=status.HTTP_200_OK)
Esempio n. 3
0
def http_get_env_servers(request):
    env_id = request.query_params.get('env_id')
    if env_id:
        from common_env.models import EnvTerminal
        envterminals = EnvTerminal.objects.filter(env_id=env_id)
        servers = [{
            'id': envterminal.sub_id,
            'name': envterminal.name,
        } for envterminal in envterminals]
    else:
        servers = []
    return response.Response(constant.to_response(servers),
                             status=status.HTTP_200_OK)
Esempio n. 4
0
def http_gettask_detail(request):
    try:
        p_type = request.GET['type_id']
        task_hash = request.GET['task_hash']
    except:
        return response.Response(constant.ILLEGAL_REQUEST_PARAMETERS,
                                 status=status.HTTP_200_OK)
    task_detail = get_task_detail(p_type, task_hash)
    if task_detail:
        return response.Response(constant.to_response(task_detail),
                                 status=status.HTTP_200_OK)
    else:
        return response.Response(constant.ILLEGAL_REQUEST_PARAMETERS,
                                 status=status.HTTP_200_OK)
Esempio n. 5
0
def http_gettask_hash_list(request):
    p_type = request.GET['type_id']
    if 'event_id' in request.query_params and request.query_params[
            'event_id'] != '':
        event = TaskEvent.objects.get(id=request.query_params['event_id'])
    else:
        event = None
    cache_key = "hashlist?event={event}".format(event=event)
    cache_key = hashlib.md5(cache_key).hexdigest()
    if cache.get(cache_key) is not None:
        row = cache.get(cache_key)
    else:
        task_list = get_hash_list(p_type, event)
        row = [task.get('hash') for task in task_list]
        cache.set(cache_key, row, 60)
    return response.Response(constant.to_response(row),
                             status=status.HTTP_200_OK)
Esempio n. 6
0
def get_personal_task_record(request):
    submit_record_solved_list = {}
    theory_record_list = get_record_by_type(request.user, PRACTICE_TYPE_THEORY,
                                            True)
    exercise_record_list = get_record_by_type(request.user,
                                              PRACTICE_TYPE_EXCRISE, True)
    man_machine_record_list = get_record_by_type(request.user,
                                                 PRACTICE_TYPE_MAN_MACHINE,
                                                 True)
    real_vuln_list = get_record_by_type(request.user, PRACTICE_TYPE_REAL_VULN,
                                        True)

    submit_record_solved_list[PRACTICE_TYPE_THEORY] = theory_record_list
    submit_record_solved_list[PRACTICE_TYPE_EXCRISE] = exercise_record_list
    submit_record_solved_list[
        PRACTICE_TYPE_MAN_MACHINE] = man_machine_record_list
    submit_record_solved_list[PRACTICE_TYPE_REAL_VULN] = real_vuln_list

    return response.Response(constant.to_response(submit_record_solved_list))
Esempio n. 7
0
def get_task_record_detail(request, task_hash):
    if not task_hash:
        return response.Response(constant.ILLEGAL_REQUEST_PARAMETERS,
                                 status=status.HTTP_200_OK)

    task_type = int(task_hash.split('.')[-1])
    task_record_detail = get_task_record_by_hash(request.user, task_type,
                                                 task_hash)

    if task_type != 0:
        total_score, current_score = get_all_flag(task_type, task_hash,
                                                  request.user)
        if task_record_detail is not None:
            score_per = round(current_score / (int(total_score) * 1.0), 2)
            task_record_detail.update({
                "score_per": score_per,
            })
        else:
            task_record_detail = {"score_per": 0}

    return response.Response(constant.to_response(task_record_detail))
Esempio n. 8
0
def commit_task_answer(request):
    if 'type_id' in request.POST and 'hash' in request.POST:
        p_type = int(request.POST['type_id'])
        submit_task = get_task_object(request.POST['hash'])
        user = request.user
        answer = request.POST.get('answer')

        # 没有答案
        if not answer:
            return response.Response(constant.ILLEGAL_REQUEST_PARAMETERS)

        _ret = _commit_task_answer(p_type, submit_task, user, answer)

        if p_type != 0:
            if submit_task.solving_mode:
                score_per = round(
                    _ret["score"] / (int(_ret["all_score"]) * 1.0), 2)
                _ret.update({
                    "score_per": score_per,
                })
            else:
                score_per = 1 if _ret["is_solved"] else 0
                _ret.update({
                    "score_per": score_per,
                })

        if _ret["is_solved"] and p_type != 0:
            # 增加实操练习个人日历
            if _ret["content"] is not None and _ret["content"] != "":
                task_content = _ret["content"].encode('utf-8')
            else:
                task_content = ""
            if p_type == PRACTICE_TYPE_REAL_VULN:
                practice_type = _("x_real_vuln")
                calendar_api.add_calendar(practice_type,
                                          task_content,
                                          calendar_api.CALENDAR_PARCTICE_REAl,
                                          reverse("practice:defensetraintask",
                                                  kwargs={
                                                      "typeid":
                                                      p_type,
                                                      'task_hash':
                                                      request.POST['hash']
                                                  }),
                                          False,
                                          user=request.user)
            else:
                practice_type = _("x_practical_exercises")
                calendar_api.add_calendar(practice_type,
                                          task_content,
                                          calendar_api.CALENDAR_PARCTICE_EXE,
                                          reverse("practice:defensetraintask",
                                                  kwargs={
                                                      "typeid":
                                                      p_type,
                                                      'task_hash':
                                                      request.POST['hash']
                                                  }),
                                          False,
                                          user=request.user)

        return response.Response(constant.to_response(_ret))

    else:
        return response.Response(constant.ILLEGAL_REQUEST_PARAMETERS)