Esempio n. 1
0
def add_tiankong(request):
    if request.method == 'POST':  # 当提交表单时
        form = TiankongProblemAddForm(request.POST)  # form 包含提交的数据
        if form.is_valid():  # 如果提交的数据合法
            problem = form.save(user=request.user)  # 保存题目
            old_path = '/tmp/' + request.POST['random_name'] + '/' + request.POST['file_name'] + '_files/'
            shutil.move(old_path, '/home/judge/data/')
            os.rename('/home/judge/data/' + request.POST['file_name'] + '_files',
                      '/home/judge/data/' + str(problem.problem_id))
            shutil.rmtree('/tmp/' + request.POST['random_name'])
            return redirect(reverse("tiankong_problem_detail", args=[problem.problem_id]))
    else:  # 当正常访问时
        form = TiankongProblemAddForm()
    return render(request, 'tiankong_problem_add.html', {'form': form, 'title': '新建程序填空题'})
Esempio n. 2
0
def update_tiankong(request, id):
    problem = get_object_or_404(Problem, pk=id)
    if request.user != problem.creater and not request.user.is_admin:
        raise PermissionDenied
    json_dic = {}  # 知识点选择的需要的初始化数据
    for point in problem.knowledgePoint2.all():
        json_dic[
            point.
            id] = point.upperPoint.classname.name + ' > ' + point.upperPoint.name + ' > ' + point.name
    initial = {
        'title': problem.title,
        'description': problem.description,
        'time_limit': problem.time_limit,
        'memory_limit': problem.memory_limit,
        'input': problem.input,
        'output': problem.output,
        'sample_code': problem.sample_code,
        'sample_input1': problem.sample_input,
        'sample_output1': problem.sample_output,
        'sample_input2': problem.sample_input2,
        'sample_output2': problem.sample_output2,
        'classname': 0,
        'keypoint': json.dumps(json_dic, ensure_ascii=False).encode('utf8')
    }  # 生成表单的初始化数据
    if request.method == "POST":  # 当提交表单时
        form = TiankongProblemAddForm(request.POST)
        if form.is_valid():
            form.save(user=request.user, problemid=id)
            if request.POST['file_name'] != '':
                try:  # 对文件进行解压和保存
                    old_path = '/tmp/' + request.POST[
                        'random_name'] + '/' + request.POST[
                            'file_name'] + '_files/'
                    store_dir = '/home/judge/data/' + str(id)
                    if os.path.exists(store_dir):
                        shutil.rmtree(store_dir)
                    shutil.move(old_path, '/home/judge/data/')
                    os.rename(
                        '/home/judge/data/' + request.POST['file_name'] +
                        '_files', '/home/judge/data/' + str(id))
                    shutil.rmtree('/tmp/' + request.POST['random_name'])
                except Exception as e:
                    print(e)
                    pass
            return redirect(reverse("tiankong_problem_detail", args=[id]))
    return render(request, 'tiankong_problem_add.html',
                  {'form': TiankongProblemAddForm(initial=initial)})