Ejemplo n.º 1
0
def contest_problems_POST(request, body, user, response, contest_code):
    contest = get_or_none(contest_data, code=contest_code)
    assert_found(contest, 'no contest with this code')
    assert_allowed(contest.owner == user,
                   "you are not allowed to update this contest")

    title = body['title']
    code = body['code']
    description = body['description']
    enabled = bool(body['enabled'])

    problem = get_or_none(problems_data, code=code)
    if problem:
        assert_allowed(problem.contest.owner == user,
                       "problem with this code already exists")
        problem.title = title
        problem.description = description
        problem.enabled = enabled
        problem.save()
    else:
        problem = problems_data.objects.create(title=title,
                                               code=code,
                                               description=description,
                                               contest=contest,
                                               enabled=enabled)

    response['problem'] = get_json_serializable(problem)
    return JsonResponse(response)
Ejemplo n.º 2
0
def submit_POST(request, user, response, problem_code):
	program = body['program']
	username = user.username
	lang = body['lang'].upper()

	problem = get_or_none(problems_data, code = problem_code)
	assert_found(problem, 'problem with this code not found')

	submission = submission_data.objects.create(
		program = program,
		lang = lang,
		user = user,
		problem = problem
		)

	print "submission created"

	for testcase in testcase_data.objects.filter(problem = problem):
		# execute_program.delay(submission.id, testcase.id)
		execute_program(submission.id, testcase.id)
		print "xyz"

	response['message'] = 'submission queued'
	response['submission'] = get_json_serializable(submission)
	return JsonResponse(response)
Ejemplo n.º 3
0
def problem_GET(request, body, problem_code):
    response = {}
    response['success'] = True
    problem = get_or_none(problems_data, code=problem_code)
    assert_found(problem, "no problem with this code found")
    response['problem'] = get_json_serializable(problem)

    return JsonResponse(response)
Ejemplo n.º 4
0
def testcases_GET(request, body, user, response, problem_code):
    problem = get_or_none(problems_data, code=problem_code)
    assert_found(problem, 'problem not found')
    assert_allowed(problem.contest.owner == user,
                   "access denied to the problem details")
    response['testcases'] = serialize(testcase_data, problem=problem)

    return JsonResponse(response)
Ejemplo n.º 5
0
def problem_POST(request, body, user, response, problem_code):
    problem = get_or_none(problems_data, code=problem_code)
    assert_found(problem, 'problem not found')
    assert_allowed(problem.contest.owner == user,
                   "you cannot update the problem")

    id = body['id']
    score = int(body['score'])
    enabled = bool(body['enabled'])
    time_limit = int(body['time_limit'])

    if id == False:
        print "new testcase"
        testcase = testcase_data.objects.create(score=score,
                                                enabled=True,
                                                time_limit=time_limit,
                                                problem=problem)
        id = testcase.id
    else:
        print "old testcase"
        id = int(id)
        testcase = get_or_none(testcase_data, id=id)
        assert_found(testcase, "testcase with this id not found")
        assert_allowed(testcase.problem.contest.owner == user)
        testcase.score = score
        testcase.enabled = enabled
        testcase.time_limit = time_limit
        testcase.save()

    contest_code = testcase.problem.contest.code
    # location = "%s/%s/%s/%s"%(MEDIA_ROOT, contest_code, problem_code, id)
    contest_folder = "%s/%s" % (MEDIA_ROOT, contest_code)
    problem_folder = "%s/%s" % (contest_folder, problem_code)
    location = "%s/%s" % (problem_folder, id)
    create_folder(contest_folder)
    create_folder(problem_folder)
    create_folder(location)

    input_file_path = '%s/%s' % (location, IN)
    output_file_path = '%s/%s' % (location, EXPECTED)

    input_file = request.FILES.get('input_file', False)
    output_file = request.FILES.get('output_file', False)

    if input_file:
        delete_folder_files(input_file_path)
        write_file(input_file_path, input_file.read())
        testcase.input = input_file_path
        testcase.save()

    if output_file:
        delete_folder_files(output_file_path)
        write_file(output_file_path, output_file.read())
        testcase.output = output_file_path
        testcase.save()

    response['testcase'] = get_json_serializable(testcase)
    return JsonResponse(response)
Ejemplo n.º 6
0
def contest_problems_GET(request, body, contest_code):
    response = {}
    response['success'] = True
    contest = get_or_none(contest_data, code=contest_code)
    print contest
    assert_found(contest, 'no contest with this code')
    assert_allowed(contest.is_running or contest.is_ended,
                   'contest hasn\'t started yet')
    response['problems'] = serialize(problems_data,
                                     'get_submission_count',
                                     contest=contest,
                                     enabled=True)
    response['contest'] = get_json_serializable(contest)
    return JsonResponse(response)
Ejemplo n.º 7
0
def execute_program(submission_id, testcase_id):
	response = {}
	submission = get_or_none(submission_data, id = submission_id)
	testcase = get_or_none(testcase_data, id = testcase_id)
	assert_found(testcase)
	assert_found(submission)
	lang = submission.lang
	time_limit = testcase.time_limit
	username = submission.user.username
	path = get_path(username)
	create_folder(path)

	code_filename = '%s.%s'%(username, LANG_COMMAND[lang]['ext'])

	code_file_path = '%s/%s'%(path, code_filename)
	input_file_path = '%s/%s'%(path, IN)
	expected_output_file_path = '%s/%s'%(path, EXPECTED)
	output_file_path = '%s/%s'%(path, OUT)
	time_output = "%s/%s"%(path, TIME_OUTPUT)

	# error in program
	error_file_path = "%s/%s"%(path, ERROR)

	#error code in execution of program
	error_code_file_path = '%s/%s'%(path, ERR_CODE_FILE)

	#error in comparison of output
	error_compare_path = '%s/%s'%(path, ERR_COMPARE_FILE)

	script_path = '%s/%s'%(path, SCRIPT_FILE)
	SAND_COMMAND = SAND%(path)#, SCRIPT_FILE)

	write_file(code_file_path, submission.program)

	testcase_input = "%s/%s"%(BASE_DIR, testcase.input)
	testcase_output = "%s/%s"%(BASE_DIR, testcase.output)

	copy_folder_file(testcase_input, input_file_path)
	copy_folder_file(testcase_output, expected_output_file_path)

	for command in LANG_COMMAND[lang]['commands']:
		current_command = eval(command['command'])
		script = SCRIPTS[command['script']]
		current_script = script % (current_command, command['error_code'], ERR_CODE_FILE, OUT, TIME_OUTPUT)
		delete_folder_files(script_path)
		write_file(script_path, current_script)
		execute(SAND_COMMAND)
		err_code = get_err_code(error_code_file_path)

		if err_code != 0 :
			break

	if err_code != 406:
		response = read_stats(read_file(time_output))
		print response
		# program_output = read_file(output_file_path, response['Number_of_characters'])
		# response['program_output'] = program_output
		# print response

	error_txt = read_file(error_file_path)

	if err_code == 0:
		#compare files
		delete_folder_files(script_path)
		script = SCRIPTS['COMPARE']
		current_script = script % (expected_output_file_path, output_file_path, error_compare_path)
		write_file(script_path, current_script)
		execute(SAND_COMMAND)
		err_code = get_err_code(error_compare_path)

	response['error'] = error_txt
	response['Exit_status'] = err_code
	response['status'] = ERROR_CODE[err_code]

	testcase_submission = testcase_submission_data.objects.create(
		testcase = testcase,
		submission = submission,
		stats = response
		)

	# return testcase_submission
	# logger.info("completed test")
	print "completed test"
	# delete_folder_files(path)
	return response
	# return JsonResponse(response)