Merge branch 'dev' into hohoTT-dev

Conflicts:
	contest/views.py
This commit is contained in:
hohoTT 2015-09-07 20:01:59 +08:00
commit aab770b8bf
18 changed files with 262 additions and 43 deletions

View File

@ -140,12 +140,12 @@ class UserAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("user_admin_api")
user = User.objects.create(username="testx", real_name="xx", admin_type=SUPER_ADMIN)
user.set_password("testxx")
user.save()
user = User.objects.create(username="testy", real_name="yy", admin_type=SUPER_ADMIN)
user.set_password("testyy")
user.save()
self.user1 = User.objects.create(username="testx", real_name="xx", admin_type=SUPER_ADMIN)
self.user1.set_password("testxx")
self.user1.save()
self.user = User.objects.create(username="testy", real_name="yy", admin_type=SUPER_ADMIN)
self.user.set_password("testyy")
self.user.save()
self.client.login(username="testx", password="testxx")
# 以下是编辑用户的测试
@ -164,19 +164,19 @@ class UserAdminAPITest(APITestCase):
self.assertEqual(response.data, {"code": 1, "data": u"该用户不存在!"})
def test_username_exists(self):
data = {"id": 1, "username": "testy", "real_name": "test00",
data = {"id": self.user.id, "username": "testx", "real_name": "test00",
"password": "testaa", "email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"昵称已经存在"})
def test_user_edit_not_password_successfully(self):
data = {"id": 1, "username": "test0", "real_name": "test00",
data = {"id": self.user.id, "username": "test0", "real_name": "test00",
"email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_user_edit_change_password_successfully(self):
data = {"id": 1, "username": "test0", "real_name": "test00", "password": "111111",
data = {"id": self.user.id, "username": "test0", "real_name": "test00", "password": "111111",
"email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)

View File

@ -373,7 +373,7 @@ class ContestProblemAdminAPItEST(APITestCase):
def test_query_contest_problem_exists_by_contest_id(self):
self.client.login(username="test3", password="testaa")
response = self.client.get(self.url + "?contest_id=1")
response = self.client.get(self.url + "?contest_id="+ str(self.global_contest.id))
self.assertEqual(response.data["code"], 0)
self.assertEqual(len(response.data["data"]), 0)

View File

@ -317,9 +317,22 @@ def contest_problems_list_page(request, contest_id):
比赛所有题目的列表页
"""
try:
contest_problems = ContestProblem.objects.filter(contest=Contest.objects.get(id=contest_id)).order_by("sort_index")
except ContestProblem.DoesNotExist:
return error_page(request, u"比赛题目不存在")
contest = Contest.objects.get(id=contest_id)
except Contest.DoesNotExist:
return error_page(request, u"比赛不存在")
contest_problems = ContestProblem.objects.filter(contest=contest).order_by("sort_index")
submissions = ContestSubmission.objects.filter(user=request.user, contest=contest)
state = {}
for item in submissions:
state[item.problem_id] = item.ac
for item in contest_problems:
if item.id in state:
if state[item.id]:
item.state = 1
else:
item.state = 2
else:
item.state = 0
# 右侧的公告列表
announcements = Announcement.objects.filter(is_global=True, visible=True).order_by("-create_time")
return render(request, "oj/contest/contest_problems_list.html", {"contest_problems": contest_problems,
@ -342,7 +355,7 @@ def contest_list_page(request, page=1):
# 筛选我能参加的比赛
join = request.GET.get("join", None)
if join:
contests = contests.filter(Q(contest_type__in=[1, 2]) | Q(groups__in=request.user.group_set.all())).\
contests = contests.filter(Q(contest_type__in=[1, 2]) | Q(groups__in=request.user.group_set.all())). \
filter(end_time__gt=datetime.datetime.now(), start_time__lt=datetime.datetime.now())
paginator = Paginator(contests, 20)
@ -373,7 +386,6 @@ def contest_list_page(request, page=1):
"join": join})
def _cmp(x, y):
if x["total_ac"] > y["total_ac"]:
return 1
@ -390,10 +402,11 @@ def _cmp(x, y):
def contest_rank_page(request, contest_id):
contest = Contest.objects.get(id=contest_id)
contest_problems = ContestProblem.objects.filter(contest=contest).order_by("sort_index")
result = ContestSubmission.objects.values("user_id").annotate(total_submit=Count("user_id"))
result = ContestSubmission.objects.filter(contest=contest).values("user_id").annotate(
total_submit=Sum("total_submission_number"))
for i in range(0, len(result)):
# 这个人所有的提交
submissions = ContestSubmission.objects.filter(user_id=result[i]["user_id"])
submissions = ContestSubmission.objects.filter(user_id=result[i]["user_id"], contest_id=contest_id)
result[i]["submissions"] = {}
for item in submissions:
result[i]["submissions"][item.problem_id] = item
@ -401,11 +414,6 @@ def contest_rank_page(request, contest_id):
result[i]["user"] = User.objects.get(id=result[i]["user_id"])
result[i]["total_time"] = submissions.filter(ac=True).aggregate(total_time=Sum("total_time"))["total_time"]
return render(request, "oj/contest/contest_rank.html",
{"contest": contest, "contest_problems": contest_problems, "result": sorted(result, cmp=_cmp, reverse=True)})
{"contest": contest, "contest_problems": contest_problems,
"result": sorted(result, cmp=_cmp, reverse=True)})

View File

@ -21,7 +21,7 @@ class EditGroupSerializer(serializers.Serializer):
class CreateJoinGroupRequestSerializer(serializers.Serializer):
group_id = serializers.IntegerField()
message = serializers.CharField(max_length=30)
message = serializers.CharField(max_length=30, required=False)
class JoinGroupRequestSerializer(serializers.ModelSerializer):

View File

@ -183,6 +183,8 @@ class JoinGroupAPIView(APIView):
else:
return error_response(u"你已经是小组成员了")
elif group.join_group_setting == 1:
if not data["message"]:
return error_response(u"message : 该字段是必填项。")
try:
JoinGroupRequest.objects.get(user=request.user, group=group, status=False)
return error_response(u"你已经提交过申请了,请等待审核")
@ -295,6 +297,7 @@ def group_page(request, group_id):
return error_page(request, u"小组不存在")
return render(request, "oj/group/group.html", {"group": group})
@login_required
def application_list_page(request, group_id):
try:
@ -305,6 +308,7 @@ def application_list_page(request, group_id):
return render(request, "oj/group/my_application_list.html",
{"group": group, "applications": applications})
@login_required
def application_page(request, request_id):
try:
@ -312,4 +316,4 @@ def application_page(request, request_id):
except JoinGroupRequest.DoesNotExist:
return error_page(request, u"申请不存在")
return render(request, "oj/group/my_application.html",
{"application": application})
{"application": application})

View File

@ -0,0 +1,163 @@
# coding=utf-8
import json
import commands
import hashlib
from multiprocessing import Pool
from settings import max_running_number, lrun_gid, lrun_uid, judger_workspace
from language import languages
from result import result
from compiler import compile_
from judge_exceptions import JudgeClientError, CompileError
from utils import parse_lrun_output
# 下面这个函数作为代理访问实例变量否则Python2会报错是Python2的已知问题
# http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-when-using-pythons-multiprocessing-pool-ma/7309686
def _run(instance, test_case_id):
return instance._judge_one(test_case_id)
class JudgeClient(object):
def __init__(self, language_code, exe_path, max_cpu_time,
max_real_time, max_memory, test_case_dir):
"""
:param language_code: 语言编号
:param exe_path: 可执行文件路径
:param max_cpu_time: 最大cpu时间单位ms
:param max_real_time: 最大执行时间单位ms
:param max_memory: 最大内存单位MB
:param test_case_dir: 测试用例文件夹路径
:return:返回结果list
"""
self._language = languages[language_code]
self._exe_path = exe_path
self._max_cpu_time = max_cpu_time
self._max_real_time = max_real_time
self._max_memory = max_memory
self._test_case_dir = test_case_dir
# 进程池
self._pool = Pool(processes=max_running_number)
def _generate_command(self, test_case_id):
"""
设置相关运行限制 进制访问网络 如果启用tmpfs 就把代码输出写入tmpfs否则写入硬盘
"""
# todo 系统调用白名单 chroot等参数
command = "lrun" + \
" --max-cpu-time " + str(self._max_cpu_time / 1000.0) + \
" --max-real-time " + str(self._max_real_time / 1000.0 * 2) + \
" --max-memory " + str(self._max_memory * 1000 * 1000) + \
" --network false" + \
" --uid " + str(lrun_uid) + \
" --gid " + str(lrun_gid)
execute_command = self._language["execute_command"].format(exe_path=self._exe_path)
command += (" " +
execute_command +
# 0就是stdin
" 0<" + self._test_case_dir + str(test_case_id) + ".in" +
# 1就是stdout
" 1>" + judger_workspace + str(test_case_id) + ".out" +
# 3是stderr包含lrun的输出和程序的异常输出
" 3>&2")
return command
def _parse_lrun_output(self, output):
# 要注意的是 lrun把结果输出到了stderr所以有些情况下lrun的输出可能与程序的一些错误输出的混合的要先分离一下
error = None
# 倒序找到MEMORY的位置
output_start = output.rfind("MEMORY")
if output_start == -1:
raise JudgeClientError("Lrun result parse error")
# 如果不是0说明lrun输出前面有输出也就是程序的stderr有内容
if output_start != 0:
error = output[0:output_start]
# 分离出lrun的输出
output = output[output_start:]
return error, parse_lrun_output(output)
def _compare_output(self, test_case_id):
output_path = judger_workspace + str(test_case_id) + ".out"
try:
f = open(output_path, "r")
except IOError:
# 文件不存在等引发的异常 返回结果错误
return False
try:
std = open(self._test_case_dir+test_case_id+".out", "r")
except IOError:
# 文件不存在等引发的异常 返回结果错误
return False
lines=std.readline()
line_conut = len(lines)
for i in range(0, line_conut-2):
if lines[i]
def _judge_one(self, test_case_id):
# 运行lrun程序 接收返回值
command = self._generate_command(test_case_id)
status_code, output = commands.getstatusoutput(command)
if status_code:
raise JudgeClientError(output)
error, run_result = self._parse_lrun_output(output)
run_result["test_case_id"] = test_case_id
# 如果返回值非0 或者信号量不是0 或者程序的stderr有输出 代表非正常结束
if run_result["exit_code"] or run_result["term_sig"] or run_result["siginaled"] or error:
run_result["result"] = result["runtime_error"]
return run_result
# 代表内存或者时间超过限制了
if run_result["exceed"]:
if run_result["exceed"] == "memory":
run_result["result"] = result["memory_limit_exceeded"]
elif run_result["exceed"] in ["cpu_time", "real_time"]:
run_result["result"] = result["time_limit_exceeded"]
else:
raise JudgeClientError("Error exceeded type: " + run_result["exceed"])
return run_result
# 下面就是代码正常运行了 需要判断代码的输出是否正确
if self._compare_output(test_case_id):
run_result["result"] = result["accepted"]
else:
run_result["result"] = result["wrong_answer"]
return run_result
def run(self):
# 添加到任务队列
_results = []
results = []
for i in range(self._test_case_info["test_case_number"]):
_results.append(self._pool.apply_async(_run, (self, i + 1)))
self._pool.close()
self._pool.join()
for item in _results:
# 注意多进程中的异常只有在get()的时候才会被引发
# http://stackoverflow.com/questions/22094852/how-to-catch-exceptions-in-workers-in-multiprocessing
try:
results.append(item.get())
except Exception as e:
# todo logging
print e
results.append({"result": result["system_error"]})
return results
def __getstate__(self):
# 不同的pool之间进行pickle的时候要排除自己否则报错
# http://stackoverflow.com/questions/25382455/python-notimplementederror-pool-objects-cannot-be-passed-between-processes
self_dict = self.__dict__.copy()
del self_dict['_pool']
return self_dict

View File

@ -2,8 +2,15 @@
import sys
import json
import MySQLdb
import os
# 判断判题模式
judge_model = os.environ.get("judge_model", "default")
if judge_model == "default":
from client import JudgeClient
elif judge_model == "loose":
from loose_client import JudgeClient
from client import JudgeClient
from language import languages
from compiler import compile_
from result import result

View File

@ -52,6 +52,8 @@ class MessageQueue(object):
try:
contest_submission = ContestSubmission.objects.get(user_id=submission.user_id, contest=contest,
problem_id=contest_problem.id)
# 提交次数加1
contest_submission.total_submission_number += 1
if submission.result == result["accepted"]:
@ -65,13 +67,10 @@ class MessageQueue(object):
contest_submission.total_time += int((submission.create_time - contest.start_time).total_seconds() / 60)
# 标记为已经通过
contest_submission.ac = True
# 提交次数加1
contest_submission.total_submission_number += 1
# contest problem ac 计数器加1
contest_problem.total_accepted_number += 1
else:
# 如果这个提交是错误的就罚时20分钟
contest_submission.ac = False
contest_submission.total_time += 20
contest_submission.save()
contest_problem.save()

View File

@ -144,7 +144,7 @@ class ProblemAdminTest(APITestCase):
self.assertEqual(response.data, {"code": 1, "data": u"题目不存在"})
def test_query_problem_exists(self):
data = {"problem_id": 1}
data = {"problem_id": self.problem.id}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data["code"], 0)

View File

@ -185,8 +185,12 @@ class TestCaseUploadAPIView(APIView):
os.mkdir(test_case_dir)
for name in l:
f = open(test_case_dir + name, "wb")
f.write(test_case_file.read(name).replace("\r\n", "\n"))
f.close()
try:
f.write(test_case_file.read(name).replace("\r\n", "\n"))
except MemoryError:
return error_response(u"单个测试数据体积过大!")
finally:
f.close()
l.sort()
file_info = {"test_case_number": len(l) / 2, "test_cases": {}}

View File

@ -73,6 +73,10 @@ li.list-group-item {
color: green;
}
.dealing-flag {
color: #FF9933;
}
.CodeMirror{
min-height: 250px;
_height:250px;

View File

@ -1,9 +1,14 @@
require(["jquery", "csrfToken", "bsAlert"], function ($, csrfTokenHeader, bsAlert) {
$("#sendApplication").click(function (){
var message = $("#applyMessage").val();
console.log(message);
var message;
if ($("#applyMessage").length) {
message = $("#applyMessage").val();
if (!message)
bsAlert("提交失败,请填写申请信息!");
return false;
}
var groupId = window.location.pathname.split("/")[2];
console.log(groupId);
data = {group_id: groupId,message:message}
$.ajax({
url: "/api/group_join/",

View File

@ -69,7 +69,7 @@
</ul>
</nav>
{% else %}
<p>当前没有合适的比赛</p>
<p>当前没有合适的比赛,你可以尝试到<a href="/groups/">小组列表</a>申请加入一些小组,以便参加小组内部的比赛</p>
{% endif %}
</div>
</div>

View File

@ -8,6 +8,9 @@
<li role="presentation"><a
href="/contest/{{ contest_problem.contest.id }}/problem/{{ contest_problem.id }}/submissions/">我的提交</a>
</li>
<li role="presentation"><a
href="/contest/{{ contest_problem.contest.id }}/problems/">返回</a>
</li>
</ul>
<h2 class="text-center">{{ contest_problem.title }}</h2>

View File

@ -40,7 +40,22 @@
<tbody>
{% for item in contest_problems %}
<tr>
<th><span class="glyphicon glyphicon-ok ac-flag"></span></th>
<th>
<span class="glyphicon
{% if item.state %}
{% ifequal item.state 1%}
glyphicon-ok ac-flag
{% else %}
glyphicon-minus dealing-flag
{% endifequal %}
{% endif %}
"></span>
</th>
<th scope="row">
<a href="/contest/{{ item.contest.id }}/problem/{{ item.id }}/" target="_blank">{{ item.sort_index }}</a>
</th>

View File

@ -4,8 +4,11 @@
<div class="container main">
<ul class="nav nav-tabs nav-tabs-google">
<li role="presentation" class="active">
<a href="/group/{{ group.id }}/">详细信息</a></li>
<a href="/group/{{ group.id }}/">详细信息</a>
</li>
{% if group.join_group_setting %}
<li role="presentation"><a href="/group/{{ group.id }}/applications/">我的申请</a></li>
{% endif %}
</ul>
<h2 class="text-center">{{ group.name }}</h2>
@ -30,7 +33,11 @@
</div>
{% endif %}
<div class="form-group">
<button class="btn btn-primary" id="sendApplication">申请加入</button>
<button class="btn btn-primary" id="sendApplication">
{% if group.join_group_setting %}
申请
{% endif %}
加入</button>
</div>
</div>
</div>

View File

@ -97,7 +97,7 @@
<a href="/submissions/">提交</a>&nbsp;&nbsp;
<a href="/contests/">比赛</a>&nbsp;&nbsp;
<a href="/groups/">小组</a>&nbsp;&nbsp;
<a href="/about/">关于</a>
<a href="#">关于</a>
</div>

View File

@ -46,7 +46,7 @@
<li><a href="/submissions/">提交</a></li>
<li><a href="/contests/">比赛</a></li>
<li><a href="/groups/">小组</a></li>
<li><a href="/about/">关于</a></li>
<li><a href="#">关于</a></li>
</ul>
{% if request.user.is_authenticated %}
<ul class="nav navbar-nav navbar-right">
@ -97,4 +97,4 @@
</div>
<!-- footer end -->
</body>
</html>
</html>