inital commit 1
This commit is contained in:
commit
db645123c7
196 files changed
+11102
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,39 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.6 on 2017-05-09 06:41
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import jsonfield.fields
|
||||
import utils.models
|
||||
import utils.shortcuts
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Submission',
|
||||
fields=[
|
||||
('id', models.CharField(db_index=True, default=utils.shortcuts.rand_str, max_length=32, primary_key=True, serialize=False)),
|
||||
('contest_id', models.IntegerField(db_index=True, null=True)),
|
||||
('problem_id', models.IntegerField(db_index=True)),
|
||||
('created_time', models.DateTimeField(auto_now_add=True)),
|
||||
('user_id', models.IntegerField(db_index=True)),
|
||||
('code', utils.models.RichTextField()),
|
||||
('result', models.IntegerField(default=6)),
|
||||
('info', jsonfield.fields.JSONField(default={})),
|
||||
('language', models.CharField(max_length=20)),
|
||||
('shared', models.BooleanField(default=False)),
|
||||
('accepted_time', models.IntegerField(blank=True, null=True)),
|
||||
('accepted_info', jsonfield.fields.JSONField(default={})),
|
||||
],
|
||||
options={
|
||||
'db_table': 'submission',
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,38 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.6 on 2017-05-09 12:03
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('submission', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='code',
|
||||
field=models.TextField(),
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='submission',
|
||||
old_name='accepted_info',
|
||||
new_name='statistic_info',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='submission',
|
||||
name='accepted_time',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='submission',
|
||||
old_name='created_time',
|
||||
new_name='create_time',
|
||||
),
|
||||
migrations.AlterModelOptions(
|
||||
name='submission',
|
||||
options={'ordering': ('-create_time',)},
|
||||
)
|
||||
]
|
||||
@@ -0,0 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.4 on 2017-08-26 03:47
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('submission', '0002_auto_20170509_1203'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='submission',
|
||||
name='username',
|
||||
field=models.CharField(default="", max_length=30),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.4 on 2017-08-30 11:54
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('submission', '0005_submission_username'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='result',
|
||||
field=models.IntegerField(db_index=True, default=6),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.4 on 2017-09-23 13:18
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import django.contrib.postgres.fields.jsonb
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('submission', '0006_auto_20170830_1154'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='contest_id',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='contest.Contest'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='problem_id',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='problem.Problem'),
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='submission',
|
||||
old_name='contest_id',
|
||||
new_name='contest',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='submission',
|
||||
old_name='problem_id',
|
||||
new_name='problem',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='info',
|
||||
field=django.contrib.postgres.fields.jsonb.JSONField(default=dict),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='statistic_info',
|
||||
field=django.contrib.postgres.fields.jsonb.JSONField(default=dict),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.4 on 2017-11-10 06:57
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('submission', '0007_auto_20170923_1318'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='submission',
|
||||
name='ip',
|
||||
field=models.CharField(blank=True, max_length=32, null=True),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def delete_user_output(apps, schema_editor):
|
||||
Submission = apps.get_model("submission", "Submission")
|
||||
for item in Submission.objects.all():
|
||||
if "data" in item.info and isinstance(item.info["data"], list):
|
||||
for index in range(len(item.info["data"])):
|
||||
item.info["data"][index]["output"] = ""
|
||||
item.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('submission', '0008_submission_ip'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(delete_user_output, reverse_code=migrations.RunPython.noop)
|
||||
]
|
||||
@@ -0,0 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def fix_rejudge_bugs(apps, schema_editor):
|
||||
Submission = apps.get_model("submission", "Submission")
|
||||
User = apps.get_model("account", "User")
|
||||
|
||||
for user in User.objects.all():
|
||||
submissions = Submission.objects.filter(user_id=user.id, contest__isnull=True)
|
||||
profile = user.userprofile
|
||||
profile.submission_number = submissions.count()
|
||||
profile.accepted_number = submissions.filter(result=0).count()
|
||||
profile.save(update_fields=["submission_number", "accepted_number"])
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('submission', '0009_delete_user_output'),
|
||||
('problem', '0010_problem_spj_compile_ok'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(fix_rejudge_bugs, reverse_code=migrations.RunPython.noop)
|
||||
]
|
||||
@@ -0,0 +1,36 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.3 on 2018-05-01 04:36
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import utils.shortcuts
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('submission', '0011_fix_submission_number'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='id',
|
||||
field=models.TextField(db_index=True, default=utils.shortcuts.rand_str, primary_key=True, serialize=False),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='ip',
|
||||
field=models.TextField(null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='language',
|
||||
field=models.TextField(),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='submission',
|
||||
name='username',
|
||||
field=models.TextField(),
|
||||
),
|
||||
]
|
||||
Whitespace-only changes.
@@ -0,0 +1,59 @@
|
||||
from django.db import models
|
||||
|
||||
from utils.constants import ContestStatus
|
||||
from utils.models import JSONField
|
||||
from problem.models import Problem
|
||||
from contest.models import Contest
|
||||
|
||||
from utils.shortcuts import rand_str
|
||||
|
||||
|
||||
class JudgeStatus:
|
||||
COMPILE_ERROR = -2
|
||||
WRONG_ANSWER = -1
|
||||
ACCEPTED = 0
|
||||
CPU_TIME_LIMIT_EXCEEDED = 1
|
||||
REAL_TIME_LIMIT_EXCEEDED = 2
|
||||
MEMORY_LIMIT_EXCEEDED = 3
|
||||
RUNTIME_ERROR = 4
|
||||
SYSTEM_ERROR = 5
|
||||
PENDING = 6
|
||||
JUDGING = 7
|
||||
PARTIALLY_ACCEPTED = 8
|
||||
|
||||
|
||||
class Submission(models.Model):
|
||||
id = models.TextField(default=rand_str, primary_key=True, db_index=True)
|
||||
contest = models.ForeignKey(Contest, null=True, on_delete=models.CASCADE)
|
||||
problem = models.ForeignKey(Problem, on_delete=models.CASCADE)
|
||||
create_time = models.DateTimeField(auto_now_add=True)
|
||||
user_id = models.IntegerField(db_index=True)
|
||||
username = models.TextField()
|
||||
code = models.TextField()
|
||||
result = models.IntegerField(db_index=True, default=JudgeStatus.PENDING)
|
||||
# 从JudgeServer返回的判题详情
|
||||
info = JSONField(default=dict)
|
||||
language = models.TextField()
|
||||
shared = models.BooleanField(default=False)
|
||||
# 存储该提交所用时间和内存值,方便提交列表显示
|
||||
# {time_cost: "", memory_cost: "", err_info: "", score: 0}
|
||||
statistic_info = JSONField(default=dict)
|
||||
ip = models.TextField(null=True)
|
||||
|
||||
def check_user_permission(self, user, check_share=True):
|
||||
if self.user_id == user.id or user.is_super_admin() or user.can_mgmt_all_problem() or self.problem.created_by_id == user.id:
|
||||
return True
|
||||
|
||||
if check_share:
|
||||
if self.contest and self.contest.status != ContestStatus.CONTEST_ENDED:
|
||||
return False
|
||||
if self.problem.share_submission or self.shared:
|
||||
return True
|
||||
return False
|
||||
|
||||
class Meta:
|
||||
db_table = "submission"
|
||||
ordering = ("-create_time",)
|
||||
|
||||
def __str__(self):
|
||||
return self.id
|
||||
@@ -0,0 +1,51 @@
|
||||
from .models import Submission
|
||||
from utils.api import serializers
|
||||
from utils.serializers import LanguageNameChoiceField
|
||||
|
||||
|
||||
class CreateSubmissionSerializer(serializers.Serializer):
|
||||
problem_id = serializers.IntegerField()
|
||||
language = LanguageNameChoiceField()
|
||||
code = serializers.CharField(max_length=1024 * 1024)
|
||||
contest_id = serializers.IntegerField(required=False)
|
||||
captcha = serializers.CharField(required=False)
|
||||
|
||||
|
||||
class ShareSubmissionSerializer(serializers.Serializer):
|
||||
id = serializers.CharField()
|
||||
shared = serializers.BooleanField()
|
||||
|
||||
|
||||
class SubmissionModelSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Submission
|
||||
fields = "__all__"
|
||||
|
||||
|
||||
# 不显示submission info的serializer, 用于ACM rule_type
|
||||
class SubmissionSafeModelSerializer(serializers.ModelSerializer):
|
||||
problem = serializers.SlugRelatedField(read_only=True, slug_field="_id")
|
||||
|
||||
class Meta:
|
||||
model = Submission
|
||||
exclude = ("info", "contest", "ip")
|
||||
|
||||
|
||||
class SubmissionListSerializer(serializers.ModelSerializer):
|
||||
problem = serializers.SlugRelatedField(read_only=True, slug_field="_id")
|
||||
show_link = serializers.SerializerMethodField()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.user = kwargs.pop("user", None)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
class Meta:
|
||||
model = Submission
|
||||
exclude = ("info", "contest", "code", "ip")
|
||||
|
||||
def get_show_link(self, obj):
|
||||
# 没传user或为匿名user
|
||||
if self.user is None or not self.user.is_authenticated:
|
||||
return False
|
||||
return obj.check_user_permission(self.user)
|
||||
@@ -0,0 +1,78 @@
|
||||
from copy import deepcopy
|
||||
from unittest import mock
|
||||
|
||||
from problem.models import Problem, ProblemTag
|
||||
from utils.api.tests import APITestCase
|
||||
from .models import Submission
|
||||
|
||||
DEFAULT_PROBLEM_DATA = {"_id": "A-110", "title": "test", "description": "<p>test</p>", "input_description": "test",
|
||||
"output_description": "test", "time_limit": 1000, "memory_limit": 256, "difficulty": "Low",
|
||||
"visible": True, "tags": ["test"], "languages": ["C", "C++", "Java", "Python2"], "template": {},
|
||||
"samples": [{"input": "test", "output": "test"}], "spj": False, "spj_language": "C",
|
||||
"spj_code": "", "test_case_id": "499b26290cc7994e0b497212e842ea85",
|
||||
"test_case_score": [{"output_name": "1.out", "input_name": "1.in", "output_size": 0,
|
||||
"stripped_output_md5": "d41d8cd98f00b204e9800998ecf8427e",
|
||||
"input_size": 0, "score": 0}],
|
||||
"rule_type": "ACM", "hint": "<p>test</p>", "source": "test"}
|
||||
|
||||
DEFAULT_SUBMISSION_DATA = {
|
||||
"problem_id": "1",
|
||||
"user_id": 1,
|
||||
"username": "test",
|
||||
"code": "xxxxxxxxxxxxxx",
|
||||
"result": -2,
|
||||
"info": {},
|
||||
"language": "C",
|
||||
"statistic_info": {}
|
||||
}
|
||||
|
||||
|
||||
# todo contest submission
|
||||
|
||||
|
||||
class SubmissionPrepare(APITestCase):
|
||||
def _create_problem_and_submission(self):
|
||||
user = self.create_admin("test", "test123", login=False)
|
||||
problem_data = deepcopy(DEFAULT_PROBLEM_DATA)
|
||||
tags = problem_data.pop("tags")
|
||||
problem_data["created_by"] = user
|
||||
self.problem = Problem.objects.create(**problem_data)
|
||||
for tag in tags:
|
||||
tag = ProblemTag.objects.create(name=tag)
|
||||
self.problem.tags.add(tag)
|
||||
self.problem.save()
|
||||
self.submission_data = deepcopy(DEFAULT_SUBMISSION_DATA)
|
||||
self.submission_data["problem_id"] = self.problem.id
|
||||
self.submission = Submission.objects.create(**self.submission_data)
|
||||
|
||||
|
||||
class SubmissionListTest(SubmissionPrepare):
|
||||
def setUp(self):
|
||||
self._create_problem_and_submission()
|
||||
self.create_user("123", "345")
|
||||
self.url = self.reverse("submission_list_api")
|
||||
|
||||
def test_get_submission_list(self):
|
||||
resp = self.client.get(self.url, data={"limit": "10"})
|
||||
self.assertSuccess(resp)
|
||||
|
||||
|
||||
@mock.patch("submission.views.oj.judge_task.send")
|
||||
class SubmissionAPITest(SubmissionPrepare):
|
||||
def setUp(self):
|
||||
self._create_problem_and_submission()
|
||||
self.user = self.create_user("123", "test123")
|
||||
self.url = self.reverse("submission_api")
|
||||
|
||||
def test_create_submission(self, judge_task):
|
||||
resp = self.client.post(self.url, self.submission_data)
|
||||
self.assertSuccess(resp)
|
||||
judge_task.assert_called()
|
||||
|
||||
def test_create_submission_with_wrong_language(self, judge_task):
|
||||
self.submission_data.update({"language": "Python3"})
|
||||
resp = self.client.post(self.url, self.submission_data)
|
||||
self.assertFailed(resp)
|
||||
self.assertDictEqual(resp.data, {"error": "error",
|
||||
"data": "Python3 is now allowed in the problem"})
|
||||
judge_task.assert_not_called()
|
||||
Whitespace-only changes.
@@ -0,0 +1,7 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from ..views.admin import SubmissionRejudgeAPI
|
||||
|
||||
urlpatterns = [
|
||||
url(r"^submission/rejudge?$", SubmissionRejudgeAPI.as_view(), name="submission_rejudge_api"),
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from ..views.oj import SubmissionAPI, SubmissionListAPI, ContestSubmissionListAPI, SubmissionExistsAPI
|
||||
|
||||
urlpatterns = [
|
||||
url(r"^submission/?$", SubmissionAPI.as_view(), name="submission_api"),
|
||||
url(r"^submissions/?$", SubmissionListAPI.as_view(), name="submission_list_api"),
|
||||
url(r"^submission_exists/?$", SubmissionExistsAPI.as_view(), name="submission_exists"),
|
||||
url(r"^contest_submissions/?$", ContestSubmissionListAPI.as_view(), name="contest_submission_list_api"),
|
||||
]
|
||||
Whitespace-only changes.
@@ -0,0 +1,22 @@
|
||||
from account.decorators import super_admin_required
|
||||
from judge.tasks import judge_task
|
||||
# from judge.dispatcher import JudgeDispatcher
|
||||
from utils.api import APIView
|
||||
from ..models import Submission
|
||||
|
||||
|
||||
class SubmissionRejudgeAPI(APIView):
|
||||
@super_admin_required
|
||||
def get(self, request):
|
||||
id = request.GET.get("id")
|
||||
if not id:
|
||||
return self.error("Parameter error, id is required")
|
||||
try:
|
||||
submission = Submission.objects.select_related("problem").get(id=id, contest_id__isnull=True)
|
||||
except Submission.DoesNotExist:
|
||||
return self.error("Submission does not exists")
|
||||
submission.statistic_info = {}
|
||||
submission.save()
|
||||
|
||||
judge_task.send(submission.id, submission.problem.id)
|
||||
return self.success()
|
||||
@@ -0,0 +1,203 @@
|
||||
import ipaddress
|
||||
|
||||
from account.decorators import login_required, check_contest_permission
|
||||
from contest.models import ContestStatus, ContestRuleType
|
||||
from judge.tasks import judge_task
|
||||
from options.options import SysOptions
|
||||
# from judge.dispatcher import JudgeDispatcher
|
||||
from problem.models import Problem, ProblemRuleType
|
||||
from utils.api import APIView, validate_serializer
|
||||
from utils.cache import cache
|
||||
from utils.captcha import Captcha
|
||||
from utils.throttling import TokenBucket
|
||||
from ..models import Submission
|
||||
from ..serializers import (CreateSubmissionSerializer, SubmissionModelSerializer,
|
||||
ShareSubmissionSerializer)
|
||||
from ..serializers import SubmissionSafeModelSerializer, SubmissionListSerializer
|
||||
|
||||
|
||||
class SubmissionAPI(APIView):
|
||||
def throttling(self, request):
|
||||
# 使用 open_api 的请求暂不做限制
|
||||
auth_method = getattr(request, "auth_method", "")
|
||||
if auth_method == "api_key":
|
||||
return
|
||||
user_bucket = TokenBucket(key=str(request.user.id),
|
||||
redis_conn=cache, **SysOptions.throttling["user"])
|
||||
can_consume, wait = user_bucket.consume()
|
||||
if not can_consume:
|
||||
return "Please wait %d seconds" % (int(wait))
|
||||
|
||||
# ip_bucket = TokenBucket(key=request.session["ip"],
|
||||
# redis_conn=cache, **SysOptions.throttling["ip"])
|
||||
# can_consume, wait = ip_bucket.consume()
|
||||
# if not can_consume:
|
||||
# return "Captcha is required"
|
||||
|
||||
@check_contest_permission(check_type="problems")
|
||||
def check_contest_permission(self, request):
|
||||
contest = self.contest
|
||||
if contest.status == ContestStatus.CONTEST_ENDED:
|
||||
return self.error("The contest have ended")
|
||||
if not request.user.is_contest_admin(contest):
|
||||
user_ip = ipaddress.ip_address(request.session.get("ip"))
|
||||
if contest.allowed_ip_ranges:
|
||||
if not any(user_ip in ipaddress.ip_network(cidr, strict=False) for cidr in contest.allowed_ip_ranges):
|
||||
return self.error("Your IP is not allowed in this contest")
|
||||
|
||||
@validate_serializer(CreateSubmissionSerializer)
|
||||
@login_required
|
||||
def post(self, request):
|
||||
data = request.data
|
||||
hide_id = False
|
||||
if data.get("contest_id"):
|
||||
error = self.check_contest_permission(request)
|
||||
if error:
|
||||
return error
|
||||
contest = self.contest
|
||||
if not contest.problem_details_permission(request.user):
|
||||
hide_id = True
|
||||
|
||||
if data.get("captcha"):
|
||||
if not Captcha(request).check(data["captcha"]):
|
||||
return self.error("Invalid captcha")
|
||||
error = self.throttling(request)
|
||||
if error:
|
||||
return self.error(error)
|
||||
|
||||
try:
|
||||
problem = Problem.objects.get(id=data["problem_id"], contest_id=data.get("contest_id"), visible=True)
|
||||
except Problem.DoesNotExist:
|
||||
return self.error("Problem not exist")
|
||||
if data["language"] not in problem.languages:
|
||||
return self.error(f"{data['language']} is not allowed in the problem")
|
||||
submission = Submission.objects.create(user_id=request.user.id,
|
||||
username=request.user.username,
|
||||
language=data["language"],
|
||||
code=data["code"],
|
||||
problem_id=problem.id,
|
||||
ip=request.session["ip"],
|
||||
contest_id=data.get("contest_id"))
|
||||
# use this for debug
|
||||
# JudgeDispatcher(submission.id, problem.id).judge()
|
||||
judge_task.send(submission.id, problem.id)
|
||||
if hide_id:
|
||||
return self.success()
|
||||
else:
|
||||
return self.success({"submission_id": submission.id})
|
||||
|
||||
@login_required
|
||||
def get(self, request):
|
||||
submission_id = request.GET.get("id")
|
||||
if not submission_id:
|
||||
return self.error("Parameter id doesn't exist")
|
||||
try:
|
||||
submission = Submission.objects.select_related("problem").get(id=submission_id)
|
||||
except Submission.DoesNotExist:
|
||||
return self.error("Submission doesn't exist")
|
||||
if not submission.check_user_permission(request.user):
|
||||
return self.error("No permission for this submission")
|
||||
|
||||
if submission.problem.rule_type == ProblemRuleType.OI or request.user.is_admin_role():
|
||||
submission_data = SubmissionModelSerializer(submission).data
|
||||
else:
|
||||
submission_data = SubmissionSafeModelSerializer(submission).data
|
||||
# 是否有权限取消共享
|
||||
submission_data["can_unshare"] = submission.check_user_permission(request.user, check_share=False)
|
||||
return self.success(submission_data)
|
||||
|
||||
@validate_serializer(ShareSubmissionSerializer)
|
||||
@login_required
|
||||
def put(self, request):
|
||||
"""
|
||||
share submission
|
||||
"""
|
||||
try:
|
||||
submission = Submission.objects.select_related("problem").get(id=request.data["id"])
|
||||
except Submission.DoesNotExist:
|
||||
return self.error("Submission doesn't exist")
|
||||
if not submission.check_user_permission(request.user, check_share=False):
|
||||
return self.error("No permission to share the submission")
|
||||
if submission.contest and submission.contest.status == ContestStatus.CONTEST_UNDERWAY:
|
||||
return self.error("Can not share submission now")
|
||||
submission.shared = request.data["shared"]
|
||||
submission.save(update_fields=["shared"])
|
||||
return self.success()
|
||||
|
||||
|
||||
class SubmissionListAPI(APIView):
|
||||
def get(self, request):
|
||||
if not request.GET.get("limit"):
|
||||
return self.error("Limit is needed")
|
||||
if request.GET.get("contest_id"):
|
||||
return self.error("Parameter error")
|
||||
|
||||
submissions = Submission.objects.filter(contest_id__isnull=True).select_related("problem__created_by")
|
||||
problem_id = request.GET.get("problem_id")
|
||||
myself = request.GET.get("myself")
|
||||
result = request.GET.get("result")
|
||||
username = request.GET.get("username")
|
||||
if problem_id:
|
||||
try:
|
||||
problem = Problem.objects.get(_id=problem_id, contest_id__isnull=True, visible=True)
|
||||
except Problem.DoesNotExist:
|
||||
return self.error("Problem doesn't exist")
|
||||
submissions = submissions.filter(problem=problem)
|
||||
if (myself and myself == "1") or not SysOptions.submission_list_show_all:
|
||||
submissions = submissions.filter(user_id=request.user.id)
|
||||
elif username:
|
||||
submissions = submissions.filter(username__icontains=username)
|
||||
if result:
|
||||
submissions = submissions.filter(result=result)
|
||||
data = self.paginate_data(request, submissions)
|
||||
data["results"] = SubmissionListSerializer(data["results"], many=True, user=request.user).data
|
||||
return self.success(data)
|
||||
|
||||
|
||||
class ContestSubmissionListAPI(APIView):
|
||||
@check_contest_permission(check_type="submissions")
|
||||
def get(self, request):
|
||||
if not request.GET.get("limit"):
|
||||
return self.error("Limit is needed")
|
||||
|
||||
contest = self.contest
|
||||
submissions = Submission.objects.filter(contest_id=contest.id).select_related("problem__created_by")
|
||||
problem_id = request.GET.get("problem_id")
|
||||
myself = request.GET.get("myself")
|
||||
result = request.GET.get("result")
|
||||
username = request.GET.get("username")
|
||||
if problem_id:
|
||||
try:
|
||||
problem = Problem.objects.get(_id=problem_id, contest_id=contest.id, visible=True)
|
||||
except Problem.DoesNotExist:
|
||||
return self.error("Problem doesn't exist")
|
||||
submissions = submissions.filter(problem=problem)
|
||||
|
||||
if myself and myself == "1":
|
||||
submissions = submissions.filter(user_id=request.user.id)
|
||||
elif username:
|
||||
submissions = submissions.filter(username__icontains=username)
|
||||
if result:
|
||||
submissions = submissions.filter(result=result)
|
||||
|
||||
# filter the test submissions submitted before contest start
|
||||
if contest.status != ContestStatus.CONTEST_NOT_START:
|
||||
submissions = submissions.filter(create_time__gte=contest.start_time)
|
||||
|
||||
# 封榜的时候只能看到自己的提交
|
||||
if contest.rule_type == ContestRuleType.ACM:
|
||||
if not contest.real_time_rank and not request.user.is_contest_admin(contest):
|
||||
submissions = submissions.filter(user_id=request.user.id)
|
||||
|
||||
data = self.paginate_data(request, submissions)
|
||||
data["results"] = SubmissionListSerializer(data["results"], many=True, user=request.user).data
|
||||
return self.success(data)
|
||||
|
||||
|
||||
class SubmissionExistsAPI(APIView):
|
||||
def get(self, request):
|
||||
if not request.GET.get("problem_id"):
|
||||
return self.error("Parameter error, problem_id is required")
|
||||
return self.success(request.user.is_authenticated and
|
||||
Submission.objects.filter(problem_id=request.GET["problem_id"],
|
||||
user_id=request.user.id).exists())
|
||||
Reference in new issue
Block a user