From efdf6f21417f810bb44e204e180ff082606e7d5e Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Sat, 17 Oct 2015 17:59:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20contest=5Fsubmission=20app?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest_submission/__init__.py | 0 contest_submission/admin.py | 3 - contest_submission/migrations/__init__.py | 0 contest_submission/models.py | 3 - contest_submission/serializers.py | 11 -- contest_submission/tests.py | 172 -------------------- contest_submission/views.py | 183 ---------------------- 7 files changed, 372 deletions(-) delete mode 100644 contest_submission/__init__.py delete mode 100644 contest_submission/admin.py delete mode 100644 contest_submission/migrations/__init__.py delete mode 100644 contest_submission/models.py delete mode 100644 contest_submission/serializers.py delete mode 100644 contest_submission/tests.py delete mode 100644 contest_submission/views.py diff --git a/contest_submission/__init__.py b/contest_submission/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/contest_submission/admin.py b/contest_submission/admin.py deleted file mode 100644 index 8c38f3f..0000000 --- a/contest_submission/admin.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/contest_submission/migrations/__init__.py b/contest_submission/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/contest_submission/models.py b/contest_submission/models.py deleted file mode 100644 index 71a8362..0000000 --- a/contest_submission/models.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.db import models - -# Create your models here. diff --git a/contest_submission/serializers.py b/contest_submission/serializers.py deleted file mode 100644 index eb9a860..0000000 --- a/contest_submission/serializers.py +++ /dev/null @@ -1,11 +0,0 @@ -# coding=utf-8 -from rest_framework import serializers - -from account.models import User - - -class CreateContestSubmissionSerializer(serializers.Serializer): - contest_id = serializers.IntegerField() - problem_id = serializers.IntegerField() - language = serializers.IntegerField() - code = serializers.CharField(max_length=3000) diff --git a/contest_submission/tests.py b/contest_submission/tests.py deleted file mode 100644 index cb44331..0000000 --- a/contest_submission/tests.py +++ /dev/null @@ -1,172 +0,0 @@ -# coding=utf-8 -import json - -from django.test import TestCase, Client -from django.core.urlresolvers import reverse -from rest_framework.test import APITestCase, APIClient - -from account.models import User, REGULAR_USER, ADMIN, SUPER_ADMIN -from contest.models import Contest, ContestProblem -from contest.models import PUBLIC_CONTEST, PASSWORD_PROTECTED_CONTEST -from submission.models import Submission - - -class ContestSubmissionAPITest(APITestCase): - def setUp(self): - self.client = APIClient() - self.url = reverse('contest_submission_api') - self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN) - self.user1.set_password("testaa") - self.user1.save() - self.user2 = User.objects.create(username="test2", admin_type=REGULAR_USER) - self.user2.set_password("testbb") - self.user2.save() - self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1, - contest_type=PUBLIC_CONTEST, show_rank=True, - show_user_submission=True, - start_time="2015-08-15T10:00:00.000Z", - end_time="2015-08-30T12:00:00.000Z", - created_by=User.objects.get(username="test1")) - self.contest_problem = ContestProblem.objects.create(title="titlex", - description="descriptionx", - input_description="input1_description", - output_description="output1_description", - test_case_id="1", - samples=json.dumps([{"input": "1 1", "output": "2"}]), - time_limit=100, - memory_limit=1000, - hint="hint1", - created_by=User.objects.get(username="test1"), - contest=Contest.objects.get(title="titlex"), - sort_index="a") - - # 以下是创建比赛的提交 - def test_invalid_format(self): - self.client.login(username="test1", password="testaa") - data = {"contest_id": self.global_contest.id, "language": 1} - response = self.client.post(self.url, data=data) - self.assertEqual(response.data["code"], 1) - - def test_contest_submission_successfully(self): - self.client.login(username="test1", password="testaa") - data = {"contest_id": self.global_contest.id, "problem_id": self.contest_problem.id, - "language": 1, "code": '#include "stdio.h"\nint main(){\n\treturn 0;\n}'} - response = self.client.post(self.url, data=data) - self.assertEqual(response.data["code"], 0) - - def test_contest_problem_does_not_exist(self): - self.client.login(username="test1", password="testaa") - data = {"contest_id": self.global_contest.id, "problem_id": self.contest_problem.id + 10, - "language": 1, "code": '#include "stdio.h"\nint main(){\n\treturn 0;\n}'} - response = self.client.post(self.url, data=data) - self.assertEqual(response.data, {"code": 1, "data": u"题目不存在"}) - - -class ContestProblemMySubmissionListTest(TestCase): - # 以下是我比赛单个题目的提交列表的测试 - def setUp(self): - self.client = Client() - self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN) - self.user1.set_password("testaa") - self.user1.save() - self.user2 = User.objects.create(username="test2", admin_type=REGULAR_USER) - self.user2.set_password("testbb") - self.user2.save() - self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1, - contest_type=PUBLIC_CONTEST, show_rank=True, - show_user_submission=True, - start_time="2015-08-15T10:00:00.000Z", - end_time="2015-08-30T12:00:00.000Z", - created_by=User.objects.get(username="test1")) - self.contest_problem = ContestProblem.objects.create(title="titlex", - description="descriptionx", - input_description="input1_description", - output_description="output1_description", - test_case_id="1", - samples=json.dumps([{"input": "1 1", "output": "2"}]), - time_limit=100, - memory_limit=1000, - hint="hint1", - created_by=self.user1, - contest=self.global_contest, - sort_index="a") - - def test_contestsList_page_not_exist(self): - self.client.login(username="test1", password="testaa") - response = self.client.get('/contest/1/submissions/999/') - self.assertTemplateUsed(response, "utils/error.html") - - -class SubmissionAPITest(APITestCase): - def setUp(self): - self.client = APIClient() - self.url = reverse('contest_submission_admin_api_view') - self.userA = User.objects.create(username="test1", admin_type=ADMIN) - self.userA.set_password("testaa") - self.userA.save() - self.userS = User.objects.create(username="test2", admin_type=SUPER_ADMIN) - self.userS.set_password("testbb") - self.userS.save() - self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1, - contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True, - show_user_submission=True, - start_time="2015-08-15T10:00:00.000Z", - end_time="2015-08-15T12:00:00.000Z", - password="aacc", created_by=self.userS - ) - self.problem = ContestProblem.objects.create(title="title1", - description="description1", - input_description="input1_description", - output_description="output1_description", - test_case_id="1", - sort_index="1", - samples=json.dumps([{"input": "1 1", "output": "2"}]), - time_limit=100, - memory_limit=1000, - hint="hint1", - contest=self.global_contest, - created_by=self.userS) - self.submission = Submission.objects.create(user_id=self.userA.id, - language=1, - code='#include "stdio.h"\nint main(){\n\treturn 0;\n}', - problem_id=self.problem.id) - self.submissionS = Submission.objects.create(user_id=self.userS.id, - language=2, - code='#include "stdio.h"\nint main(){\n\treturn 0;\n}', - problem_id=self.problem.id) - - def test_submission_contest_does_not_exist(self): - self.client.login(username="test2", password="testbb") - response = self.client.get(self.url + "?contest_id=99") - self.assertEqual(response.data["code"], 1) - - def test_submission_contest_parameter_error(self): - self.client.login(username="test2", password="testbb") - response = self.client.get(self.url) - self.assertEqual(response.data["code"], 1) - - def test_submission_access_denied(self): - self.client.login(username="test1", password="testaa") - response = self.client.get(self.url + "?problem_id=" + str(self.problem.id)) - self.assertEqual(response.data["code"], 1) - - def test_submission_access_denied_with_contest_id(self): - self.client.login(username="test1", password="testaa") - response = self.client.get(self.url + "?contest_id=" + str(self.global_contest.id)) - self.assertEqual(response.data["code"], 1) - - def test_get_submission_successfully(self): - self.client.login(username="test2", password="testbb") - response = self.client.get( - self.url + "?contest_id=" + str(self.global_contest.id) + "&problem_id=" + str(self.problem.id)) - self.assertEqual(response.data["code"], 0) - - def test_get_submission_successfully_problem(self): - self.client.login(username="test2", password="testbb") - response = self.client.get(self.url + "?problem_id=" + str(self.problem.id)) - self.assertEqual(response.data["code"], 0) - - def test_get_submission_problem_do_not_exist(self): - self.client.login(username="test2", password="testbb") - response = self.client.get(self.url + "?problem_id=9999") - self.assertEqual(response.data["code"], 1) diff --git a/contest_submission/views.py b/contest_submission/views.py deleted file mode 100644 index 4c893d2..0000000 --- a/contest_submission/views.py +++ /dev/null @@ -1,183 +0,0 @@ -# coding=utf-8 -import json -from datetime import datetime -import redis -import pytz -from django.shortcuts import render -from django.core.paginator import Paginator -from django.utils import timezone -from rest_framework.views import APIView - -from judge.judger_controller.tasks import judge - -from account.decorators import login_required -from account.models import SUPER_ADMIN - -from contest.decorators import check_user_contest_permission -from contest.models import Contest, ContestProblem - -from utils.shortcuts import serializer_invalid_response, error_response, success_response, error_page, paginate -from utils.cache import get_cache_redis -from submission.models import Submission -from .serializers import CreateContestSubmissionSerializer -from submission.serializers import SubmissionSerializer - - -class ContestSubmissionAPIView(APIView): - @check_user_contest_permission - def post(self, request): - """ - 创建比赛的提交 - --- - request_serializer: CreateContestSubmissionSerializer - """ - serializer = CreateContestSubmissionSerializer(data=request.data) - if serializer.is_valid(): - data = serializer.data - contest = Contest.objects.get(id=data["contest_id"]) - try: - problem = ContestProblem.objects.get(contest=contest, id=data["problem_id"]) - except ContestProblem.DoesNotExist: - return error_response(u"题目不存在") - submission = Submission.objects.create(user_id=request.user.id, language=int(data["language"]), - contest_id=contest.id, code=data["code"], problem_id=problem.id) - try: - judge.delay(submission.id, problem.time_limit, problem.memory_limit, problem.test_case_id) - except Exception: - return error_response(u"提交判题任务失败") - # 修改用户解题状态 - problems_status = request.user.problems_status - if "contest_problems" not in problems_status: - problems_status["contest_problems"] = {} - problems_status["contest_problems"][str(data["problem_id"])] = 2 - request.user.problems_status = problems_status - request.user.save() - # 增加redis 中判题队列长度的计数器 - r = get_cache_redis() - r.incr("judge_queue_length") - return success_response({"submission_id": submission.id}) - else: - return serializer_invalid_response(serializer) - - -@login_required -def contest_problem_my_submissions_list_page(request, contest_id, contest_problem_id): - """ - 我比赛单个题目的所有提交列表 - """ - try: - Contest.objects.get(id=contest_id) - except Contest.DoesNotExist: - return error_page(request, u"比赛不存在") - try: - contest_problem = ContestProblem.objects.get(id=contest_problem_id, visible=True) - except ContestProblem.DoesNotExist: - return error_page(request, u"比赛问题不存在") - submissions = Submission.objects.filter(user_id=request.user.id, problem_id=contest_problem.id).order_by( - "-create_time"). \ - values("id", "result", "create_time", "accepted_answer_time", "language") - return render(request, "oj/submission/problem_my_submissions_list.html", - {"submissions": submissions, "problem": contest_problem}) - - -@check_user_contest_permission -def contest_problem_submissions_list_page(request, contest_id, page=1): - """ - 单个比赛中的所有提交(包含自己和别人,自己可查提交结果,其他人不可查) - """ - try: - contest = Contest.objects.get(id=contest_id) - except Contest.DoesNotExist: - return error_page(request, u"比赛不存在") - - submissions = Submission.objects.filter(contest_id=contest_id).\ - values("id", "contest_id", "problem_id", "result", "create_time", - "accepted_answer_time", "language", "user_id").order_by("-create_time") - - user_id = request.GET.get("user_id", None) - if user_id: - submissions = submissions.filter(user_id=request.GET.get("user_id")) - - # 封榜的时候只能看到自己的提交 - if not contest.real_time_rank: - if not (request.user.admin_type == SUPER_ADMIN or request.user == contest.created_by): - submissions = submissions.filter(user_id=request.user.id) - - language = request.GET.get("language", None) - filter = None - if language: - submissions = submissions.filter(language=int(language)) - filter = {"name": "language", "content": language} - result = request.GET.get("result", None) - if result: - submissions = submissions.filter(result=int(result)) - filter = {"name": "result", "content": result} - paginator = Paginator(submissions, 20) - - # 为查询题目标题创建新字典 - title = {} - contest_problems = ContestProblem.objects.filter(contest=contest) - for item in contest_problems: - title[item.id] = item.title - for item in submissions: - item['title'] = title[item['problem_id']] - - try: - current_page = paginator.page(int(page)) - except Exception: - return error_page(request, u"不存在的页码") - previous_page = next_page = None - try: - previous_page = current_page.previous_page_number() - except Exception: - pass - try: - next_page = current_page.next_page_number() - except Exception: - pass - - for item in current_page: - # 自己提交的 管理员和创建比赛的可以看到所有的提交链接 - if item["user_id"] == request.user.id or request.user.admin_type == SUPER_ADMIN or \ - request.user == contest.created_by: - item["show_link"] = True - else: - item["show_link"] = False - - return render(request, "oj/contest/submissions_list.html", - {"submissions": current_page, "page": int(page), - "previous_page": previous_page, "next_page": next_page, "start_id": int(page) * 20 - 20, - "contest": contest, "filter": filter, "user_id": user_id}) - - -class ContestSubmissionAdminAPIView(APIView): - def get(self, request): - """ - 查询比赛提交,单个比赛题目提交的adminAPI - --- - response_serializer: SubmissionSerializer - """ - problem_id = request.GET.get("problem_id", None) - contest_id = request.GET.get("contest_id", None) - if contest_id: - try: - contest = Contest.objects.get(pk=contest_id) - except Contest.DoesNotExist: - return error_response(u"比赛不存在!") - if request.user.admin_type != SUPER_ADMIN and contest.created_by != request.user: - return error_response(u"您无权查看该信息!") - submissions = Submission.objects.filter(contest_id=contest_id).order_by("-create_time") - else: - if problem_id: - try: - contest_problem = ContestProblem.objects.get(pk=problem_id) - except ContestProblem.DoesNotExist: - return error_response(u"问题不存在!") - if request.user.admin_type != SUPER_ADMIN and contest_problem.contest.created_by != request.user: - return error_response(u"您无权查看该信息!") - submissions = Submission.objects.filter(contest_id=contest_problem.contest_id).order_by("-create_time") - else: - return error_response(u"参数错误!") - if problem_id: - submissions = submissions.filter(problem_id=problem_id) - return paginate(request, submissions, SubmissionSerializer)