修改判题设置;增加web 和 mongodb 数据库的操作

This commit is contained in:
virusdefender 2015-08-12 14:01:34 +08:00
parent 8979def927
commit 88be032a38
19 changed files with 127 additions and 46 deletions

2
config/config.py Normal file
View File

@ -0,0 +1,2 @@
# coding=utf-8
DB_NAME = "db.sqlite3"

3
judge/README.md Normal file
View 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

View File

@ -2,21 +2,21 @@
languages = {
"1": {
1: {
"name": "c",
"src_name": "main.c",
"code": 1,
"compile_command": "gcc -DONLINE_JUDGE -O2 -w -std=c99 -pipe {src_path} -lm -o {exe_path}main",
"execute_command": "{exe_path}main"
},
"2": {
2: {
"name": "cpp",
"src_name": "main.cpp",
"code": 2,
"compile_command": "g++ -DONLINE_JUDGE -O2 -w -std=c++11 -pipe {src_path} -lm -o {exe_path}main",
"execute_command": "{exe_path}main"
},
"3": {
3: {
"name": "java",
"src_name": "Main.java",
"code": 3,

View File

@ -1,12 +1,17 @@
# coding=utf-8
import sys
import pymongo
from bson.objectid import ObjectId
from client import JudgeClient
from language import languages
from compiler import compile_
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
# 获取到的值是['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]
test_case_id = args[8]
# todo 去数据库查一下
language_code = 1
source_string = """
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a + b);
return 0;
}
"""
language = languages[str(language_code)]
src_path = judger_workspace + language["src_name"]
mongodb_setting = settings.DATABASES["mongodb"]
connection = pymongo.MongoClient(host=mongodb_setting["HOST"], port=mongodb_setting["PORT"])
collection = connection["oj"]["oj_submission"]
submission = collection.find_one({"_id": ObjectId(solution_id)})
if not submission:
exit()
# 将代码写入文件
language = languages[submission["language"]]
src_path = judger_workspace + "run/" + language["src_name"]
f = open(src_path, "w")
f.write(source_string)
f.write(submission["code"])
f.close()
# 编译
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:
print e
print [{"result": result["compile_error"]}]
exit()
client = JudgeClient(language_code=language_code,
client = JudgeClient(language_code=language,
exe_path=exe_path,
max_cpu_time=1000000,
max_real_time=200000,
max_memory=1000,
test_case_dir="/var/test_cases/1/")
test_case_dir="/var/judger/test_case/" + str(test_case_id) + "/")
print client.run()

View File

@ -1,19 +1,28 @@
# coding=utf-8
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__)))
# 下面是需要自己修改的
LOG_PATH = "LOG/"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.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/"

View File

@ -14,6 +14,7 @@ from admin.views import AdminTemplateView
from problem.views import ProblemAdminAPIView
from problem.views import TestCaseUploadAPIView, ProblemTagAdminAPIView
from submission.views import SubmissionnAPIView
urlpatterns = [
@ -50,5 +51,6 @@ urlpatterns = [
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/submission/$', SubmissionnAPIView.as_view(), name="submission_api"),
]

View File

@ -10,6 +10,8 @@ from django.db.models import Q
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 .serizalizers import (CreateProblemSerializer, EditProblemSerializer, ProblemSerializer,
ProblemTagSerializer, CreateProblemTagSerializer)
@ -35,13 +37,6 @@ class ProblemTagAdminAPIView(APIView):
def get(self, request):
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):
@ -159,7 +154,7 @@ class TestCaseUploadAPIView(APIView):
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:
for chunk in f:
test_case_zip.write(chunk)
@ -192,7 +187,7 @@ class TestCaseUploadAPIView(APIView):
return error_response(u"测试用例文件不完整,缺少" + name[0] + ".in")
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)

View File

@ -7,4 +7,5 @@ django-rest-swagger
celery
gunicorn
coverage
subprocess32
subprocess32
pymongo

View File

@ -1,3 +0,0 @@
from django.shortcuts import render
# Create your views here.

View File

@ -1,8 +1,9 @@
require(["jquery", "code_mirror"], function ($, code_mirror) {
var code_editor = code_mirror($("#code-editor")[0], "text/x-csrc");
var language = "1";
$("#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"};
code_editor.setOption("mode", language_types[language]);
});
@ -19,6 +20,9 @@ require(["jquery", "code_mirror"], function ($, code_mirror) {
$("#submit-code-button").click(function () {
show_loading();
$.ajax({
});
setTimeout(
function () {
$("#a").animate({opacity: '1'})

1
submission/models.py Normal file
View File

@ -0,0 +1 @@
# coding=utf-8

10
submission/serializers.py Normal file
View 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
View 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"提交不存在")

View File

@ -66,13 +66,13 @@
<div>
<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 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 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>
</div>
</div>