mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-09-21 00:13:18 +00:00
修改判题设置;增加web 和 mongodb 数据库的操作
This commit is contained in:
parent
8979def927
commit
88be032a38
2
config/config.py
Normal file
2
config/config.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
DB_NAME = "db.sqlite3"
|
3
judge/README.md
Normal file
3
judge/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/usr/bin/docker run -t -i --privileged -v /var/test_case/:/var/judger/test_case/ -v /var/code/:/var/judger/code/ judger /bin/bash
|
||||||
|
|
||||||
|
python judge/judger/run.py -solution_id 1 -max_cpu_time 1 -max_memory 1 -test_case_id 1
|
@ -2,21 +2,21 @@
|
|||||||
|
|
||||||
|
|
||||||
languages = {
|
languages = {
|
||||||
"1": {
|
1: {
|
||||||
"name": "c",
|
"name": "c",
|
||||||
"src_name": "main.c",
|
"src_name": "main.c",
|
||||||
"code": 1,
|
"code": 1,
|
||||||
"compile_command": "gcc -DONLINE_JUDGE -O2 -w -std=c99 -pipe {src_path} -lm -o {exe_path}main",
|
"compile_command": "gcc -DONLINE_JUDGE -O2 -w -std=c99 -pipe {src_path} -lm -o {exe_path}main",
|
||||||
"execute_command": "{exe_path}main"
|
"execute_command": "{exe_path}main"
|
||||||
},
|
},
|
||||||
"2": {
|
2: {
|
||||||
"name": "cpp",
|
"name": "cpp",
|
||||||
"src_name": "main.cpp",
|
"src_name": "main.cpp",
|
||||||
"code": 2,
|
"code": 2,
|
||||||
"compile_command": "g++ -DONLINE_JUDGE -O2 -w -std=c++11 -pipe {src_path} -lm -o {exe_path}main",
|
"compile_command": "g++ -DONLINE_JUDGE -O2 -w -std=c++11 -pipe {src_path} -lm -o {exe_path}main",
|
||||||
"execute_command": "{exe_path}main"
|
"execute_command": "{exe_path}main"
|
||||||
},
|
},
|
||||||
"3": {
|
3: {
|
||||||
"name": "java",
|
"name": "java",
|
||||||
"src_name": "Main.java",
|
"src_name": "Main.java",
|
||||||
"code": 3,
|
"code": 3,
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
import sys
|
import sys
|
||||||
|
import pymongo
|
||||||
|
|
||||||
|
from bson.objectid import ObjectId
|
||||||
|
|
||||||
from client import JudgeClient
|
from client import JudgeClient
|
||||||
from language import languages
|
from language import languages
|
||||||
from compiler import compile_
|
from compiler import compile_
|
||||||
from result import result
|
from result import result
|
||||||
|
from settings import judger_workspace
|
||||||
|
|
||||||
|
from oj import settings
|
||||||
|
|
||||||
judger_workspace = "/var/judger/run/"
|
|
||||||
# 简单的解析命令行参数
|
# 简单的解析命令行参数
|
||||||
# 参数有 -solution_id -max_cpu_time -max_memory -test_case_id
|
# 参数有 -solution_id -max_cpu_time -max_memory -test_case_id
|
||||||
# 获取到的值是['xxx.py', '-solution_id', '1111', '-max_cpu_time', '1000', '-max_memory', '100', '-test_case_id', 'aaaa']
|
# 获取到的值是['xxx.py', '-solution_id', '1111', '-max_cpu_time', '1000', '-max_memory', '100', '-test_case_id', 'aaaa']
|
||||||
@ -16,37 +21,37 @@ max_cpu_time = args[4]
|
|||||||
max_memory = args[6]
|
max_memory = args[6]
|
||||||
test_case_id = args[8]
|
test_case_id = args[8]
|
||||||
|
|
||||||
# todo 去数据库查一下
|
|
||||||
language_code = 1
|
mongodb_setting = settings.DATABASES["mongodb"]
|
||||||
source_string = """
|
connection = pymongo.MongoClient(host=mongodb_setting["HOST"], port=mongodb_setting["PORT"])
|
||||||
#include <stdio.h>
|
collection = connection["oj"]["oj_submission"]
|
||||||
int main()
|
|
||||||
{
|
submission = collection.find_one({"_id": ObjectId(solution_id)})
|
||||||
int a, b;
|
if not submission:
|
||||||
scanf("%d %d", &a, &b);
|
exit()
|
||||||
printf("%d", a + b);
|
|
||||||
return 0;
|
|
||||||
}
|
# 将代码写入文件
|
||||||
"""
|
language = languages[submission["language"]]
|
||||||
language = languages[str(language_code)]
|
src_path = judger_workspace + "run/" + language["src_name"]
|
||||||
src_path = judger_workspace + language["src_name"]
|
|
||||||
f = open(src_path, "w")
|
f = open(src_path, "w")
|
||||||
f.write(source_string)
|
f.write(submission["code"])
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
# 编译
|
||||||
try:
|
try:
|
||||||
exe_path = compile_(languages[str(language_code)], src_path, judger_workspace)
|
exe_path = compile_(language, src_path, judger_workspace + "run/")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print e
|
print e
|
||||||
print [{"result": result["compile_error"]}]
|
print [{"result": result["compile_error"]}]
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
client = JudgeClient(language_code=language_code,
|
client = JudgeClient(language_code=language,
|
||||||
exe_path=exe_path,
|
exe_path=exe_path,
|
||||||
max_cpu_time=1000000,
|
max_cpu_time=1000000,
|
||||||
max_real_time=200000,
|
max_real_time=200000,
|
||||||
max_memory=1000,
|
max_memory=1000,
|
||||||
test_case_dir="/var/test_cases/1/")
|
test_case_dir="/var/judger/test_case/" + str(test_case_id) + "/")
|
||||||
print client.run()
|
print client.run()
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,19 +1,28 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
import os
|
import os
|
||||||
|
|
||||||
LOG_PATH = "LOG/"
|
|
||||||
|
|
||||||
# Database
|
|
||||||
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
|
|
||||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
|
|
||||||
|
# 下面是需要自己修改的
|
||||||
|
LOG_PATH = "LOG/"
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||||
'CONN_MAX_AGE': 1,
|
'CONN_MAX_AGE': 0.3,
|
||||||
|
},
|
||||||
|
'mongodb': {
|
||||||
|
'HOST': '127.0.0.1',
|
||||||
|
'USERNAME': 'root',
|
||||||
|
'PASSWORD': 'root',
|
||||||
|
'PORT': 27017
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
|
||||||
|
TEST_CASE_DIR = "/var/test_case/"
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ from admin.views import AdminTemplateView
|
|||||||
|
|
||||||
from problem.views import ProblemAdminAPIView
|
from problem.views import ProblemAdminAPIView
|
||||||
from problem.views import TestCaseUploadAPIView, ProblemTagAdminAPIView
|
from problem.views import TestCaseUploadAPIView, ProblemTagAdminAPIView
|
||||||
|
from submission.views import SubmissionnAPIView
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@ -50,5 +51,6 @@ urlpatterns = [
|
|||||||
url(r'^my_solution/(?P<solution_id>\d+)/$', "problem.views.my_solution", name="my_solution_page"),
|
url(r'^my_solution/(?P<solution_id>\d+)/$', "problem.views.my_solution", name="my_solution_page"),
|
||||||
|
|
||||||
url(r'^api/admin/join_group_request/$', JoinGroupRequestAdminAPIView.as_view(), name="join_group_request_admin_api"),
|
url(r'^api/admin/join_group_request/$', JoinGroupRequestAdminAPIView.as_view(), name="join_group_request_admin_api"),
|
||||||
|
url(r'^api/submission/$', SubmissionnAPIView.as_view(), name="submission_api"),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
@ -10,6 +10,8 @@ from django.db.models import Q
|
|||||||
|
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from utils.shortcuts import serializer_invalid_response, error_response, success_response, paginate, rand_str
|
from utils.shortcuts import serializer_invalid_response, error_response, success_response, paginate, rand_str
|
||||||
from .serizalizers import (CreateProblemSerializer, EditProblemSerializer, ProblemSerializer,
|
from .serizalizers import (CreateProblemSerializer, EditProblemSerializer, ProblemSerializer,
|
||||||
ProblemTagSerializer, CreateProblemTagSerializer)
|
ProblemTagSerializer, CreateProblemTagSerializer)
|
||||||
@ -35,13 +37,6 @@ class ProblemTagAdminAPIView(APIView):
|
|||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
return success_response(ProblemTagSerializer(ProblemTag.objects.all(), many=True).data)
|
return success_response(ProblemTagSerializer(ProblemTag.objects.all(), many=True).data)
|
||||||
keyword = request.GET.get("keyword", None)
|
|
||||||
if not keyword:
|
|
||||||
return error_response(u"参数错误")
|
|
||||||
tags = ProblemTag.objects.filter(name__contains=keyword)
|
|
||||||
return success_response(ProblemTagSerializer(tags, many=True).data)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def problem_page(request, problem_id):
|
def problem_page(request, problem_id):
|
||||||
@ -159,7 +154,7 @@ class TestCaseUploadAPIView(APIView):
|
|||||||
|
|
||||||
f = request.FILES["file"]
|
f = request.FILES["file"]
|
||||||
|
|
||||||
tmp_zip = "tmp/" + rand_str() + ".zip"
|
tmp_zip = "/tmp/" + rand_str() + ".zip"
|
||||||
with open(tmp_zip, "wb") as test_case_zip:
|
with open(tmp_zip, "wb") as test_case_zip:
|
||||||
for chunk in f:
|
for chunk in f:
|
||||||
test_case_zip.write(chunk)
|
test_case_zip.write(chunk)
|
||||||
@ -192,7 +187,7 @@ class TestCaseUploadAPIView(APIView):
|
|||||||
return error_response(u"测试用例文件不完整,缺少" + name[0] + ".in")
|
return error_response(u"测试用例文件不完整,缺少" + name[0] + ".in")
|
||||||
|
|
||||||
problem_test_dir = rand_str()
|
problem_test_dir = rand_str()
|
||||||
test_case_dir = "test_case/" + problem_test_dir + "/"
|
test_case_dir = settings.TEST_CASE_DIR + "test_case/" + problem_test_dir + "/"
|
||||||
|
|
||||||
# 得到了合法的测试用例文件列表 然后去解压缩
|
# 得到了合法的测试用例文件列表 然后去解压缩
|
||||||
os.mkdir(test_case_dir)
|
os.mkdir(test_case_dir)
|
||||||
|
@ -7,4 +7,5 @@ django-rest-swagger
|
|||||||
celery
|
celery
|
||||||
gunicorn
|
gunicorn
|
||||||
coverage
|
coverage
|
||||||
subprocess32
|
subprocess32
|
||||||
|
pymongo
|
@ -1,3 +0,0 @@
|
|||||||
from django.shortcuts import render
|
|
||||||
|
|
||||||
# Create your views here.
|
|
@ -1,8 +1,9 @@
|
|||||||
require(["jquery", "code_mirror"], function ($, code_mirror) {
|
require(["jquery", "code_mirror"], function ($, code_mirror) {
|
||||||
var code_editor = code_mirror($("#code-editor")[0], "text/x-csrc");
|
var code_editor = code_mirror($("#code-editor")[0], "text/x-csrc");
|
||||||
|
var language = "1";
|
||||||
|
|
||||||
$("#language-selector").change(function () {
|
$("#language-selector").change(function () {
|
||||||
var language = $("#language-selector").val();
|
language = $("#language-selector").val();
|
||||||
var language_types = {c: "text/x-csrc", cpp: "text/x-c++src", java: "text/x-java"};
|
var language_types = {c: "text/x-csrc", cpp: "text/x-c++src", java: "text/x-java"};
|
||||||
code_editor.setOption("mode", language_types[language]);
|
code_editor.setOption("mode", language_types[language]);
|
||||||
});
|
});
|
||||||
@ -19,6 +20,9 @@ require(["jquery", "code_mirror"], function ($, code_mirror) {
|
|||||||
|
|
||||||
$("#submit-code-button").click(function () {
|
$("#submit-code-button").click(function () {
|
||||||
show_loading();
|
show_loading();
|
||||||
|
$.ajax({
|
||||||
|
|
||||||
|
});
|
||||||
setTimeout(
|
setTimeout(
|
||||||
function () {
|
function () {
|
||||||
$("#a").animate({opacity: '1'})
|
$("#a").animate({opacity: '1'})
|
||||||
|
1
submission/models.py
Normal file
1
submission/models.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# coding=utf-8
|
10
submission/serializers.py
Normal file
10
submission/serializers.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class CreateSubmissionSerializer(serializers.Serializer):
|
||||||
|
problem_id = serializers.IntegerField()
|
||||||
|
language = serializers.IntegerField()
|
||||||
|
code = serializers.CharField(max_length=3000)
|
||||||
|
|
52
submission/views.py
Normal file
52
submission/views.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
import pymongo
|
||||||
|
from bson.objectid import ObjectId
|
||||||
|
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
from judge.judger.result import result
|
||||||
|
from account.decorators import login_required
|
||||||
|
from utils.shortcuts import serializer_invalid_response, error_response, success_response
|
||||||
|
from .serializers import CreateSubmissionSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class SubmissionnAPIView(APIView):
|
||||||
|
def _create_mondodb_connection(self):
|
||||||
|
mongodb_setting = settings.DATABASES["mongodb"]
|
||||||
|
connection = pymongo.MongoClient(host=mongodb_setting["HOST"], port=mongodb_setting["PORT"])
|
||||||
|
return connection["oj"]["oj_submission"]
|
||||||
|
|
||||||
|
# @login_required
|
||||||
|
def post(self, request):
|
||||||
|
"""
|
||||||
|
提交代码
|
||||||
|
---
|
||||||
|
request_serializer: CreateSubmissionSerializer
|
||||||
|
"""
|
||||||
|
serializer = CreateSubmissionSerializer(data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
data = serializer.data
|
||||||
|
data["user_id"] = request.user.id
|
||||||
|
data["result"] = result["waiting"]
|
||||||
|
mongodb_setting = settings.DATABASES["mongodb"]
|
||||||
|
connection = pymongo.MongoClient(host=mongodb_setting["HOST"], port=mongodb_setting["PORT"])
|
||||||
|
collection = connection["oj"]["oj_submission"]
|
||||||
|
submission_id = str(collection.insert_one(data).inserted_id)
|
||||||
|
return success_response({"submission_id": submission_id})
|
||||||
|
else:
|
||||||
|
return serializer_invalid_response(serializer)
|
||||||
|
|
||||||
|
# @login_required
|
||||||
|
def get(self, request):
|
||||||
|
submission_id = request.GET.get("submission_id", None)
|
||||||
|
if not submission_id:
|
||||||
|
return error_response(u"参数错误")
|
||||||
|
submission = self._create_mondodb_connection().find_one({"_id": ObjectId(submission_id), "user_id": result.user.id})
|
||||||
|
if submission:
|
||||||
|
return success_response({"result": submission["result"]})
|
||||||
|
else:
|
||||||
|
return error_response(u"提交不存在")
|
@ -66,13 +66,13 @@
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label class="radio-inline">
|
<label class="radio-inline">
|
||||||
<input type="radio" name="inlineRadioOptions" value="c" checked> c (gcc 4.8)
|
<input type="radio" name="inlineRadioOptions" value="1" checked> c (gcc 4.8)
|
||||||
</label>
|
</label>
|
||||||
<label class="radio-inline">
|
<label class="radio-inline">
|
||||||
<input type="radio" name="inlineRadioOptions" value="cpp"> c++ (g++ 4.3)
|
<input type="radio" name="inlineRadioOptions" value="2"> c++ (g++ 4.3)
|
||||||
</label>
|
</label>
|
||||||
<label class="radio-inline">
|
<label class="radio-inline">
|
||||||
<input type="radio" name="inlineRadioOptions" value="java"> Java (jre 1.7)
|
<input type="radio" name="inlineRadioOptions" value="3"> Java (jre 1.7)
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user