mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-12-28 16:12:13 +00:00
后端比赛与比赛题目部分已修改完成
This commit is contained in:
parent
9c43fe92ff
commit
8b4580d8f6
@ -36,7 +36,7 @@ class AnnouncementAdminAPIView(APIView):
|
||||
if request.user.admin_type == SUPER_ADMIN:
|
||||
groups = Group.objects.filter(id__in=data["groups"])
|
||||
else:
|
||||
groups = Group.objects.filter(id__in=data["groups"], created_by=request.user)
|
||||
groups = Group.objects.filter(id__in=data["groups"], admin=request.user)
|
||||
if not groups.count():
|
||||
return error_response(u"至少选择一个小组")
|
||||
else:
|
||||
@ -75,7 +75,7 @@ class AnnouncementAdminAPIView(APIView):
|
||||
if request.user.admin_type == SUPER_ADMIN:
|
||||
groups = Group.objects.filter(id__in=data["groups"])
|
||||
else:
|
||||
groups = Group.objects.filter(id__in=data["groups"], created_by=request.user)
|
||||
groups = Group.objects.filter(id__in=data["groups"], admin=request.user)
|
||||
if not groups.count():
|
||||
return error_response(u"至少选择一个小组")
|
||||
announcement.title = data["title"]
|
||||
|
20
contest/migrations/0003_contestproblem_difficulty.py
Normal file
20
contest/migrations/0003_contestproblem_difficulty.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contest', '0002_contest_visible'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='contestproblem',
|
||||
name='difficulty',
|
||||
field=models.IntegerField(default=1),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
18
contest/migrations/0004_remove_contestproblem_difficulty.py
Normal file
18
contest/migrations/0004_remove_contestproblem_difficulty.py
Normal file
@ -0,0 +1,18 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contest', '0003_contestproblem_difficulty'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='contestproblem',
|
||||
name='difficulty',
|
||||
),
|
||||
]
|
@ -3,6 +3,7 @@ import json
|
||||
from rest_framework import serializers
|
||||
|
||||
from account.models import User
|
||||
from account.serializers import UserSerializer
|
||||
from .models import Contest, ContestProblem
|
||||
|
||||
|
||||
@ -58,6 +59,7 @@ class JSONField(serializers.Field):
|
||||
|
||||
|
||||
class CreateContestProblemSerializer(serializers.Serializer):
|
||||
contest_id = serializers.IntegerField()
|
||||
title = serializers.CharField(max_length=50)
|
||||
description = serializers.CharField(max_length=10000)
|
||||
input_description = serializers.CharField(max_length=10000)
|
||||
@ -67,20 +69,20 @@ class CreateContestProblemSerializer(serializers.Serializer):
|
||||
test_case_id = serializers.CharField(max_length=40)
|
||||
time_limit = serializers.IntegerField()
|
||||
memory_limit = serializers.IntegerField()
|
||||
difficulty = serializers.IntegerField()
|
||||
hint = serializers.CharField(max_length=3000, allow_blank=True)
|
||||
sort_index = serializers.CharField(max_length=30)
|
||||
|
||||
|
||||
class ContestProblemSerializer(serializers.ModelSerializer):
|
||||
samples = JSONField()
|
||||
|
||||
class ContestSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Contest
|
||||
fields = ["title"]
|
||||
fields = ["title", "id"]
|
||||
|
||||
created_by = ContestSerializer()
|
||||
samples = JSONField()
|
||||
contest = ContestSerializer()
|
||||
created_by = UserSerializer()
|
||||
|
||||
class Meta:
|
||||
model = ContestProblem
|
||||
@ -95,7 +97,6 @@ class EditContestProblemSerializer(serializers.Serializer):
|
||||
test_case_id = serializers.CharField(max_length=40)
|
||||
time_limit = serializers.IntegerField()
|
||||
memory_limit = serializers.IntegerField()
|
||||
difficulty = serializers.IntegerField()
|
||||
samples = ContestProblemSampleSerializer()
|
||||
hint = serializers.CharField(max_length=3000, allow_blank=True)
|
||||
visible = serializers.BooleanField()
|
||||
|
129
contest/tests.py
129
contest/tests.py
@ -196,24 +196,141 @@ class ContestAdminAPITest(APITestCase):
|
||||
for item in response.data["data"]:
|
||||
self.assertEqual(item["visible"], True)
|
||||
|
||||
def test_query_by_keyword(self):
|
||||
self.client.login(username="test1", password="testaa")
|
||||
response = self.client.get(self.url + "?keyword=title1")
|
||||
self.assertEqual(response.data["code"], 0)
|
||||
|
||||
|
||||
class ContestProblemAdminAPItEST(APITestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.url = reverse('contest_problem_admin_api')
|
||||
self.user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
|
||||
self.user = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
|
||||
self.user.set_password("testaa")
|
||||
self.user.save()
|
||||
self.client.login(username="test", password="testaa")
|
||||
self. contest_problem = ContestProblem.objects.create(title="title1",
|
||||
description="description1",
|
||||
self.client.login(username="test1", password="testaa")
|
||||
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
|
||||
contest_type=2, 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=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,
|
||||
difficulty=1,
|
||||
hint="hint1",
|
||||
created_by=User.objects.get(username="test"),
|
||||
created_by=User.objects.get(username="test1"),
|
||||
contest=Contest.objects.get(title="titlex"),
|
||||
sort_index="a")
|
||||
|
||||
# 以下是发布比赛题目的测试
|
||||
def test_invalid_format(self):
|
||||
data = {"title": "test1"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
|
||||
def test_release_contest_problem_successfully(self):
|
||||
data = {"title": "title2",
|
||||
"description": "description2",
|
||||
"input_description": "input_description2",
|
||||
"output_description": "output_description2",
|
||||
"test_case_id": "1",
|
||||
"source": "source1",
|
||||
"samples": [{"input": "1 1", "output": "2"}],
|
||||
"time_limit": "100",
|
||||
"memory_limit": "1000",
|
||||
"hint": "hint1",
|
||||
"sort_index": "b",
|
||||
"contest_id": self.global_contest.id}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data["code"], 0)
|
||||
|
||||
def test_contest_does_not_exists(self):
|
||||
data = {"title": "titlezzzzzzzz",
|
||||
"description": "descriptionzzzzzzzzzzz",
|
||||
"input_description": "input_description2",
|
||||
"output_description": "output_description2",
|
||||
"test_case_id": "1",
|
||||
"source": "source1",
|
||||
"samples": [{"input": "1 1", "output": "2"}],
|
||||
"time_limit": "100",
|
||||
"memory_limit": "1000",
|
||||
"hint": "hint1",
|
||||
"sort_index": "b",
|
||||
"contest_id": self.global_contest.id + 10}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 1, "data": u"比赛不存在"})
|
||||
|
||||
# 以下是编辑比赛题目的测试
|
||||
def test_invalid_data(self):
|
||||
data = {"title": "test1"}
|
||||
response = self.client.put(self.url, data=data)
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
|
||||
def test_edit_problem_does_not_exist(self):
|
||||
data = {"id": self.contest_problem.id + 1,
|
||||
"title": "title2",
|
||||
"description": "description2",
|
||||
"input_description": "input_description2",
|
||||
"output_description": "output_description2",
|
||||
"test_case_id": "1",
|
||||
"source": "source1",
|
||||
"samples": [{"input": "1 1", "output": "2"}],
|
||||
"time_limit": "100",
|
||||
"memory_limit": "1000",
|
||||
"hint": "hint1",
|
||||
"sort_index": "b",
|
||||
"visible": True}
|
||||
response = self.client.put(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 1, "data": u"该比赛题目不存在!"})
|
||||
|
||||
def test_edit_problem_successfully(self):
|
||||
data = {"id": self.contest_problem.id,
|
||||
"title": "title2222222",
|
||||
"description": "description22222222",
|
||||
"input_description": "input_description2",
|
||||
"output_description": "output_description2",
|
||||
"test_case_id": "1",
|
||||
"source": "source1",
|
||||
"samples": [{"input": "1 1", "output": "2"}],
|
||||
"time_limit": "100",
|
||||
"memory_limit": "1000",
|
||||
"hint": "hint1",
|
||||
"sort_index": "b",
|
||||
"visible": True}
|
||||
response = self.client.put(self.url, data=data)
|
||||
self.assertEqual(response.data["code"], 0)
|
||||
|
||||
# 以下是比赛题目分页的测试
|
||||
def test_get_data_successfully(self):
|
||||
self.client.login(username="test1", password="testaa")
|
||||
self.assertEqual(self.client.get(self.url).data["code"], 0)
|
||||
|
||||
def test_keyword_contest(self):
|
||||
self.client.login(username="test1", password="testaa")
|
||||
response = self.client.get(self.url + "?visible=true")
|
||||
self.assertEqual(response.data["code"], 0)
|
||||
for item in response.data["data"]:
|
||||
self.assertEqual(item["visible"], True)
|
||||
|
||||
def test_query_by_keyword(self):
|
||||
self.client.login(username="test1", password="testaa")
|
||||
response = self.client.get(self.url + "?keyword=title1")
|
||||
self.assertEqual(response.data["code"], 0)
|
||||
|
||||
def test_query_contest_problem_does_not_exist(self):
|
||||
data = {"contest_problem_id": 1000000}
|
||||
response = self.client.get(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 1, "data": u"比赛题目不存在"})
|
||||
|
||||
def test_query_contest_problem_exists(self):
|
||||
data = {"contest_problem_id": self.contest_problem.id}
|
||||
response = self.client.get(self.url, data=data)
|
||||
self.assertEqual(response.data["code"], 0)
|
||||
|
||||
|
||||
|
@ -148,6 +148,10 @@ class ContestProblemAdminAPIView(APIView):
|
||||
serializer = CreateContestProblemSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
data = serializer.data
|
||||
try:
|
||||
contest = Contest.objects.get(id=data["contest_id"])
|
||||
except Contest.DoesNotExist:
|
||||
return error_response(u"比赛不存在")
|
||||
contest_problem = ContestProblem.objects.create(title=data["title"],
|
||||
description=data["description"],
|
||||
input_description=data["input_description"],
|
||||
@ -156,10 +160,9 @@ class ContestProblemAdminAPIView(APIView):
|
||||
samples=json.dumps(data["samples"]),
|
||||
time_limit=data["time_limit"],
|
||||
memory_limit=data["memory_limit"],
|
||||
difficulty=data["difficulty"],
|
||||
created_by=request.user,
|
||||
hint=data["hint"],
|
||||
contest=request.contest,
|
||||
contest=contest,
|
||||
sort_index=data["sort_index"])
|
||||
return success_response(ContestProblemSerializer(contest_problem).data)
|
||||
else:
|
||||
@ -169,10 +172,10 @@ class ContestProblemAdminAPIView(APIView):
|
||||
"""
|
||||
比赛题目编辑json api接口
|
||||
---
|
||||
request_serializer: EditContestSerializer
|
||||
request_serializer: EditContestProblemSerializer
|
||||
response_serializer: ContestProblemSerializer
|
||||
"""
|
||||
serializer = EditContestSerializer(data=request.data)
|
||||
serializer = EditContestProblemSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
data = serializer.data
|
||||
try:
|
||||
@ -186,13 +189,12 @@ class ContestProblemAdminAPIView(APIView):
|
||||
contest_problem.test_case_id = data["test_case_id"]
|
||||
contest_problem.time_limit = data["time_limit"]
|
||||
contest_problem.memory_limit = data["memory_limit"]
|
||||
contest_problem.difficulty = data["difficulty"]
|
||||
contest_problem.samples = json.dumps(data["samples"])
|
||||
contest_problem.hint = data["hint"]
|
||||
contest_problem.visible = data["visible"]
|
||||
contest_problem.sort_index = data["sort_index"]
|
||||
contest_problem.save()
|
||||
return success_response(ContestProblem(contest_problem).data)
|
||||
return success_response(ContestProblemSerializer(contest_problem).data)
|
||||
else:
|
||||
return serializer_invalid_response(serializer)
|
||||
|
||||
|
@ -8,7 +8,7 @@ from account.views import (UserLoginAPIView, UsernameCheckAPIView, UserRegisterA
|
||||
UserAdminAPIView, UserInfoAPIView)
|
||||
from announcement.views import AnnouncementAdminAPIView
|
||||
|
||||
from contest.views import ContestAdminAPIView, ContestAdminAPIView
|
||||
from contest.views import ContestAdminAPIView, ContestProblemAdminAPIView
|
||||
|
||||
from group.views import (GroupAdminAPIView, GroupMemberAdminAPIView,
|
||||
JoinGroupAPIView, JoinGroupRequestAdminAPIView)
|
||||
@ -52,7 +52,7 @@ urlpatterns = [
|
||||
url(r'^api/admin/group_member/$', GroupMemberAdminAPIView.as_view(), name="group_member_admin_api"),
|
||||
url(r'^api/admin/group_join/$', JoinGroupAPIView.as_view(), name="group_join_admin_api"),
|
||||
url(r'^api/admin/problem/$', ProblemAdminAPIView.as_view(), name="problem_admin_api"),
|
||||
url(r'^api/admin/contest_problem/$', ContestAdminAPIView.as_view(), name="contest_problem_admin_api"),
|
||||
url(r'^api/admin/contest_problem/$', ContestProblemAdminAPIView.as_view(), name="contest_problem_admin_api"),
|
||||
url(r'^api/admin/test_case_upload/$', TestCaseUploadAPIView.as_view(), name="test_case_upload_api"),
|
||||
url(r'^api/admin/tag/$', ProblemTagAdminAPIView.as_view(), name="problem_tag_admin_api"),
|
||||
url(r'^problem/(?P<problem_id>\d+)/my_submissions/$', "submission.views.problem_my_submissions_list_page",
|
||||
|
Loading…
Reference in New Issue
Block a user