def add_problem(request): if request.method == 'POST': # 当提交表单时 form = ProblemAddForm(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)) print(request.POST['random_name']) shutil.rmtree('/tmp/' + request.POST['random_name']) return redirect(reverse("problem_detail", args=[problem.problem_id])) else: # 当正常访问时 form = ProblemAddForm() return render(request, 'problem_add.html', {'form': form, 'title': '新建编程题'})
def update_problem(request, id): problem = get_object_or_404(Problem, pk=id) 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_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 = ProblemAddForm(request.POST) if form.is_valid(): form.save(user=request.user, problemid=id) 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("problem_detail", args=[id])) return render(request, 'problem_add.html', {'form': ProblemAddForm(initial=initial)})
def add_problem(request): if request.method == 'POST': # 当提交表单时 form = ProblemAddForm(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("problem_detail", args=[problem.problem_id])) else: # 当正常访问时 form = ProblemAddForm() return render(request, 'problem_add.html', {'form': form, 'title': '新建编程题'})
def update_problem(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, 'program': problem.program, '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 = ProblemAddForm(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("problem_detail", args=[id])) return render(request, 'problem_add.html', {'form': ProblemAddForm(initial=initial)})