def submit_specific_problem(request, problem_id): problem = Problem.objects.get(pk=problem_id) initial_info = {'specific_problem': problem.title} if request.method == "POST": form = SubmitSpecificProblem(request.POST, request.FILES, initial=initial_info) if form.is_valid(): post = form.save(commit=False) post.submit_time = timezone.now() post.user = request.user post.problem = problem post.submit_file = None post.server_id = 1 post.save() post.submit_file = request.FILES.get('submit_file') post.result = 'Judging' post.save() judge_background.apply_async([post.id]) update_statistics(post) return redirect('submit_specific_problem', problem_id) else: form = SubmitSpecificProblem(initial=initial_info) all_submits = Submit.objects.filter( user=request.user, problem=problem, problem__is_public=True).order_by('submit_time').reverse() for i in all_submits: i.source_code = read_source_code(i.submit_file) i.language_mode = i.language.editor_mode form1 = SubmitSpecificProblemWithEditor(initial=initial_info) form1.fields['language'].choices = [(None, '----------')] + [ (i.id, i) for i in Language.objects.filter(enable=True) ] all_submits, paginator = pagination(request, all_submits) context = { 'form': form, 'form1': form1, 'all_submits': all_submits, 'problem': problem, "paginator": paginator, 'submit': 'hover' } return render(request, 'public_specific_problem_submit.html', context)
def public_submit(request): problem_list = Problem.objects.filter(is_public=True).order_by('title') if request.method == "POST": form = SubmitAnswer(request.POST, request.FILES) form.fields['problem'].queryset = problem_list if form.is_valid(): post = form.save(commit=False) post.submit_time = timezone.now() post.user = request.user post.submit_file = None post.server_id = 1 post.save() post.submit_file = request.FILES.get('submit_file') post.result = 'Judging' post.save() judge_background.apply_async([post.id]) # result = judge(file_name=post.submit_file.path, # problem=post.problem, language=post.language, submit=post) # post.result = result # post.save() update_statistics(post) return redirect('public_submit') else: form = SubmitAnswer() form.fields['problem'].queryset = problem_list all_submits = Submit.objects.filter( user=request.user).order_by('submit_time').reverse() for i in all_submits: i.source_code = read_source_code(i.submit_file) i.language_mode = i.language.editor_mode form1 = SubmitWithEditor() form1.fields['problem'].choices = [(None, '----------') ] + [(i.id, i) for i in problem_list] form1.fields['language'].choices = [(None, '----------')] + [ (i.id, i) for i in Language.objects.filter(enable=True) ] all_submits, paginator = pagination(request, all_submits) context = { 'form': form, 'form1': form1, 'all_submits': all_submits, "paginator": paginator, 'submit': 'hover' } return render(request, 'public_submit.html', context)
def submit_specific_problem(request, problem_id): problem = Problem.objects.get(pk=problem_id) initial_info = {'specific_problem': problem.title} if request.method == "POST": form = SubmitSpecificProblem(request.POST, request.FILES, initial=initial_info) if form.is_valid(): post = form.save(commit=False) post.submit_time = timezone.now() post.user = request.user post.problem = problem post.submit_file = None post.save() post.submit_file = request.FILES.get('submit_file') post.save() result = judge(file_name=post.submit_file.path, problem=post.problem, language=post.language, submit=post) post.result = result post.save() update_statistics(post) return redirect('submit_specific_problem', problem_id) else: form = SubmitSpecificProblem(initial=initial_info) all_submits = Submit.objects.filter( user=request.user, problem=problem, problem__is_public=True).order_by('submit_time').reverse() for i in all_submits: i.source_code = read_source_code(i.submit_file) i.language_mode = i.language.editor_mode form1 = SubmitSpecificProblemWithEditor(initial=initial_info) form1.fields['language'].choices = [(None, '----------')] + [ (i.id, i) for i in Language.objects.all() ] return render( request, 'public_specific_problem_submit.html', { 'form': form, 'form1': form1, 'problem': problem, 'all_submits': all_submits })
def public_submit(request): problem_list = Problem.objects.filter(is_public=True).order_by('title') if request.method == "POST": form = SubmitAnswer(request.POST, request.FILES) form.fields['problem'].queryset = problem_list if form.is_valid(): post = form.save(commit=False) post.submit_time = timezone.now() post.user = request.user post.submit_file = None post.save() post.submit_file = request.FILES.get('submit_file') post.save() result = judge(file_name=post.submit_file.path, problem=post.problem, language=post.language, submit=post) post.result = result post.save() update_statistics(post) return redirect('public_submit') else: form = SubmitAnswer() form.fields['problem'].queryset = problem_list all_submits = Submit.objects.filter( user=request.user).order_by('submit_time').reverse() for i in all_submits: i.source_code = read_source_code(i.submit_file) i.language_mode = i.language.editor_mode form1 = SubmitWithEditor() form1.fields['problem'].choices = [(None, '----------') ] + [(i.id, i) for i in problem_list] form1.fields['language'].choices = [(None, '----------')] + [ (i.id, i) for i in Language.objects.all() ] return render(request, 'public_submit.html', { 'form': form, 'form1': form1, 'all_submits': all_submits })
def submit_specific_problem_with_editor(request, problem_id): problem = Problem.objects.get(pk=problem_id) initial_info = {'specific_problem': problem.title} if request.method == "POST": form1 = SubmitSpecificProblemWithEditor(request.POST, initial=initial_info) form1.fields['language'].choices = [(None, '----------')] + [ (i.id, i) for i in Language.objects.filter(enable=True) ] if form1.is_valid(): post = Submit() now = timezone.now() post.submit_time = now post.user = request.user lang = Language.objects.get(pk=int(request.POST['language'])) post.language = lang post.problem = problem post.submit_file = None post.server_id = 1 post.save() path = problem.title + '_' + str( request.user.id) + '_' + str(now) + '.' + lang.extension path = path.replace(' ', '').replace('/', '') source = request.POST['source'] if lang.name == 'Java': path = java_class_name_find(source, path) code = open(path, 'w') code.write(source) code.close() code = open(path, 'rb') source_code = code.read() code.close() submit_file = InMemoryUploadedFile(BytesIO(source_code), 'file', path, 'file/text', sys.getsizeof(source_code), None) post.submit_file = submit_file post.result = 'Judging' post.save() judge_background.apply_async([post.id]) os.system(f'rm "{path}"') update_statistics(post) return redirect('submit_specific_problem', problem_id) else: form1 = SubmitSpecificProblemWithEditor(initial=initial_info) form1.fields['language'].choices = [(None, '----------')] + [ (i.id, i) for i in Language.objects.filter(enable=True) ] all_submits = Submit.objects.filter( user=request.user).order_by('submit_time').reverse() for i in all_submits: i.source_code = read_source_code(i.submit_file) i.language_mode = i.language.editor_mode form = SubmitSpecificProblem(initial=initial_info) all_submits, paginator = pagination(request, all_submits) context = { 'form': form, 'form1': form1, 'all_submits': all_submits, 'problem': problem, "paginator": paginator, 'submit': 'hover' } return render(request, 'public_specific_problem_submit.html', context)
def judge_background(submission_id, rejudge=False, public=False, previous_result=None): submission = Submit.objects.get(id=submission_id) with ChooseJudgeServer() as server: url = server.address + "/judge" with open(submission.submit_file.path, 'r') as f: content = f.read() kwargs = {} memory_limit = submission.problem.memory_limit if memory_limit: memory_limit = int(round(float(memory_limit))) temp_data = { # "headers": {"X-Judge-Server-Token": 'amir'}, "src_code": content, "testcase_id": str(submission.problem.id), "max_cpu_time": int(1000 * submission.problem.time_limit), "max_real_time": 5 * int(1000 * submission.problem.time_limit), "max_memory": memory_limit, "language": submission.language.name, "max_output_size": int(submission.problem.max_output_size), "absolute_error": float(submission.problem.error), } kwargs['json'] = temp_data judge_server_result = requests.get(url, **kwargs).json() # print(judge_server_result) submission.server = server submission.output_path = judge_server_result["user_output_path"] submission.save() if judge_server_result['success']: for item in judge_server_result['data']: if item['result'] == 0: total_result = 'Correct' continue elif item['result'] == 2 or item['result'] == 3: total_result = 'Time Limit Exceeded' break elif item['result'] == 4: total_result = 'Memory Limit Exceeded' break elif item['result'] == 5: total_result = 'Run Time Error' break elif item['result'] == -1: total_result = 'Wrong Answer' break elif item['result'] == 7: total_result = 'No Output' break submission.result = total_result elif judge_server_result['error'] == 'CompileError': submission.result = "Compiler Error" elif judge_server_result['error'] == 'Exception': submission.result = "Run Time Error" submission.save() if not rejudge: testcase_output(judge_server_result['data'], submission) rank_update(submission) update_statistics(submission) else: rejudge_testcase_output(judge_server_result['data'], submission) if not public: update_score_and_rank(submission) else: update_rejudge_statistics(submission, previous_result)
def public_submit_with_editor(request): problem_list = Problem.objects.filter(is_public=True).order_by('title') if request.method == "POST": form1 = SubmitWithEditor(request.POST) form1.fields['problem'].choices = [(None, '----------')] + [ (i.id, i) for i in problem_list ] form1.fields['language'].choices = [(None, '----------')] + [ (i.id, i) for i in Language.objects.all() ] if form1.is_valid(): post = Submit() now = timezone.now() post.submit_time = now post.user = request.user lang = Language.objects.get(pk=int(request.POST['language'])) post.language = lang pro = Problem.objects.get(pk=int(request.POST['problem'])) post.problem = pro post.submit_file = None post.save() source = request.POST['source'] path = pro.title + '_' + str( request.user.id) + '_' + str(now) + '.' + lang.extension path = path.replace(' ', '').replace('/', '') if lang.name == 'Java': path = java_class_name_find(source, path) code = open(path, 'w') code.write(source) code.close() code = open(path, 'rb') source_code = code.read() code.close() submit_file = InMemoryUploadedFile(BytesIO(source_code), 'file', path, 'file/text', sys.getsizeof(source_code), None) post.submit_file = submit_file post.save() result = judge(file_name=post.submit_file.path, problem=post.problem, language=post.language, submit=post) post.result = result post.save() os.system(f'rm "{path}"') update_statistics(post) return redirect('public_submit') else: form1 = SubmitWithEditor() form1.fields['problem'].choices = [(None, '----------')] + [ (i.id, i) for i in problem_list ] form1.fields['language'].choices = [(None, '----------')] + [ (i.id, i) for i in Language.objects.all() ] all_submits = Submit.objects.filter( user=request.user).order_by('submit_time').reverse() for i in all_submits: i.source_code = read_source_code(i.submit_file) i.language_mode = i.language.editor_mode form = SubmitAnswer() form.fields['problem'].queryset = problem_list return render(request, 'public_submit.html', { 'form': form, 'form1': form1, 'all_submits': all_submits })