Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Similar support #205

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions contest/migrations/0010_contest_similarity_check_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2018-12-05 10:13
from __future__ import unicode_literals

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('contest', '0009_auto_20180501_0436'),
]

operations = [
migrations.AddField(
model_name='contest',
name='similarity_check_result',
field=django.contrib.postgres.fields.jsonb.JSONField(default=list),
),
]
1 change: 1 addition & 0 deletions contest/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Contest(models.Model):
# 是否可见 false的话相当于删除
visible = models.BooleanField(default=True)
allowed_ip_ranges = JSONField(default=list)
similarity_check_result = JSONField(default=list)

@property
def status(self):
Expand Down
6 changes: 6 additions & 0 deletions contest/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ class ACMContesHelperSerializer(serializers.Serializer):
problem_id = serializers.CharField()
rank_id = serializers.IntegerField()
checked = serializers.BooleanField()


class ContestSimilarResultSerializer(serializers.ModelSerializer):
class Meta:
model = Contest
fields = ["similarity_check_result"]
9 changes: 9 additions & 0 deletions contest/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class ContestAPITest(APITestCase):
def setUp(self):
user = self.create_admin()
self.contest = Contest.objects.create(created_by=user, **DEFAULT_CONTEST_DATA)
self.contest.similarity_check_result = [{"user1": "aaa", "user2": "bbb"}, {"user1": "ccc", "user2": "ddd"}]
self.contest.save()
self.url = self.reverse("contest_api") + "?id=" + str(self.contest.id)

def test_get_contest_list(self):
Expand Down Expand Up @@ -97,6 +99,13 @@ def test_regular_user_access_contest(self):
resp = self.client.get(self.url)
self.assertSuccess(resp)

def test_regular_user_get_similiar(self):
self.create_user("test", "test123")
url = self.reverse("contest_get_similar_api")
resp = self.client.get(url + "?contest_id=" + str(self.contest.id))
self.assertEqual(resp.data["data"]["similarity_check_result"][0], {"user1": "aaa", "user2": "bbb"})
self.assertEqual(resp.data["data"]["similarity_check_result"][1], {"user1": "ccc", "user2": "ddd"})


class ContestAnnouncementAdminAPITest(APITestCase):
def setUp(self):
Expand Down
3 changes: 2 additions & 1 deletion contest/urls/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.conf.urls import url

from ..views.admin import ContestAnnouncementAPI, ContestAPI, ACMContestHelper, DownloadContestSubmissions
from ..views.admin import ContestAnnouncementAPI, ContestAPI, ACMContestHelper, DownloadContestSubmissions, ContestCheckSimilarAPI

urlpatterns = [
url(r"^contest/?$", ContestAPI.as_view(), name="contest_admin_api"),
url(r"^contest/announcement/?$", ContestAnnouncementAPI.as_view(), name="contest_announcement_admin_api"),
url(r"^contest/acm_helper/?$", ACMContestHelper.as_view(), name="acm_contest_helper"),
url(r"^download_submissions/?$", DownloadContestSubmissions.as_view(), name="acm_contest_helper"),
url(r"^contest_check_similar/?$", ContestCheckSimilarAPI.as_view(), name="contest_check_similar_admin_api")
]
2 changes: 2 additions & 0 deletions contest/urls/oj.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ..views.oj import ContestPasswordVerifyAPI, ContestAccessAPI
from ..views.oj import ContestListAPI, ContestAPI
from ..views.oj import ContestRankAPI
from ..views.oj import ContestGetSimilarAPI

urlpatterns = [
url(r"^contests/?$", ContestListAPI.as_view(), name="contest_list_api"),
Expand All @@ -12,4 +13,5 @@
url(r"^contest/announcement/?$", ContestAnnouncementListAPI.as_view(), name="contest_announcement_api"),
url(r"^contest/access/?$", ContestAccessAPI.as_view(), name="contest_access_api"),
url(r"^contest_rank/?$", ContestRankAPI.as_view(), name="contest_rank_api"),
url(r"^contest/get_similar/?$", ContestGetSimilarAPI.as_view(), name="contest_get_similar_api"),
]
72 changes: 72 additions & 0 deletions contest/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

import dateutil.parser
from django.http import FileResponse
from django.conf import settings

from account.decorators import check_contest_permission, ensure_created_by
from account.models import User
from contest.models import ContestStatus
from problem.models import Problem
from submission.models import Submission, JudgeStatus
from utils.api import APIView, validate_serializer
from utils.cache import cache
Expand All @@ -19,6 +22,7 @@
CreateConetestSeriaizer, CreateContestAnnouncementSerializer,
EditConetestSeriaizer, EditContestAnnouncementSerializer,
ACMContesHelperSerializer, )
from account.decorators import super_admin_required


class ContestAPI(APIView):
Expand Down Expand Up @@ -239,3 +243,71 @@ def get(self, request):
resp["Content-Type"] = "application/zip"
resp["Content-Disposition"] = f"attachment;filename={os.path.basename(zip_path)}"
return resp


class ContestCheckSimilarAPI(APIView):
@super_admin_required
def get(self, request):
cid = request.GET.get("contest_id")
contest = Contest.objects.get(id=cid)
if contest.status != ContestStatus.CONTEST_ENDED:
return self.error("Contest not ended")
problems = Problem.objects.filter(contest_id=cid)
owner = {}
data_to_write = []

for problem in problems:
check_dir = os.path.join(settings.TEST_CASE_DIR, str(problem.id) + "_similar_tmp")
if not os.path.exists(check_dir):
os.mkdir(check_dir)
os.chmod(check_dir, 0o710)

submissions = Submission.objects.filter(problem_id=problem.id, result=JudgeStatus.ACCEPTED)
for submission in submissions:
owner[submission.id] = submission.username
file_path = os.path.join(check_dir, submission.id)
f = open(file_path, "w")
f.write(submission.code)
f.close()

output_dir = os.path.join(check_dir, "result")
check_files = check_dir + "/*"
os.system(f"/app/sim_c -p -t40 -o {output_dir} {check_files}")
f = open(output_dir)
similar_submissions = []
for line in f:
# xxxx consists for xx % of xxxx material
if "consists" in line:
splited_line = line.split()
sub1 = splited_line[0].split("/")[-1]
sub2 = splited_line[-2].split("/")[-1]
if owner[sub1] == owner[sub2]:
continue
similar_submissions.append(sub1)
similar_submissions.append(sub2)
sim = splited_line[3]
to_append = {
"problem_id": problem._id,
"submission_a": sub1,
"user_a": owner[sub1],
"submission_b": sub2,
"user_b": owner[sub2],
"similarity": sim
}
data_to_write.append(to_append)
f.close()

os.remove(output_dir)
for submission in submissions:
owner[submission.id] = submission.username
file_path = os.path.join(check_dir, submission.id)
os.remove(file_path)
if submission.id in similar_submissions:
submission.shared = True
submission.save()
os.rmdir(check_dir)

contest.similarity_check_result = data_to_write
contest.save()

return self.success()
8 changes: 8 additions & 0 deletions contest/views/oj.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ..serializers import ContestAnnouncementSerializer
from ..serializers import ContestSerializer, ContestPasswordVerifySerializer
from ..serializers import OIContestRankSerializer, ACMContestRankSerializer
from ..serializers import ContestSimilarResultSerializer


class ContestAnnouncementListAPI(APIView):
Expand Down Expand Up @@ -185,3 +186,10 @@ def get(self, request):
page_qs = self.paginate_data(request, qs)
page_qs["results"] = serializer(page_qs["results"], many=True, is_contest_admin=is_contest_admin).data
return self.success(page_qs)


class ContestGetSimilarAPI(APIView):
def get(self, request):
contest_id = request.GET.get("contest_id")
contest = Contest.objects.get(id=contest_id, visible=True)
return self.success(ContestSimilarResultSerializer(contest).data)
4 changes: 2 additions & 2 deletions judge/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from account.models import User
from conf.models import JudgeServer
from contest.models import ContestRuleType, ACMContestRank, OIContestRank, ContestStatus
from contest.models import ContestRuleType, ACMContestRank, OIContestRank
from judge.languages import languages, spj_languages
from options.options import SysOptions
from problem.models import Problem, ProblemRuleType
Expand Down Expand Up @@ -177,7 +177,7 @@ def judge(self):
self.release_judge_server(server.id)

if self.contest_id:
if self.contest.status != ContestStatus.CONTEST_UNDERWAY or \
if self.submission.create_time > self.contest.end_time or \
User.objects.get(id=self.submission.user_id).is_contest_admin(self.contest):
logger.info(
"Contest debug mode, id: " + str(self.contest_id) + ", submission id: " + self.submission.id)
Expand Down
Binary file added sim_c
Binary file not shown.